1
1
package org .iot .dsa .node ;
2
2
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
3
6
/**
4
7
* The primitive types of the SDK.
5
8
*
6
9
* @author Aaron Hansen
7
10
*/
8
- public enum DSElementType {
11
+ public enum DSElementType implements DSIEnum , DSIValue {
9
12
10
13
BOOLEAN ,
11
14
BYTES ,
@@ -16,13 +19,76 @@ public enum DSElementType {
16
19
NULL ,
17
20
STRING ;
18
21
19
- private String display ;
22
+ private static Map <String , DSElementType > nameMap ;
23
+ private DSString display ;
20
24
21
- public String toString () {
25
+ @ Override
26
+ public DSIObject copy () {
27
+ return this ;
28
+ }
29
+
30
+ @ Override
31
+ public DSList getEnums (DSList bucket ) {
32
+ if (bucket == null ) {
33
+ bucket = new DSList ();
34
+ }
35
+ for (DSElementType e : values ()) {
36
+ bucket .add (e .toElement ());
37
+ }
38
+ return bucket ;
39
+ }
40
+
41
+ @ Override
42
+ public DSValueType getValueType () {
43
+ return DSValueType .ENUM ;
44
+ }
45
+
46
+ @ Override
47
+ public boolean isNull () {
48
+ return false ;
49
+ }
50
+
51
+ /**
52
+ * Returns a DSString representation of the name() in lowercase.
53
+ */
54
+ @ Override
55
+ public DSElement toElement () {
22
56
if (display == null ) {
23
- display = name ().toLowerCase ();
57
+ display = DSString . valueOf ( name ().toLowerCase () );
24
58
}
25
59
return display ;
26
60
}
27
61
62
+ public String toString () {
63
+ return toElement ().toString ();
64
+ }
65
+
66
+ /**
67
+ * Unlike Enum.valueOf, this will handle the lowercase display.
68
+ */
69
+ public static DSElementType valueFor (String type ) {
70
+ DSElementType ret = nameMap .get (type );
71
+ return ret == null ? NULL : ret ;
72
+ }
73
+
74
+ @ Override
75
+ public DSElementType valueOf (DSElement element ) {
76
+ DSElementType ret = nameMap .get (element .toString ());
77
+ return ret == null ? NULL : ret ;
78
+ }
79
+
80
+ static {
81
+ DSRegistry .registerDecoder (DSElementType .class , BOOLEAN );
82
+ nameMap = new HashMap <>();
83
+ for (DSElementType e : values ()) {
84
+ nameMap .put (e .name (), e );
85
+ nameMap .put (e .toString (), e );
86
+ }
87
+ //DSA types
88
+ nameMap .put ("bool" , BOOLEAN );
89
+ nameMap .put ("number" , DOUBLE );
90
+ nameMap .put ("array" , LIST );
91
+ nameMap .put ("binary" , BYTES );
92
+ nameMap .put ("dynamic" , NULL );
93
+ }
28
94
}
0 commit comments