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.Generator; 019 020import java.lang.reflect.Array; 021import java.util.List; 022import java.util.Objects; 023 024public class ArrayGenerator<T> extends AbstractTransformerGenerator<List<T>, T[]> { 025 026 public static final int MAX_SIZE = ListGenerator.MAX_SIZE; 027 public static final int MIN_SIZE = ListGenerator.MIN_SIZE; 028 029 private T[] emptyArrayOfT; 030 031 public ArrayGenerator(Generator<? extends T> content, Class<T> type) { 032 super(new ListGenerator<>(content)); 033 setEmptyArrayOfT(type); 034 } 035 036 public ArrayGenerator(Generator<? extends T> content, Generator<Integer> size, Class<T> type) { 037 super(new ListGenerator<>(content, size)); 038 setEmptyArrayOfT(type); 039 } 040 041 private void setEmptyArrayOfT(Class<T> type) { 042 Objects.requireNonNull(type, "content type"); 043 this.emptyArrayOfT = emptyArrayOfT(type); 044 } 045 046 @Override 047 protected T[] transform(Generator<List<T>> inputGenerator) { 048 return inputGenerator.next().toArray(emptyArrayOfT); 049 } 050 051 @SuppressWarnings("unchecked") 052 private T[] emptyArrayOfT(Class<T> type) { 053 return (T[]) Array.newInstance(type, 0); 054 } 055}