NebulaGraph Java Client  release-3.8
ValueWrapper.java
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.graph.data;
7 
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;
14 import java.util.Map;
15 import java.util.Objects;
16 
17 public class ValueWrapper {
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;
27  int nullType;
28 
29  public NullType(int nullType) {
30  this.nullType = nullType;
31  }
32 
33  public int getNullType() {
34  return nullType;
35  }
36 
37  @Override
38  public String toString() {
39  switch (nullType) {
40  case __NULL__:
41  return "NULL";
42  case NaN:
43  return "NaN";
44  case BAD_DATA:
45  return "BAD_DATA";
46  case BAD_TYPE:
47  return "BAD_TYPE";
48  case ERR_OVERFLOW:
49  return "ERR_OVERFLOW";
50  case UNKNOWN_PROP:
51  return "UNKNOWN_PROP";
52  case DIV_BY_ZERO:
53  return "DIV_BY_ZERO";
54  case OUT_OF_RANGE:
55  return "OUT_OF_RANGE";
56  default:
57  return "Unknown type: " + nullType;
58  }
59  }
60  }
61 
62  private final Value value;
63  private String decodeType = "utf-8";
64  private int timezoneOffset = 0;
65 
66  private String descType() {
67  switch (value.getSetField()) {
68  case Value.NVAL:
69  return "NULL";
70  case Value.BVAL:
71  return "BOOLEAN";
72  case Value.IVAL:
73  return "INT";
74  case Value.FVAL:
75  return "FLOAT";
76  case Value.SVAL:
77  return "STRING";
78  case Value.DVAL:
79  return "DATE";
80  case Value.TVAL:
81  return "TIME";
82  case Value.DTVAL:
83  return "DATETIME";
84  case Value.VVAL:
85  return "VERTEX";
86  case Value.EVAL:
87  return "EDGE";
88  case Value.PVAL:
89  return "PATH";
90  case Value.LVAL:
91  return "LIST";
92  case Value.MVAL:
93  return "MAP";
94  case Value.UVAL:
95  return "SET";
96  case Value.GVAL:
97  return "DATASET";
98  case Value.GGVAL:
99  return "GEOGRAPHY";
100  case Value.DUVAL:
101  return "DURATION";
102  default:
103  throw new IllegalArgumentException("Unknown field id " + value.getSetField());
104  }
105  }
106 
112  public ValueWrapper(Value value, String decodeType) {
113  this.value = value;
114  this.decodeType = decodeType;
115  this.timezoneOffset = 0;
116  }
117 
124  public ValueWrapper(Value value, String decodeType, int timezoneOffset) {
125  this.value = value;
126  this.decodeType = decodeType;
127  this.timezoneOffset = timezoneOffset;
128  }
129 
135  public Value getValue() {
136  return value;
137  }
138 
144  public boolean isEmpty() {
145  return value.getSetField() == 0;
146  }
147 
153  public boolean isNull() {
154  return value.getSetField() == Value.NVAL;
155  }
156 
162  public boolean isBoolean() {
163  return value.getSetField() == Value.BVAL;
164  }
165 
171  public boolean isLong() {
172  return value.getSetField() == Value.IVAL;
173  }
174 
180  public boolean isDouble() {
181  return value.getSetField() == Value.FVAL;
182  }
183 
189  public boolean isString() {
190  return value.getSetField() == Value.SVAL;
191  }
192 
198  public boolean isList() {
199  return value.getSetField() == Value.LVAL;
200  }
201 
207  public boolean isSet() {
208  return value.getSetField() == Value.UVAL;
209  }
210 
216  public boolean isMap() {
217  return value.getSetField() == Value.MVAL;
218  }
219 
225  public boolean isTime() {
226  return value.getSetField() == Value.TVAL;
227  }
228 
234  public boolean isDate() {
235  return value.getSetField() == Value.DVAL;
236  }
237 
243  public boolean isDateTime() {
244  return value.getSetField() == Value.DTVAL;
245  }
246 
252  public boolean isVertex() {
253  return value.getSetField() == Value.VVAL;
254  }
255 
261  public boolean isEdge() {
262  return value.getSetField() == Value.EVAL;
263  }
264 
270  public boolean isPath() {
271  return value.getSetField() == Value.PVAL;
272  }
273 
279  public boolean isGeography() {
280  return value.getSetField() == Value.GGVAL;
281  }
282 
288  public boolean isDuration() {
289  return value.getSetField() == Value.DUVAL;
290  }
291 
298  public NullType asNull() throws InvalidValueException {
299  if (value.getSetField() == Value.NVAL) {
300  return new NullType(((com.vesoft.nebula.NullType) value.getFieldValue()).getValue());
301  } else {
302  throw new InvalidValueException(
303  "Cannot get field nullType because value's type is " + descType());
304  }
305  }
306 
313  public boolean asBoolean() throws InvalidValueException {
314  if (value.getSetField() == Value.BVAL) {
315  return (boolean) (value.getFieldValue());
316  }
317  throw new InvalidValueException(
318  "Cannot get field boolean because value's type is " + descType());
319  }
320 
327  public long asLong() throws InvalidValueException {
328  if (value.getSetField() == Value.IVAL) {
329  return (long) (value.getFieldValue());
330  } else {
331  throw new InvalidValueException(
332  "Cannot get field long because value's type is " + descType());
333  }
334  }
335 
343  public String asString() throws InvalidValueException, UnsupportedEncodingException {
344  if (value.getSetField() == Value.SVAL) {
345  return new String((byte[]) value.getFieldValue(), decodeType);
346  }
347  throw new InvalidValueException(
348  "Cannot get field string because value's type is " + descType());
349  }
350 
357  public double asDouble() throws InvalidValueException {
358  if (value.getSetField() == Value.FVAL) {
359  return (double) value.getFieldValue();
360  }
361  throw new InvalidValueException(
362  "Cannot get field double because value's type is " + descType());
363  }
364 
371  public ArrayList<ValueWrapper> asList() throws InvalidValueException {
372  if (value.getSetField() != Value.LVAL) {
373  throw new InvalidValueException(
374  "Cannot get field type `list' because value's type is " + descType());
375  }
376  ArrayList<ValueWrapper> values = new ArrayList<>();
377  for (Value value : value.getLVal().getValues()) {
378  values.add(new ValueWrapper(value, decodeType, timezoneOffset));
379  }
380  return values;
381  }
382 
389  public HashSet<ValueWrapper> asSet() throws InvalidValueException {
390  if (value.getSetField() != Value.UVAL) {
391  throw new InvalidValueException(
392  "Cannot get field type `set' because value's type is " + descType());
393  }
394  HashSet<ValueWrapper> values = new HashSet<>();
395  for (Value value : value.getUVal().getValues()) {
396  values.add(new ValueWrapper(value, decodeType, timezoneOffset));
397  }
398  return values;
399  }
400 
407  public HashMap<String, ValueWrapper> asMap()
408  throws InvalidValueException, UnsupportedEncodingException {
409  if (value.getSetField() != Value.MVAL) {
410  throw new InvalidValueException(
411  "Cannot get field type `set' because value's type is " + descType());
412  }
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));
418  }
419  return kvs;
420  }
421 
429  if (value.getSetField() == Value.TVAL) {
430  return (TimeWrapper) new TimeWrapper(value.getTVal())
431  .setDecodeType(decodeType)
432  .setTimezoneOffset(timezoneOffset);
433  }
434  throw new InvalidValueException(
435  "Cannot get field time because value's type is " + descType());
436  }
437 
445  if (value.getSetField() == Value.DVAL) {
446  return new DateWrapper(value.getDVal());
447  }
448  throw new InvalidValueException(
449  "Cannot get field date because value's type is " + descType());
450  }
451 
459  if (value.getSetField() == Value.DTVAL) {
460  return (DateTimeWrapper) new DateTimeWrapper(value.getDtVal())
461  .setDecodeType(decodeType)
462  .setTimezoneOffset(timezoneOffset);
463  }
464  throw new InvalidValueException(
465  "Cannot get field datetime because value's type is " + descType());
466  }
467 
475  public Node asNode() throws InvalidValueException, UnsupportedEncodingException {
476  if (value.getSetField() == Value.VVAL) {
477  return (Node) new Node(value.getVVal())
478  .setDecodeType(decodeType)
479  .setTimezoneOffset(timezoneOffset);
480  }
481  throw new InvalidValueException(
482  "Cannot get field Node because value's type is " + descType());
483  }
484 
492  if (value.getSetField() == Value.EVAL) {
493  return (Relationship) new Relationship(value.getEVal())
494  .setDecodeType(decodeType)
495  .setTimezoneOffset(timezoneOffset);
496  }
497  throw new InvalidValueException(
498  "Cannot get field Relationship because value's type is " + descType());
499  }
500 
508  public PathWrapper asPath() throws InvalidValueException, UnsupportedEncodingException {
509  if (value.getSetField() == Value.PVAL) {
510  return (PathWrapper) new PathWrapper(value.getPVal())
511  .setDecodeType(decodeType)
512  .setTimezoneOffset(timezoneOffset);
513  }
514  throw new InvalidValueException(
515  "Cannot get field PathWrapper because value's type is " + descType());
516  }
517 
525  if (value.getSetField() == Value.GGVAL) {
526  return (GeographyWrapper) new GeographyWrapper(value.getGgVal())
527  .setDecodeType(decodeType)
528  .setTimezoneOffset(timezoneOffset);
529  }
530  throw new InvalidValueException(
531  "Cannot get field GeographyWrapper because value's type is " + descType());
532  }
533 
541  if (value.getSetField() == Value.DUVAL) {
542  return (DurationWrapper) new DurationWrapper(value.getDuVal())
543  .setDecodeType(decodeType)
544  .setTimezoneOffset(timezoneOffset);
545  }
546  throw new InvalidValueException("Cannot get field DurationWrapper because value's type is "
547  + descType());
548  }
549 
550  @Override
551  public boolean equals(Object o) {
552  if (this == o) {
553  return true;
554  }
555  if (o == null || getClass() != o.getClass()) {
556  return false;
557  }
558  ValueWrapper that = (ValueWrapper) o;
559  return Objects.equals(value, that.value)
560  && Objects.equals(decodeType, that.decodeType);
561  }
562 
563  @Override
564  public int hashCode() {
565  return Objects.hash(value, decodeType);
566  }
567 
573  @Override
574  public String toString() {
575  try {
576  if (isEmpty()) {
577  return "__EMPTY__";
578  } else if (isNull()) {
579  return asNull().toString();
580  } else if (isBoolean()) {
581  return String.valueOf(asBoolean());
582  } else if (isLong()) {
583  return String.valueOf(asLong());
584  } else if (isDouble()) {
585  return String.valueOf(asDouble());
586  } else if (isString()) {
587  return "\"" + asString() + "\"";
588  } else if (isList()) {
589  return asList().toString();
590  } else if (isSet()) {
591  return asSet().toString();
592  } else if (isMap()) {
593  return asMap().toString();
594  } else if (isTime()) {
595  return asTime().toString();
596  } else if (isDate()) {
597  return asDate().toString();
598  } else if (isDateTime()) {
599  return asDateTime().toString();
600  } else if (isVertex()) {
601  return asNode().toString();
602  } else if (isEdge()) {
603  return asRelationship().toString();
604  } else if (isPath()) {
605  return asPath().toString();
606  } else if (isGeography()) {
607  return asGeography().toString();
608  } else if (isDuration()) {
609  return asDuration().toString();
610  }
611  return "Unknown type: " + descType();
612  } catch (UnsupportedEncodingException e) {
613  return e.getMessage();
614  }
615  }
616 }
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.