1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.metapath;
7   
8   import gov.nist.secauto.metaschema.core.datatype.adapter.DecimalAdapter;
9   import gov.nist.secauto.metaschema.core.metapath.item.ICollectionValue;
10  import gov.nist.secauto.metaschema.core.metapath.item.IItem;
11  import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
12  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
13  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem;
14  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBase64BinaryItem;
15  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
16  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem;
17  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem;
18  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDayTimeDurationItem;
19  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDecimalItem;
20  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDurationItem;
21  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem;
22  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IQNameItem;
23  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
24  import gov.nist.secauto.metaschema.core.metapath.item.atomic.ITimeItem;
25  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUuidItem;
26  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
27  import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
28  import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
29  import gov.nist.secauto.metaschema.core.metapath.item.function.IMapKey;
30  import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
31  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
32  
33  import java.math.BigDecimal;
34  import java.math.BigInteger;
35  import java.util.Map;
36  
37  import edu.umd.cs.findbugs.annotations.NonNull;
38  
39  public final class TestUtils {
40    /**
41     * Create an empty sequence.
42     *
43     * @param <T>
44     *          the item Java type contained within the sequence
45     * @return the sequence
46     */
47    @NonNull
48    public static <T extends IItem> ISequence<T> sequence() {
49      return ISequence.of();
50    }
51  
52    /**
53     * Create a sequence containing the provided items.
54     *
55     * @param <T>
56     *          the item Java type contained within the sequence
57     * @param items
58     *          the items to add to the sequence
59     * @return the sequence
60     */
61    @SafeVarargs
62    @NonNull
63    public static <T extends IItem> ISequence<T> sequence(@NonNull T... items) {
64      return ISequence.of(items);
65    }
66  
67    /**
68     * Create an empty array item.
69     *
70     * @param <T>
71     *          the item Java type contained within the array item
72     * @return the array item
73     */
74    @NonNull
75    public static <T extends ICollectionValue> IArrayItem<T> array() {
76      return IArrayItem.of();
77    }
78  
79    /**
80     * Create a array item containing the provided items.
81     *
82     * @param <T>
83     *          the item Java type contained within the array item
84     * @param items
85     *          the items to add to the array item
86     * @return the array item
87     */
88    @SafeVarargs
89    @NonNull
90    public static <T extends ICollectionValue> IArrayItem<T> array(@NonNull T... items) {
91      return IArrayItem.of(items);
92    }
93  
94    /**
95     * Create a map item containing the provided entries.
96     *
97     * @param <T>
98     *          the entry value Java type contained within the map item
99     * @param entries
100    *          the entries to add to the map item
101    * @return the map item
102    */
103   @SafeVarargs
104   @NonNull
105   public static <T extends ICollectionValue> IMapItem<T> map(@NonNull Map.Entry<IMapKey, T>... entries) {
106     return IMapItem.ofEntries(entries);
107   }
108 
109   /**
110    * Create a map entry using the provided key and value.
111    *
112    * @param <T>
113    *          the entry value Java type
114    * @param key
115    *          the entry key
116    * @param value
117    *          the entry value
118    * @return the map entry
119    */
120   @NonNull
121   public static <T extends ICollectionValue> Map.Entry<IMapKey, T> entry(
122       @NonNull IAnyAtomicItem key,
123       @NonNull T value) {
124     return IMapItem.entry(key, value);
125   }
126 
127   /**
128    * Create a base64 item using the provided value.
129    *
130    * @param value
131    *          the value
132    * @return the boolean item
133    */
134   @NonNull
135   public static IBase64BinaryItem base64(@NonNull String value) {
136     return IBase64BinaryItem.valueOf(value);
137   }
138 
139   /**
140    * Create a boolean item using the provided value.
141    *
142    * @param value
143    *          the boolean value
144    * @return the boolean item
145    */
146   @NonNull
147   public static IBooleanItem bool(boolean value) {
148     return IBooleanItem.valueOf(value);
149   }
150 
151   /**
152    * Create a decimal item using the provided value.
153    *
154    * @param value
155    *          the decimal value
156    * @return the decimal item
157    */
158   public static IDecimalItem decimal(@NonNull String value) {
159     return IDecimalItem.valueOf(new BigDecimal(value, DecimalAdapter.mathContext()));
160   }
161 
162   /**
163    * Create a decimal item using the provided value.
164    *
165    * @param value
166    *          the decimal value
167    * @return the decimal item
168    */
169   @NonNull
170   public static IDecimalItem decimal(int value) {
171     return IDecimalItem.valueOf(value);
172   }
173 
174   /**
175    * Create a decimal item using the provided value.
176    *
177    * @param value
178    *          the decimal value
179    * @return the decimal item
180    */
181   @NonNull
182   public static IDecimalItem decimal(double value) {
183     return IDecimalItem.valueOf(value);
184   }
185 
186   /**
187    * Create an integer item using the provided value.
188    *
189    * @param value
190    *          the integer value
191    * @return the integer item
192    */
193   @NonNull
194   public static IIntegerItem integer(int value) {
195     return IIntegerItem.valueOf(ObjectUtils.notNull(BigInteger.valueOf(value)));
196   }
197 
198   /**
199    * Create a string item using the provided value.
200    *
201    * @param value
202    *          the string value
203    * @return the string item
204    */
205   @NonNull
206   public static IStringItem string(@NonNull String value) {
207     return IStringItem.valueOf(value);
208   }
209 
210   /**
211    * Create a uuid item using the provided value.
212    *
213    * @param value
214    *          the uuid value
215    * @return the uuid item
216    */
217   @NonNull
218   public static IUuidItem uuid(@NonNull String value) {
219     return IUuidItem.valueOf(value);
220   }
221 
222   /**
223    * Create a uri item using the provided value.
224    *
225    * @param value
226    *          the uri value
227    * @return the uri item
228    */
229   @NonNull
230   public static IAnyUriItem uri(@NonNull String value) {
231     return IAnyUriItem.valueOf(value);
232   }
233 
234   /**
235    * Create a date item using the provided value.
236    *
237    * @param value
238    *          the date value
239    * @return the date item
240    */
241   @NonNull
242   public static IDateItem date(@NonNull String value) {
243     return IDateItem.valueOf(value);
244   }
245 
246   /**
247    * Create a date/time item using the provided value.
248    *
249    * @param value
250    *          the date/time value
251    * @return the date/time item
252    */
253   @NonNull
254   public static IDateTimeItem dateTime(@NonNull String value) {
255     return IDateTimeItem.valueOf(value);
256   }
257 
258   /**
259    * Create a time item using the provided value.
260    *
261    * @param value
262    *          the time value
263    * @return the time item
264    */
265   @NonNull
266   public static ITimeItem time(@NonNull String value) {
267     return ITimeItem.valueOf(value);
268   }
269 
270   /**
271    * Create a duration item using the provided value indicating the years, months,
272    * and days of the duration.
273    *
274    * @param value
275    *          the duration value
276    * @return the duration item
277    */
278   @NonNull
279   public static IDurationItem duration(@NonNull String value) {
280     return IDurationItem.valueOf(value);
281   }
282 
283   /**
284    * Create a duration item using the provided value indicating the years, months,
285    * and days of the duration.
286    *
287    * @param value
288    *          the duration value
289    * @return the duration item
290    */
291   @NonNull
292   public static IYearMonthDurationItem yearMonthDuration(@NonNull String value) {
293     return IYearMonthDurationItem.valueOf(value);
294   }
295 
296   /**
297    * Create a duration item using the provided value indicating the seconds and
298    * nanoseconds of the duration.
299    *
300    * @param value
301    *          the duration value
302    * @return the duration item
303    */
304   @NonNull
305   public static IDayTimeDurationItem dayTimeDuration(@NonNull String value) {
306     return IDayTimeDurationItem.valueOf(value);
307   }
308 
309   @NonNull
310   public static IEnhancedQName eqname(@NonNull String namespace, @NonNull String localname) {
311     return IEnhancedQName.of(namespace, localname);
312   }
313 
314   @NonNull
315   public static IQNameItem qname(@NonNull String namespace, @NonNull String localname) {
316     return IQNameItem.valueOf(eqname(namespace, localname));
317   }
318 
319   private TestUtils() {
320     // disable construction
321   }
322 }