org.instancio.internal.ApiImpl Maven / Gradle / Ivy
/*
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.instancio.internal;
import org.instancio.Assignment;
import org.instancio.GeneratorSpecProvider;
import org.instancio.InstancioApi;
import org.instancio.Model;
import org.instancio.OnCompleteCallback;
import org.instancio.Random;
import org.instancio.Result;
import org.instancio.TargetSelector;
import org.instancio.TypeTokenSupplier;
import org.instancio.generator.Generator;
import org.instancio.generator.GeneratorSpec;
import org.instancio.internal.context.ModelContext;
import org.instancio.settings.SettingKey;
import org.instancio.settings.Settings;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class ApiImpl implements InstancioApi {
private final ModelContext.Builder modelContextBuilder;
public ApiImpl(final Type klass) {
this.modelContextBuilder = ModelContext.builder(klass);
}
public ApiImpl(final TypeTokenSupplier typeToken) {
this.modelContextBuilder = ModelContext.builder(ApiValidator.validateTypeToken(typeToken));
}
public ApiImpl(final Model model) {
final InternalModel suppliedModel = (InternalModel) model;
final ModelContext suppliedContext = suppliedModel.getModelContext();
// copy context data to allow overriding
this.modelContextBuilder = suppliedContext.toBuilder();
}
protected final void addTypeParameters(final Type... types) {
modelContextBuilder.withRootTypeParameters(Arrays.asList(types));
}
@Override
public InstancioApi ignore(final TargetSelector selector) {
modelContextBuilder.withIgnored(selector);
return this;
}
@Override
public InstancioApi generate(
final TargetSelector selector,
final GeneratorSpecProvider gen) {
modelContextBuilder.withGeneratorSpec(selector, gen);
return this;
}
@Override
public InstancioApi generate(
final TargetSelector selector,
final GeneratorSpec spec) {
modelContextBuilder.withGenerator(selector, (Generator) spec);
return this;
}
@Override
public InstancioApi onComplete(
final TargetSelector selector,
final OnCompleteCallback callback) {
modelContextBuilder.withOnCompleteCallback(selector, callback);
return this;
}
@Override
public InstancioApi set(final TargetSelector selector, final V value) {
modelContextBuilder.withSupplier(selector, () -> value);
return this;
}
@Override
public InstancioApi supply(
final TargetSelector selector,
final Generator generator) {
modelContextBuilder.withGenerator(selector, generator);
return this;
}
@Override
public InstancioApi supply(
final TargetSelector selector,
final Supplier supplier) {
modelContextBuilder.withSupplier(selector, supplier);
return this;
}
@Override
public InstancioApi subtype(
final TargetSelector selector,
final Class> subtype) {
modelContextBuilder.withSubtype(selector, subtype);
return this;
}
@Override
public InstancioApi assign(final Assignment... assignments) {
modelContextBuilder.withAssignments(assignments);
return this;
}
@Override
public InstancioApi withSeed(final long seed) {
modelContextBuilder.withSeed(seed);
return this;
}
@Override
public InstancioApi withMaxDepth(final int maxDepth) {
modelContextBuilder.withMaxDepth(maxDepth);
return this;
}
@Override
public InstancioApi withNullable(final TargetSelector selector) {
modelContextBuilder.withNullable(selector);
return this;
}
@Override
public InstancioApi withSetting(final SettingKey key, final V value) {
modelContextBuilder.withSetting(key, value);
return this;
}
@Override
public InstancioApi withSettings(final Settings settings) {
modelContextBuilder.withSettings(settings);
return this;
}
@Override
public InstancioApi lenient() {
modelContextBuilder.lenient();
return this;
}
@Override
public InstancioApi verbose() {
modelContextBuilder.verbose();
return this;
}
@Override
public Model toModel() {
return createModel();
}
@Override
public T create() {
return createRootObject(createModel());
}
@Override
public Result asResult() {
final InternalModel model = createModel();
final long seed = model.getModelContext().getRandom().getSeed();
return new InternalResult<>(createRootObject(model), seed);
}
@Override
public Stream stream() {
final AtomicBoolean modelDumped = new AtomicBoolean();
final AtomicLong nextSeed = new AtomicLong();
return Stream.generate(() -> {
final InternalModel model = new InternalModel<>(modelContextBuilder.build());
// verbose() should print only once per stream()
if (modelDumped.compareAndSet(false, true)) {
InternalModelDump.printVerbose(model);
}
// Update seed for each stream element to avoid generating the same object
final Random random = model.getModelContext().getRandom();
nextSeed.set(random.longRange(1, Long.MAX_VALUE));
modelContextBuilder.withSeed(nextSeed.get());
return createRootObject(model);
});
}
private T createRootObject(final InternalModel model) {
return new InstancioEngine(model).createRootObject();
}
private InternalModel createModel() {
final InternalModel model = new InternalModel<>(modelContextBuilder.build());
InternalModelDump.printVerbose(model);
return model;
}
}