View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.model.data;
28  
29  import com.rapiddweller.common.operation.AndOperation;
30  import com.rapiddweller.common.operation.MaxNumberStringOperation;
31  import com.rapiddweller.common.operation.MaxOperation;
32  import com.rapiddweller.common.operation.MinNumberStringOperation;
33  import com.rapiddweller.common.operation.MinOperation;
34  import com.rapiddweller.script.PrimitiveType;
35  
36  /**
37   * Describes a simple type.<br/>
38   * <br/>
39   * Created: 03.03.2008 08:58:58
40   *
41   * @author Volker Bergmann
42   * @since 0.5.0
43   */
44  public class SimpleTypeDescriptor extends TypeDescriptor {
45  
46    /**
47     * The constant MIN.
48     */
49    public static final String MIN = "min";
50    /**
51     * The constant MAX.
52     */
53    public static final String MAX = "max";
54    /**
55     * The constant MIN_INCLUSIVE.
56     */
57    public static final String MIN_INCLUSIVE = "minInclusive";
58    /**
59     * The constant MAX_INCLUSIVE.
60     */
61    public static final String MAX_INCLUSIVE = "maxInclusive";
62  
63    /**
64     * The constant GRANULARITY.
65     */
66    public static final String GRANULARITY = "granularity";
67  
68    /**
69     * The constant TRUE_QUOTA.
70     */
71    public static final String TRUE_QUOTA = "trueQuota";
72    /**
73     * The constant MIN_LENGTH.
74     */
75    public static final String MIN_LENGTH = "minLength";
76    /**
77     * The constant MAX_LENGTH.
78     */
79    public static final String MAX_LENGTH = "maxLength";
80    /**
81     * The constant LENGTH_DISTRIBUTION.
82     */
83    public static final String LENGTH_DISTRIBUTION = "lengthDistribution";
84  
85    /**
86     * The constant CONSTANT.
87     */
88    public static final String CONSTANT = "constant";
89    /**
90     * The constant VALUES.
91     */
92    public static final String VALUES = "values";
93    /**
94     * The constant MAP.
95     */
96    public static final String MAP = "map";
97  
98    private PrimitiveType primitiveType = null;
99  
100   /**
101    * Instantiates a new Simple type descriptor.
102    *
103    * @param name     the name
104    * @param provider the provider
105    */
106   public SimpleTypeDescriptor(String name, DescriptorProvider provider) {
107     this(name, provider, (String) null);
108   }
109 
110   /**
111    * Instantiates a new Simple type descriptor.
112    *
113    * @param name     the name
114    * @param provider the provider
115    * @param parent   the parent
116    */
117   public SimpleTypeDescriptor(String name, DescriptorProvider provider,
118                               SimpleTypeDescriptor parent) {
119     this(name, provider, parent.getName());
120     this.parent = parent;
121   }
122 
123   /**
124    * Instantiates a new Simple type descriptor.
125    *
126    * @param name       the name
127    * @param provider   the provider
128    * @param parentName the parent name
129    */
130   public SimpleTypeDescriptor(String name, DescriptorProvider provider,
131                               String parentName) {
132     super(name, provider, parentName);
133     // number setup
134     addConstraint(MIN, String.class, new MaxNumberStringOperation());
135     addConstraint(MAX, String.class, new MinNumberStringOperation());
136     addConstraint(MIN_INCLUSIVE, Boolean.class, new AndOperation());
137     addConstraint(MAX_INCLUSIVE, Boolean.class, new AndOperation());
138     addConfig(GRANULARITY, String.class);
139     // boolean setup
140     addConfig(TRUE_QUOTA, Double.class);
141     // string setup
142     addConstraint(MIN_LENGTH, Integer.class, new MaxOperation<>());
143     addConstraint(MAX_LENGTH, Integer.class, new MinOperation<>());
144     addConfig(LENGTH_DISTRIBUTION, String.class);
145     // other config
146     addConfig(VALUES, String.class);
147     addConfig(CONSTANT, String.class);
148     addConfig(MAP, String.class);
149   }
150 
151   // properties ------------------------------------------------------------------------------------------------------
152 
153   @Override
154   public SimpleTypeDescriptor getParent() {
155     return (SimpleTypeDescriptor) super.getParent();
156   }
157 
158   /**
159    * Gets primitive type.
160    *
161    * @return the primitive type
162    */
163   public PrimitiveType getPrimitiveType() {
164     if (primitiveType != null) {
165       return primitiveType;
166     }
167     primitiveType = PrimitiveType.getInstance(getName());
168     if (primitiveType != null) {
169       return primitiveType;
170     }
171     if (getParent() != null) {
172       return getParent().getPrimitiveType();
173     }
174     return null;
175   }
176 
177   /**
178    * Gets min.
179    *
180    * @return the min
181    */
182   public String getMin() {
183     return (String) getDetailValue(MIN);
184   }
185 
186   /**
187    * Sets min.
188    *
189    * @param min the min
190    */
191   public void setMin(String min) {
192     setDetailValue(MIN, min);
193   }
194 
195   /**
196    * Is min inclusive boolean.
197    *
198    * @return the boolean
199    */
200   public Boolean isMinInclusive() {
201     return (Boolean) getDetailValue(MIN_INCLUSIVE);
202   }
203 
204   /**
205    * Sets min inclusive.
206    *
207    * @param minInclusive the min inclusive
208    */
209   public void setMinInclusive(Boolean minInclusive) {
210     setDetailValue(MIN, minInclusive);
211   }
212 
213   /**
214    * Gets max.
215    *
216    * @return the max
217    */
218   public String getMax() {
219     return (String) getDetailValue(MAX);
220   }
221 
222   /**
223    * Sets max.
224    *
225    * @param max the max
226    */
227   public void setMax(String max) {
228     setDetailValue(MAX, max);
229   }
230 
231   /**
232    * Is max inclusive boolean.
233    *
234    * @return the boolean
235    */
236   public Boolean isMaxInclusive() {
237     return (Boolean) getDetailValue(MAX_INCLUSIVE);
238   }
239 
240   /**
241    * Sets max inclusive.
242    *
243    * @param maxInclusive the max inclusive
244    */
245   public void setMaxInclusive(Boolean maxInclusive) {
246     setDetailValue(MAX_INCLUSIVE, maxInclusive);
247   }
248 
249   /**
250    * Gets granularity.
251    *
252    * @return the granularity
253    */
254   public String getGranularity() {
255     return (String) getDetailValue(GRANULARITY);
256   }
257 
258   /**
259    * Sets granularity.
260    *
261    * @param granularity the granularity
262    */
263   public void setGranularity(String granularity) {
264     setDetailValue(GRANULARITY, granularity);
265   }
266 
267   /**
268    * Gets true quota.
269    *
270    * @return the true quota
271    */
272   public Double getTrueQuota() {
273     return (Double) getDetailValue(TRUE_QUOTA);
274   }
275 
276   /**
277    * Sets true quota.
278    *
279    * @param trueQuota the true quota
280    */
281   public void setTrueQuota(Double trueQuota) {
282     setDetailValue(TRUE_QUOTA, trueQuota);
283   }
284 
285   /**
286    * Gets min length.
287    *
288    * @return the min length
289    */
290   public Integer getMinLength() {
291     return (Integer) getDetailValue(MIN_LENGTH);
292   }
293 
294   /**
295    * Sets min length.
296    *
297    * @param minLength the min length
298    */
299   public void setMinLength(Integer minLength) {
300     setDetailValue(MIN_LENGTH, minLength);
301   }
302 
303   /**
304    * Gets max length.
305    *
306    * @return the max length
307    */
308   public Integer getMaxLength() {
309     return (Integer) getDetailValue(MAX_LENGTH);
310   }
311 
312   /**
313    * Sets max length.
314    *
315    * @param maxLength the max length
316    */
317   public void setMaxLength(Integer maxLength) {
318     setDetailValue(MAX_LENGTH, maxLength);
319   }
320 
321   /**
322    * Gets length distribution.
323    *
324    * @return the length distribution
325    */
326   public String getLengthDistribution() {
327     return (String) getDetailValue(LENGTH_DISTRIBUTION);
328   }
329 
330   /**
331    * Sets length distribution.
332    *
333    * @param lengthDistribution the length distribution
334    */
335   public void setLengthDistribution(String lengthDistribution) {
336     setDetailValue(LENGTH_DISTRIBUTION, lengthDistribution);
337   }
338 
339   /**
340    * Gets values.
341    *
342    * @return the values
343    */
344   public String getValues() {
345     return (String) getDetailValue(VALUES);
346   }
347 
348   /**
349    * Sets values.
350    *
351    * @param values the values
352    */
353   public void setValues(String values) {
354     setDetailValue(VALUES, values);
355   }
356 
357   /**
358    * Add value.
359    *
360    * @param value the value
361    */
362   public void addValue(String value) {
363     String valuesBefore = getValues();
364     if (valuesBefore == null || valuesBefore.length() == 0) {
365       setValues(value);
366     } else {
367       setValues(valuesBefore + ',' + value);
368     }
369   }
370 
371   /**
372    * Gets constant.
373    *
374    * @return the constant
375    */
376   public String getConstant() {
377     return (String) getDetailValue(CONSTANT);
378   }
379 
380   /**
381    * Sets constant.
382    *
383    * @param constant the constant
384    */
385   public void setConstant(String constant) {
386     setDetailValue(CONSTANT, constant);
387   }
388 
389   /**
390    * Gets map.
391    *
392    * @return the map
393    */
394   public String getMap() {
395     return (String) getDetailValue(MAP);
396   }
397 
398   /**
399    * Sets map.
400    *
401    * @param map the map
402    */
403   public void setMap(String map) {
404     setDetailValue(MAP, map);
405   }
406 
407   // literate build helpers ------------------------------------------------------------------------------------------
408 
409   /**
410    * With min simple type descriptor.
411    *
412    * @param min the min
413    * @return the simple type descriptor
414    */
415   public SimpleTypeDescriptor withMin(String min) {
416     setMin(min);
417     return this;
418   }
419 
420   /**
421    * With max simple type descriptor.
422    *
423    * @param max the max
424    * @return the simple type descriptor
425    */
426   public SimpleTypeDescriptor withMax(String max) {
427     setMax(max);
428     return this;
429   }
430 
431   /**
432    * With granularity simple type descriptor.
433    *
434    * @param granularity the granularity
435    * @return the simple type descriptor
436    */
437   public SimpleTypeDescriptor withGranularity(String granularity) {
438     setGranularity(granularity);
439     return this;
440   }
441 
442   /**
443    * With pattern simple type descriptor.
444    *
445    * @param pattern the pattern
446    * @return the simple type descriptor
447    */
448   public SimpleTypeDescriptor withPattern(String pattern) {
449     setPattern(pattern);
450     return this;
451   }
452 
453   /**
454    * With distribution simple type descriptor.
455    *
456    * @param distribution the distribution
457    * @return the simple type descriptor
458    */
459   public SimpleTypeDescriptor withDistribution(String distribution) {
460     setDistribution(distribution);
461     return this;
462   }
463 
464   /**
465    * With dataset simple type descriptor.
466    *
467    * @param dataset the dataset
468    * @return the simple type descriptor
469    */
470   public SimpleTypeDescriptor withDataset(String dataset) {
471     setDataset(dataset);
472     return this;
473   }
474 
475   /**
476    * With locale id simple type descriptor.
477    *
478    * @param localeId the locale id
479    * @return the simple type descriptor
480    */
481   public SimpleTypeDescriptor withLocaleId(String localeId) {
482     setLocaleId(localeId);
483     return this;
484   }
485 
486   /**
487    * With true quota simple type descriptor.
488    *
489    * @param trueQuota the true quota
490    * @return the simple type descriptor
491    */
492   public SimpleTypeDescriptor withTrueQuota(Double trueQuota) {
493     setTrueQuota(trueQuota);
494     return this;
495   }
496 
497   /**
498    * With uri simple type descriptor.
499    *
500    * @param source the source
501    * @return the simple type descriptor
502    */
503   public SimpleTypeDescriptor withUri(String source) {
504     setSource(source);
505     return this;
506   }
507 
508   /**
509    * With values simple type descriptor.
510    *
511    * @param values the values
512    * @return the simple type descriptor
513    */
514   public SimpleTypeDescriptor withValues(String values) {
515     this.setValues(values);
516     return this;
517   }
518 
519   // generic property access -----------------------------------------------------------------------------------------
520 
521 /*
522     public void setDetail(String detailName, Object detailValue) {
523         Class<?> targetType = getDetailType(detailName);
524         if (targetType == Distribution.class && detailValue.getClass() == String.class)
525             detailValue = mapDistribution((String) detailValue);
526         else if (targetType == Converter.class && detailValue.getClass() == String.class)
527             detailValue = mapConverter((String) detailValue);
528         super.setDetailValue(detailName, detailValue);
529     }
530 */
531 
532 // private helpers -------------------------------------------------------------------------------------------------
533 /*
534     private Converter<?, ?> mapConverter(String converterString) {
535         Object result = BeanUtil.newInstance(converterString);
536         if (result instanceof Format)
537             result = new ParseFormatConverter(Object.class, (Format) result);
538         else if (!(result instanceof Converter))
539             throw new ConfigurationError("Class is no Converter: " + result.getClass());
540         return (Converter<?, ?>) result;
541     }
542 
543     private static Distribution mapDistribution(String distributionName) {
544         if (distributionName == null)
545             return null;
546         try {
547             return Sequence.getInstance(distributionName);
548         } catch (Exception e) {
549             return (Distribution) BeanUtil.newInstance(distributionName);
550         }
551     }
552 */
553 
554 }