001/* 002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.test.generator.internal.net.java.quickcheck.generator.support; 017 018import de.cuioss.test.generator.internal.net.java.quickcheck.ExtendibleGenerator; 019import de.cuioss.test.generator.internal.net.java.quickcheck.FrequencyGenerator; 020import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 021 022import java.util.Objects; 023 024public class StringGenerator implements ExtendibleGenerator<Character, String> { 025 026 public static final int MIN_LENGTH = 0; 027 028 public static final int MAX_LENGTH = 30; 029 030 private final FrequencyGenerator<Character> characters; 031 private final Generator<Integer> length; 032 033 public StringGenerator() { 034 this(new CharacterGenerator()); 035 } 036 037 public StringGenerator(char first, char last) { 038 this(new CharacterGenerator(first, last)); 039 } 040 041 public StringGenerator(Generator<Character> characters) { 042 this(new IntegerGenerator(MIN_LENGTH, MAX_LENGTH), characters); 043 } 044 045 public StringGenerator(Generator<Integer> length, Generator<Character> characters) { 046 Objects.requireNonNull(length, "length"); 047 048 this.length = length; 049 this.characters = new DefaultFrequencyGenerator<>(characters); 050 } 051 052 @Override 053 public String next() { 054 int size = length.next(); 055 StringBuilder builder = new StringBuilder(); 056 for (int count = 0; count < size; count++) { 057 builder.append(characters.next()); 058 } 059 return builder.toString(); 060 } 061 062 @Override 063 public ExtendibleGenerator<Character, String> add(Generator<Character> characterGenerator) { 064 characters.add(characterGenerator); 065 return this; 066 } 067}