2020package org .neo4j .gds .core .loading .construction ;
2121
2222import org .eclipse .collections .api .block .function .primitive .ObjectIntToObjectFunction ;
23- import org .jetbrains .annotations .Contract ;
2423import org .jetbrains .annotations .NotNull ;
2524import org .jetbrains .annotations .Nullable ;
2625import org .neo4j .gds .NodeLabel ;
4039public final class NodeLabelTokens {
4140 public static @ NotNull NodeLabelToken of (@ Nullable Object nodeLabels ) {
4241 var nodeLabelToken = ofNullable (nodeLabels );
43- if (nodeLabelToken == null ) {
42+ if (nodeLabelToken . isInvalid () ) {
4443 throw new IllegalArgumentException (formatWithLocale (
4544 "Could not represent a value of type[%s] as nodeLabels: %s" ,
46- nodeLabels .getClass (),
45+ nodeLabels != null ? nodeLabels .getClass () : "null" ,
4746 nodeLabels
4847 ));
4948 }
5049 return nodeLabelToken ;
5150 }
5251
53- public static NodeLabelToken of (TextValue textValue ) {
54- return new Singleton <>(textValue , tv -> NodeLabelTokens .labelOf (tv .stringValue ()));
55- }
56-
57- public static NodeLabelToken of (TextArray textArray ) {
58- return new Sequence <>(textArray , TextArray ::stringValue );
59- }
60-
61- public static @ NotNull NodeLabelToken of (SequenceValue nodeLabels ) {
62- return new Sequence <>(nodeLabels , (s , i ) -> ((TextValue ) s .value (i )).stringValue ());
63- }
64-
65- @ Contract ("null -> !null" )
66- public static @ Nullable NodeLabelToken ofNullable (@ Nullable Object nodeLabels ) {
52+ public static NodeLabelToken ofNullable (@ Nullable Object nodeLabels ) {
6753 if (nodeLabels == null ) {
68- return empty ();
54+ return missing ();
6955 }
7056
7157 if (nodeLabels instanceof NodeLabel []) {
@@ -88,8 +74,28 @@ public static NodeLabelToken of(TextArray textArray) {
8874 return ofSingleton (nodeLabels );
8975 }
9076
77+ public static @ NotNull NodeLabelToken missing () {
78+ return Missing .INSTANCE ;
79+ }
80+
81+ public static @ NotNull NodeLabelToken invalid () {
82+ return Invalid .INSTANCE ;
83+ }
84+
9185 public static @ NotNull NodeLabelToken empty () {
92- return Null .INSTANCE ;
86+ return Empty .INSTANCE ;
87+ }
88+
89+ public static NodeLabelToken of (TextValue textValue ) {
90+ return new Singleton <>(textValue , tv -> NodeLabelTokens .labelOf (tv .stringValue ()));
91+ }
92+
93+ public static NodeLabelToken of (TextArray textArray ) {
94+ return new Sequence <>(textArray , TextArray ::stringValue );
95+ }
96+
97+ public static @ NotNull NodeLabelToken of (SequenceValue nodeLabels ) {
98+ return new Sequence <>(nodeLabels , (s , i ) -> ((TextValue ) s .value (i )).stringValue ());
9399 }
94100
95101 public static @ NotNull NodeLabelToken ofNodeLabels (NodeLabel ... nodeLabels ) {
@@ -104,7 +110,7 @@ public static NodeLabelToken of(TextArray textArray) {
104110 return new Singleton <>(nodeLabel , Function .identity ());
105111 }
106112
107- private static @ Nullable NodeLabelToken ofList (List <?> nodeLabels ) {
113+ private static NodeLabelToken ofList (List <?> nodeLabels ) {
108114 if (nodeLabels .isEmpty ()) {
109115 return empty ();
110116 }
@@ -128,10 +134,10 @@ public static NodeLabelToken of(TextArray textArray) {
128134 return new JavaList <>((List <Label >) nodeLabels , NodeLabelTokens ::labelOf );
129135 }
130136
131- return null ;
137+ return invalid () ;
132138 }
133139
134- private static @ Nullable NodeLabelToken ofSingleton (Object nodeLabel ) {
140+ private static NodeLabelToken ofSingleton (Object nodeLabel ) {
135141 if (nodeLabel instanceof NodeLabel ) {
136142 return new Singleton <>((NodeLabel ) nodeLabel , Function .identity ());
137143 }
@@ -144,7 +150,7 @@ public static NodeLabelToken of(TextArray textArray) {
144150 return new Singleton <>((Label ) nodeLabel , NodeLabelTokens ::labelOf );
145151 }
146152
147- return null ;
153+ return invalid () ;
148154 }
149155
150156 private static NodeLabel labelOf (String label ) {
@@ -155,7 +161,87 @@ private static NodeLabel labelOf(Label label) {
155161 return new NodeLabel (label .name ());
156162 }
157163
158- private enum Null implements NodeLabelToken {
164+ private enum Missing implements NodeLabelToken {
165+ INSTANCE ;
166+
167+ @ Override
168+ public boolean isMissing () {
169+ return true ;
170+ }
171+
172+ @ Override
173+ public boolean isEmpty () {
174+ return true ;
175+ }
176+
177+ @ Override
178+ public boolean isInvalid () {
179+ return false ;
180+ }
181+
182+ @ Override
183+ public int size () {
184+ return 0 ;
185+ }
186+
187+ @ Override
188+ public @ NotNull NodeLabel get (int index ) {
189+ throw new NoSuchElementException ();
190+ }
191+
192+ @ Override
193+ public String [] getStrings () {
194+ return new String [0 ];
195+ }
196+ }
197+
198+ private enum Invalid implements NodeLabelToken {
199+ INSTANCE ;
200+
201+ @ Override
202+ public boolean isMissing () {
203+ return false ;
204+ }
205+
206+ @ Override
207+ public boolean isEmpty () {
208+ return true ;
209+ }
210+
211+ @ Override
212+ public boolean isInvalid () {
213+ return true ;
214+ }
215+
216+ @ Override
217+ public int size () {
218+ return 0 ;
219+ }
220+
221+ @ Override
222+ public @ NotNull NodeLabel get (int index ) {
223+ throw new NoSuchElementException ();
224+ }
225+
226+ @ Override
227+ public String [] getStrings () {
228+ return new String [0 ];
229+ }
230+ }
231+
232+ private interface ValidNodeLabelToken extends NodeLabelToken {
233+ @ Override
234+ default boolean isMissing () {
235+ return false ;
236+ }
237+
238+ @ Override
239+ default boolean isInvalid () {
240+ return false ;
241+ }
242+ }
243+
244+ private enum Empty implements ValidNodeLabelToken {
159245 INSTANCE ;
160246
161247 @ Override
@@ -179,7 +265,7 @@ public String[] getStrings() {
179265 }
180266 }
181267
182- private static final class Array <T > implements NodeLabelToken {
268+ private static final class Array <T > implements ValidNodeLabelToken {
183269 private final @ NotNull T [] nodeLabels ;
184270
185271 private final Function <T , NodeLabel > toNodeLabel ;
@@ -223,7 +309,7 @@ public int hashCode() {
223309 }
224310 }
225311
226- private static final class JavaList <T > implements NodeLabelToken {
312+ private static final class JavaList <T > implements ValidNodeLabelToken {
227313 private final @ NotNull List <T > nodeLabels ;
228314
229315 private final Function <T , NodeLabel > toNodeLabel ;
@@ -267,7 +353,7 @@ public int hashCode() {
267353 }
268354 }
269355
270- private static final class Sequence <T extends SequenceValue > implements NodeLabelToken {
356+ private static final class Sequence <T extends SequenceValue > implements ValidNodeLabelToken {
271357 private final T sequence ;
272358 private final ObjectIntToObjectFunction <T , String > toString ;
273359
@@ -312,7 +398,7 @@ public int hashCode() {
312398 }
313399 }
314400
315- private static final class Singleton <T > implements NodeLabelToken {
401+ private static final class Singleton <T > implements ValidNodeLabelToken {
316402
317403 private final NodeLabel item ;
318404
0 commit comments