Skip to content

Commit

Permalink
tiny improve
Browse files Browse the repository at this point in the history
Change-Id: I9e36d146f2c9463754914f6b2868416254efe371
  • Loading branch information
Linary committed May 28, 2019
1 parent 94a95ae commit fdc4879
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 36 deletions.
51 changes: 24 additions & 27 deletions hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/*
* Copyright 2017 HugeGraph Authors
*
* * Copyright 2017 HugeGraph Authors
* *
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with this
* * work for additional information regarding copyright ownership. The ASF
* * licenses this file to You under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance with the
* License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.api;
Expand Down Expand Up @@ -62,7 +59,7 @@ public class ProfileAPI {
@GET
@Timed
@Produces(MediaType.APPLICATION_JSON)
public String get(@Context Application application) {
public String getProfile(@Context Application application) {
if (SERVER_PROFILES != null) {
return SERVER_PROFILES;
}
Expand All @@ -72,11 +69,11 @@ public String get(@Context Application application) {
profiles.put("doc", DOC);
profiles.put("api-doc", API_DOC);
Set<String> apis = new HashSet<>();
for (Class<?> aClass : application.getClasses()) {
if (!isAnnotatedPathClass(aClass)) {
for (Class<?> clazz : application.getClasses()) {
if (!isAnnotatedPathClass(clazz)) {
continue;
}
Resource resource = Resource.from(aClass);
Resource resource = Resource.from(clazz);
String fullName = resource.getName();
APICategory apiCategory = getCategory(fullName);
apis.add(apiCategory.category);
Expand All @@ -90,18 +87,18 @@ public String get(@Context Application application) {
@Path("apis")
@Timed
@Produces(MediaType.APPLICATION_JSON)
public String showAll(@Context Application application) {
public String showAllAPIs(@Context Application application) {
if (API_PROFILES != null) {
return API_PROFILES;
}

Map<String, Map<String, List<APIProfile>>> apiProfiles = new HashMap<>();
for (Class<?> aClass : application.getClasses()) {
if (!isAnnotatedPathClass(aClass)) {
for (Class<?> clazz : application.getClasses()) {
if (!isAnnotatedPathClass(clazz)) {
continue;
}

Resource resource = Resource.from(aClass);
Resource resource = Resource.from(clazz);
String fullName = resource.getName();
APICategory apiCategory = getCategory(fullName);

Expand Down Expand Up @@ -134,8 +131,8 @@ private static boolean isAnnotatedPathClass(Class rc) {
if (rc.isAnnotationPresent(Path.class)) {
return true;
}
for (Class i : rc.getInterfaces()) {
if (i.isAnnotationPresent(Path.class)) {
for (Class clazz : rc.getInterfaces()) {
if (clazz.isAnnotationPresent(Path.class)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,26 @@ public <V> Number valueToNumber(V value) {
}

public <V> Date valueToDate(V value) {
if (!this.isDate()) {
return null;
}
if (value instanceof Date) {
return (Date) value;
}
if (this.isDate()) {
if (value instanceof Number) {
return new Date(((Number) value).longValue());
} else if (value instanceof String) {
return DateUtil.parse((String) value);
}
} else if (value instanceof Number) {
return new Date(((Number) value).longValue());
} else if (value instanceof String) {
return DateUtil.parse((String) value);
}
return null;
}

public <V> UUID valueToUUID(V value) {
if (!this.isUUID()) {
return null;
}
if (value instanceof UUID) {
return (UUID) value;
}
if (this.isUUID() && value instanceof String) {
} else if (value instanceof String) {
return java.util.UUID.fromString((String) value);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.baidu.hugegraph.unit.core.BackendMutationTest;
import com.baidu.hugegraph.unit.core.CassandraTest;
import com.baidu.hugegraph.unit.core.ConditionQueryFlattenTest;
import com.baidu.hugegraph.unit.core.DataTypeTest;
import com.baidu.hugegraph.unit.core.DirectionsTest;
import com.baidu.hugegraph.unit.core.EdgeIdTest;
import com.baidu.hugegraph.unit.core.JsonUtilTest;
Expand All @@ -47,6 +48,7 @@
CacheManagerTest.class,

VersionTest.class,
DataTypeTest.class,
DirectionsTest.class,
SerialEnumTest.class,
BackendMutationTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.unit.core;

import java.util.Date;
import java.util.UUID;

import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.Utils;
import com.baidu.hugegraph.type.define.DataType;

public class DataTypeTest {

@Test
public void testValueToNumber() {
Assert.assertNull(DataType.BOOLEAN.valueToNumber(1));
Assert.assertNull(DataType.INT.valueToNumber("not number"));

Assert.assertEquals((byte) 1, DataType.BYTE.valueToNumber(1));
Assert.assertEquals(1, DataType.INT.valueToNumber(1));
Assert.assertEquals(1L, DataType.LONG.valueToNumber(1));
Assert.assertEquals(1.0F, DataType.FLOAT.valueToNumber(1));
Assert.assertEquals(1.0D, DataType.DOUBLE.valueToNumber(1));
}

@Test
public void testValueToDate() {
Date date = Utils.date("2019-01-01 12:00:00");
Assert.assertEquals(date, DataType.DATE.valueToDate(date));
Assert.assertEquals(date,
DataType.DATE.valueToDate("2019-01-01 12:00:00"));
Assert.assertEquals(date, DataType.DATE.valueToDate(date.getTime()));

Assert.assertNull(DataType.TEXT.valueToDate("2019-01-01 12:00:00"));
Assert.assertNull(DataType.DATE.valueToDate(true));
}

@Test
public void testValueToUUID() {
UUID uuid = UUID.randomUUID();
Assert.assertEquals(uuid, DataType.UUID.valueToUUID(uuid));
Assert.assertEquals(uuid, DataType.UUID.valueToUUID(uuid.toString()));

Assert.assertNull(DataType.TEXT.valueToUUID("2019-01-01 12:00:00"));
Assert.assertNull(DataType.UUID.valueToUUID(true));
}
}

0 comments on commit fdc4879

Please sign in to comment.