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.objects.impl;
017
018import java.util.List;
019
020import de.cuioss.test.valueobjects.objects.ConfigurationCallBackHandler;
021import de.cuioss.test.valueobjects.objects.ParameterizedInstantiator;
022import de.cuioss.test.valueobjects.objects.RuntimeProperties;
023import de.cuioss.test.valueobjects.property.PropertyMetadata;
024import de.cuioss.test.valueobjects.property.PropertySupport;
025import lombok.RequiredArgsConstructor;
026import lombok.ToString;
027
028/**
029 * Delegate to {@link ParameterizedInstantiator} that calls
030 * {@link ConfigurationCallBackHandler#configure(Object)} after creation of the
031 * object.
032 *
033 * @author Oliver Wolff
034 * @param <T> identifying the type of the objects to be instantiated
035 */
036@RequiredArgsConstructor
037@ToString(of = "parameterizedInstantiator")
038public class CallbackAwareInstantiator<T> implements ParameterizedInstantiator<T> {
039
040    private final ParameterizedInstantiator<T> parameterizedInstantiator;
041    private final ConfigurationCallBackHandler<T> callBackHandler;
042
043    @Override
044    public T newInstance(final List<PropertySupport> properties, final boolean generatePropertyValues) {
045        final var instance = parameterizedInstantiator.newInstance(properties, generatePropertyValues);
046        callBackHandler.configure(instance);
047        return instance;
048    }
049
050    @Override
051    public T newInstance(final List<PropertyMetadata> properties) {
052        final var instance = parameterizedInstantiator.newInstance(properties);
053        callBackHandler.configure(instance);
054        return instance;
055    }
056
057    @Override
058    public T newInstanceMinimal() {
059        final var instance = parameterizedInstantiator.newInstanceMinimal();
060        callBackHandler.configure(instance);
061        return instance;
062    }
063
064    @Override
065    public T newInstanceFull() {
066        final var instance = parameterizedInstantiator.newInstanceFull();
067        callBackHandler.configure(instance);
068        return instance;
069    }
070
071    @Override
072    public RuntimeProperties getRuntimeProperties() {
073        return parameterizedInstantiator.getRuntimeProperties();
074    }
075
076}