6 package com.vesoft.nebula.client.graph.data;
8 import com.vesoft.nebula.Value;
9 import com.vesoft.nebula.client.graph.exception.InvalidValueException;
10 import java.io.UnsupportedEncodingException;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
15 import java.util.Objects;
18 public static class NullType {
19 public static final int __NULL__ = 0;
20 public static final int NaN = 1;
21 public static final int BAD_DATA = 2;
22 public static final int BAD_TYPE = 3;
23 public static final int ERR_OVERFLOW = 4;
24 public static final int UNKNOWN_PROP = 5;
25 public static final int DIV_BY_ZERO = 6;
26 public static final int OUT_OF_RANGE = 7;
29 public NullType(
int nullType) {
30 this.nullType = nullType;
33 public int getNullType() {
49 return "ERR_OVERFLOW";
51 return "UNKNOWN_PROP";
55 return "OUT_OF_RANGE";
57 return "Unknown type: " + nullType;
62 private final Value value;
63 private String decodeType =
"utf-8";
64 private int timezoneOffset = 0;
66 private String descType() {
67 switch (value.getSetField()) {
103 throw new IllegalArgumentException(
"Unknown field id " + value.getSetField());
114 this.decodeType = decodeType;
115 this.timezoneOffset = 0;
124 public ValueWrapper(Value value, String decodeType,
int timezoneOffset) {
126 this.decodeType = decodeType;
127 this.timezoneOffset = timezoneOffset;
145 return value.getSetField() == 0;
154 return value.getSetField() == Value.NVAL;
163 return value.getSetField() == Value.BVAL;
172 return value.getSetField() == Value.IVAL;
181 return value.getSetField() == Value.FVAL;
190 return value.getSetField() == Value.SVAL;
199 return value.getSetField() == Value.LVAL;
208 return value.getSetField() == Value.UVAL;
217 return value.getSetField() == Value.MVAL;
226 return value.getSetField() == Value.TVAL;
235 return value.getSetField() == Value.DVAL;
244 return value.getSetField() == Value.DTVAL;
253 return value.getSetField() == Value.VVAL;
262 return value.getSetField() == Value.EVAL;
271 return value.getSetField() == Value.PVAL;
280 return value.getSetField() == Value.GGVAL;
289 return value.getSetField() == Value.DUVAL;
299 if (value.getSetField() == Value.NVAL) {
300 return new NullType(((com.vesoft.nebula.NullType) value.getFieldValue()).getValue());
303 "Cannot get field nullType because value's type is " + descType());
314 if (value.getSetField() == Value.BVAL) {
315 return (
boolean) (value.getFieldValue());
318 "Cannot get field boolean because value's type is " + descType());
328 if (value.getSetField() == Value.IVAL) {
329 return (
long) (value.getFieldValue());
332 "Cannot get field long because value's type is " + descType());
344 if (value.getSetField() == Value.SVAL) {
345 return new String((
byte[]) value.getFieldValue(), decodeType);
348 "Cannot get field string because value's type is " + descType());
358 if (value.getSetField() == Value.FVAL) {
359 return (
double) value.getFieldValue();
362 "Cannot get field double because value's type is " + descType());
372 if (value.getSetField() != Value.LVAL) {
374 "Cannot get field type `list' because value's type is " + descType());
376 ArrayList<ValueWrapper> values =
new ArrayList<>();
377 for (Value value : value.getLVal().getValues()) {
378 values.add(
new ValueWrapper(value, decodeType, timezoneOffset));
390 if (value.getSetField() != Value.UVAL) {
392 "Cannot get field type `set' because value's type is " + descType());
394 HashSet<ValueWrapper> values =
new HashSet<>();
395 for (Value value : value.getUVal().getValues()) {
396 values.add(
new ValueWrapper(value, decodeType, timezoneOffset));
407 public HashMap<String, ValueWrapper>
asMap()
409 if (value.getSetField() != Value.MVAL) {
411 "Cannot get field type `set' because value's type is " + descType());
413 HashMap<String, ValueWrapper> kvs =
new HashMap<>();
414 Map<byte[], Value> inValues = value.getMVal().getKvs();
415 for (
byte[] key : inValues.keySet()) {
416 kvs.put(
new String(key, decodeType),
417 new ValueWrapper(inValues.get(key), decodeType, timezoneOffset));
429 if (value.getSetField() == Value.TVAL) {
431 .setDecodeType(decodeType)
432 .setTimezoneOffset(timezoneOffset);
435 "Cannot get field time because value's type is " + descType());
445 if (value.getSetField() == Value.DVAL) {
449 "Cannot get field date because value's type is " + descType());
459 if (value.getSetField() == Value.DTVAL) {
461 .setDecodeType(decodeType)
462 .setTimezoneOffset(timezoneOffset);
465 "Cannot get field datetime because value's type is " + descType());
476 if (value.getSetField() == Value.VVAL) {
477 return (
Node)
new Node(value.getVVal())
478 .setDecodeType(decodeType)
479 .setTimezoneOffset(timezoneOffset);
482 "Cannot get field Node because value's type is " + descType());
492 if (value.getSetField() == Value.EVAL) {
494 .setDecodeType(decodeType)
495 .setTimezoneOffset(timezoneOffset);
498 "Cannot get field Relationship because value's type is " + descType());
509 if (value.getSetField() == Value.PVAL) {
511 .setDecodeType(decodeType)
512 .setTimezoneOffset(timezoneOffset);
515 "Cannot get field PathWrapper because value's type is " + descType());
525 if (value.getSetField() == Value.GGVAL) {
527 .setDecodeType(decodeType)
528 .setTimezoneOffset(timezoneOffset);
531 "Cannot get field GeographyWrapper because value's type is " + descType());
541 if (value.getSetField() == Value.DUVAL) {
543 .setDecodeType(decodeType)
544 .setTimezoneOffset(timezoneOffset);
551 public boolean equals(Object o) {
555 if (o ==
null || getClass() != o.getClass()) {
559 return Objects.equals(value, that.value)
560 && Objects.equals(decodeType, that.decodeType);
564 public int hashCode() {
565 return Objects.hash(value, decodeType);
579 return asNull().toString();
583 return String.valueOf(
asLong());
589 return asList().toString();
590 }
else if (
isSet()) {
591 return asSet().toString();
592 }
else if (
isMap()) {
593 return asMap().toString();
597 return asDate().toString();
601 return asNode().toString();
605 return asPath().toString();
611 return "Unknown type: " + descType();
612 }
catch (UnsupportedEncodingException e) {
613 return e.getMessage();
double asDouble()
Convert the original data type Value to double.
boolean isSet()
judge the Value is Set type, the Set type is the nebula's type
DateWrapper asDate()
Convert the original data type Value to DateWrapper.
Node asNode()
Convert the original data type Value to Node.
HashMap< String, ValueWrapper > asMap()
Convert the original data type Value to HashMap.
boolean isMap()
judge the Value is Map type, the Map type is the nebula's type
DurationWrapper asDuration()
Convert the original data type Value to duration.
boolean isNull()
judge the Value is Null type,the Null type is the nebula's type
boolean isPath()
judge the Value is Path type, the Path type is the nebula's type
boolean isList()
judge the Value is List type, the List type is the nebula's type
ValueWrapper(Value value, String decodeType)
boolean isVertex()
judge the Value is Vertex type, the Vertex type is the nebula's type
HashSet< ValueWrapper > asSet()
Convert the original data type Value to HashSet.
NullType asNull()
Convert the original data type Value to NullType.
Value getValue()
get the original data structure, the Value is the return from nebula-graph
Relationship asRelationship()
Convert the original data type Value to Relationship.
GeographyWrapper asGeography()
Convert the original data type Value to geography.
boolean isDate()
judge the Value is Date type, the Date type is the nebula's type
boolean isLong()
judge the Value is Long type
boolean isString()
judge the Value is String type
boolean isDouble()
judge the Value is Double type
ArrayList< ValueWrapper > asList()
Convert the original data type Value to ArrayList.
TimeWrapper asTime()
Convert the original data type Value to TimeWrapper.
boolean isEmpty()
judge the Value is Empty type, the Empty type is the nebula's type
boolean isBoolean()
judge the Value is Boolean type
long asLong()
Convert the original data type Value to long.
ValueWrapper(Value value, String decodeType, int timezoneOffset)
boolean isTime()
judge the Value is Time type, the Time type is the nebula's type
boolean isEdge()
judge the Value is Edge type, the Edge type is the nebula's type
PathWrapper asPath()
Convert the original data type Value to Path.
String toString()
Convert Value to String format.
boolean isGeography()
judge the Value is Geography type, the Geography type is the nebula's type
boolean asBoolean()
Convert the original data type Value to boolean.
boolean isDuration()
judge the Value is Duration type, the Duration type is the nebula's type
boolean isDateTime()
judge the Value is DateTime type, the DateTime type is the nebula's type
DateTimeWrapper asDateTime()
Convert the original data type Value to DateTimeWrapper.
String asString()
Convert the original data type Value to String.