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.benerator.primitive;
28  
29  import com.rapiddweller.benerator.GeneratorContext;
30  import com.rapiddweller.benerator.distribution.Distribution;
31  import com.rapiddweller.benerator.wrapper.LengthGenerator;
32  import com.rapiddweller.benerator.wrapper.ProductWrapper;
33  import com.rapiddweller.format.regex.RegexParser;
34  
35  import java.util.Locale;
36  import java.util.Set;
37  
38  /**
39   * Generates {@link String}s composed of numerical digits.<br/><br/>
40   * Created: 16.10.2009 07:31:16
41   *
42   * @author Volker Bergmann
43   * @since 0.6.0
44   */
45  public class RandomVarLengthStringGenerator extends LengthGenerator<Character, String>
46      implements VarLengthStringGenerator {
47  
48    private String pattern;
49    private Locale locale;
50    private Set<Character> chars;
51  
52    /**
53     * Instantiates a new Random var length string generator.
54     */
55    public RandomVarLengthStringGenerator() {
56      this("[0-9]", 8);
57    }
58  
59    /**
60     * Instantiates a new Random var length string generator.
61     *
62     * @param pattern the pattern
63     * @param length  the length
64     */
65    public RandomVarLengthStringGenerator(String pattern, int length) {
66      this(pattern, length, length, 1);
67    }
68  
69    /**
70     * Instantiates a new Random var length string generator.
71     *
72     * @param pattern           the pattern
73     * @param minLength         the min length
74     * @param maxLength         the max length
75     * @param lengthGranularity the length granularity
76     */
77    public RandomVarLengthStringGenerator(String pattern, int minLength, int maxLength, int lengthGranularity) {
78      this(pattern, minLength, maxLength, lengthGranularity, null);
79    }
80  
81    /**
82     * Instantiates a new Random var length string generator.
83     *
84     * @param pattern            the pattern
85     * @param minLength          the min length
86     * @param maxLength          the max length
87     * @param lengthGranularity  the length granularity
88     * @param lengthDistribution the length distribution
89     */
90    public RandomVarLengthStringGenerator(String pattern, int minLength, int maxLength,
91                                          int lengthGranularity, Distribution lengthDistribution) {
92      super(null, true, minLength, maxLength, lengthGranularity, lengthDistribution);
93      this.pattern = pattern;
94    }
95  
96    /**
97     * Instantiates a new Random var length string generator.
98     *
99     * @param chars              the chars
100    * @param minLength          the min length
101    * @param maxLength          the max length
102    * @param lengthGranularity  the length granularity
103    * @param lengthDistribution the length distribution
104    */
105   public RandomVarLengthStringGenerator(Set<Character> chars, int minLength, int maxLength,
106                                         int lengthGranularity, Distribution lengthDistribution) {
107     super(null, true, minLength, maxLength, lengthGranularity, lengthDistribution);
108     this.chars = chars;
109   }
110 
111 
112   // properties ------------------------------------------------------------------------------------------------------
113 
114   /**
115    * Gets pattern.
116    *
117    * @return the pattern
118    */
119   public String getPattern() {
120     return pattern;
121   }
122 
123   /**
124    * Sets pattern.
125    *
126    * @param charSet the char set
127    */
128   public void setPattern(String charSet) {
129     this.pattern = charSet;
130     this.chars = null;
131   }
132 
133   /**
134    * Gets locale.
135    *
136    * @return the locale
137    */
138   public Locale getLocale() {
139     return locale;
140   }
141 
142   /**
143    * Sets locale.
144    *
145    * @param locale the locale
146    */
147   public void setLocale(Locale locale) {
148     this.locale = locale;
149   }
150 
151   /**
152    * Gets chars.
153    *
154    * @return the chars
155    */
156   public Set<Character> getChars() {
157     return chars;
158   }
159 
160   /**
161    * Sets chars.
162    *
163    * @param chars the chars
164    */
165   public void setChars(Set<Character> chars) {
166     this.chars = chars;
167     this.pattern = null;
168   }
169 
170 
171   // Generator interface implementation ------------------------------------------------------------------------------
172 
173   @Override
174   public Class<String> getGeneratedType() {
175     return String.class;
176   }
177 
178   @Override
179   public boolean isThreadSafe() {
180     return true;
181   }
182 
183   @Override
184   public boolean isParallelizable() {
185     return true;
186   }
187 
188   @Override
189   public void init(GeneratorContext context) {
190     if (pattern != null) {
191       this.chars = new RegexParser(locale).parseSingleChar(pattern).getCharSet().getSet();
192     }
193     setSource(new CharacterGenerator(chars));
194     super.init(context);
195   }
196 
197   @Override
198   public ProductWrapper<String> generate(ProductWrapper<String> wrapper) {
199     return wrapper.wrap(generate());
200   }
201 
202   @Override
203   public String generate() {
204     return generateWithLength(generateCardinal());
205   }
206 
207   @Override
208   public String generateWithLength(int length) {
209     StringBuilder builder = new StringBuilder();
210     for (int i = 0; i < length; i++) {
211       builder.append(generateFromSource().unwrap());
212     }
213     return builder.toString();
214   }
215 
216   @Override
217   public String toString() {
218     return getClass().getSimpleName() + "[chars=" + chars + ", minLength=" + getMinLength() + ", " +
219         "maxLength=" + getMaxLength() + ", lengthGranularity=" + getLengthGranularity() + ", " +
220         "lengthDistribution=" + getLengthDistribution() + "]";
221   }
222 
223 }