-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: convert id from string to ID with type
- Loading branch information
1 parent
01cde50
commit 6b42bf5
Showing
5 changed files
with
181 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
computer-api/src/main/java/org/apache/hugegraph/computer/core/graph/id/IdCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 org.apache.hugegraph.computer.core.graph.id; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public enum IdCategory { | ||
|
||
NUMBER, | ||
STRING, | ||
UUID, | ||
; | ||
|
||
public static IdCategory parse(String code) { | ||
if (StringUtils.isBlank(code)) { | ||
return null; | ||
} | ||
|
||
for (IdCategory idAlias : IdCategory.values()) { | ||
if (idAlias.name().equals(code.toUpperCase())) { | ||
return idAlias; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
computer-api/src/main/java/org/apache/hugegraph/computer/core/util/IdUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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 org.apache.hugegraph.computer.core.util; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.hugegraph.computer.core.common.exception.ComputerException; | ||
import org.apache.hugegraph.computer.core.graph.id.Id; | ||
import org.apache.hugegraph.computer.core.graph.id.IdCategory; | ||
import org.apache.hugegraph.computer.core.graph.id.IdFactory; | ||
import org.apache.hugegraph.computer.core.graph.id.IdType; | ||
|
||
public class IdUtil { | ||
|
||
private static String UUID_REGEX = "^[0-9a-fA-F]{8}-" + | ||
"[0-9a-fA-F]{4}-" + | ||
"[0-9a-fA-F]{4}-" + | ||
"[0-9a-fA-F]{4}-" + | ||
"[0-9a-fA-F]{12}$"; | ||
private static Pattern P = Pattern.compile(UUID_REGEX); | ||
|
||
public static Id parseId(String idStr) { | ||
if (StringUtils.isBlank(idStr)) { | ||
throw new ComputerException("Can't parse Id for empty string"); | ||
} | ||
|
||
if (StringUtils.isNumeric(idStr)) { | ||
return IdFactory.parseId(IdType.LONG, idStr); | ||
} else if (P.matcher(idStr).matches()) { | ||
return IdFactory.parseId(IdType.UUID, idStr); | ||
} else { | ||
return IdFactory.parseId(IdType.UTF8, idStr); | ||
} | ||
} | ||
|
||
public static Id parseId(IdCategory idCategory, String idStr) { | ||
if (StringUtils.isBlank(idStr)) { | ||
throw new ComputerException("Can't parse Id for empty string"); | ||
} | ||
|
||
if (idCategory == null) { | ||
// automatic inference | ||
return parseId(idStr); | ||
} | ||
|
||
switch (idCategory) { | ||
case NUMBER: | ||
return IdFactory.parseId(IdType.LONG, idStr); | ||
case UUID: | ||
return IdFactory.parseId(IdType.UUID, idStr); | ||
default: | ||
return IdFactory.parseId(IdType.UTF8, idStr); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters