001/*
002 * Copyright 2023 the original author or authors.
003 * <p>
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 * <p>
008 * https://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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.valueobjects.generator.dynamic.impl;
017
018import java.lang.reflect.Array;
019import java.util.List;
020import java.util.Optional;
021
022import de.cuioss.test.generator.TypedGenerator;
023import de.cuioss.test.generator.impl.CollectionGenerator;
024import de.cuioss.test.generator.impl.PrimitiveArrayGenerators;
025import de.cuioss.test.valueobjects.generator.dynamic.GeneratorResolver;
026import lombok.Getter;
027import lombok.NonNull;
028import lombok.RequiredArgsConstructor;
029
030/**
031 * Generator for different types of arrays.
032 *
033 * @author Oliver Wolff
034 * @param <T> identifying the concrete Array-type
035 * @param <C> identifying the componentType of the array
036 */
037@RequiredArgsConstructor
038public class ArraysGenerator<T, C> implements TypedGenerator<T> {
039
040    @NonNull
041    @Getter
042    private final Class<T> type;
043
044    @NonNull
045    private final Class<C> componentType;
046
047    @NonNull
048    private final CollectionGenerator<?> collectionGenerator;
049
050    @SuppressWarnings("unchecked")
051    @Override
052    public T next() {
053        final var collection = (List<C>) collectionGenerator.list();
054        if (!componentType.isPrimitive()) {
055            final var array = (C[]) Array.newInstance(componentType, collection.size());
056            return (T) collection.toArray(array);
057        }
058        return (T) PrimitiveArrayGenerators.resolveForType(componentType).next();
059    }
060
061    /**
062     * Factory method for creating an instance of {@link ArraysGenerator}.
063     *
064     * @param type to be, must not be an array type: {@link Class#isArray()}
065     * @return an {@link Optional} on the corresponding {@link ArraysGenerator} if
066     *         the requirements are met, {@link Optional#empty()} otherwise
067     */
068    public static final <T> Optional<TypedGenerator<T>> getGeneratorForType(final Class<T> type) {
069        if (null == type || !type.isArray()) {
070            return Optional.empty();
071        }
072        final Class<?> componentType = type.getComponentType();
073
074        final TypedGenerator<?> wrappedGenerator = GeneratorResolver.resolveGenerator(componentType);
075
076        return Optional.of(new ArraysGenerator<>(type, componentType, new CollectionGenerator<>(wrappedGenerator)));
077    }
078}