org.instancio.internal.ApiImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of instancio-core Show documentation
Show all versions of instancio-core Show documentation
The standalone module of Instancio
/*
* Copyright 2022-2023 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.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.Settings;
import java.lang.reflect.Type;
import java.util.Arrays;
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) {
ApiValidator.validateGenerateSecondArgument(gen);
modelContextBuilder.withGeneratorSpec(selector, gen);
return this;
}
@Override
public InstancioApi generate(
final TargetSelector selector,
final GeneratorSpec spec) {
ApiValidator.validateGenerateSecondArgument(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) {
ApiValidator.validateGeneratorNotNull(generator);
modelContextBuilder.withGenerator(selector, generator);
return this;
}
@Override
public InstancioApi supply(
final TargetSelector selector,
final Supplier supplier) {
ApiValidator.validateSupplierNotNull(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) {
ApiValidator.notNull(assignments, "assignments array must not be null");
modelContextBuilder.withAssignments(assignments);
return this;
}
@Override
public InstancioApi withSeed(final long seed) {
modelContextBuilder.withSeed(seed);
return this;
}
@Override
public InstancioApi withMaxDepth(final int maxDepth) {
ApiValidator.isTrue(maxDepth >= 0, "Maximum depth must not be negative: %s", maxDepth);
modelContextBuilder.withMaxDepth(maxDepth);
return this;
}
@Override
public InstancioApi withNullable(final TargetSelector selector) {
modelContextBuilder.withNullable(selector);
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 Result<>(createRootObject(model), seed);
}
@Override
public Stream stream() {
final InternalModel model = createModel();
return Stream.generate(() -> createRootObject(model));
}
private T createRootObject(final InternalModel model) {
return new InstancioEngine(model).createRootObject();
}
private InternalModel createModel() {
return new InternalModel<>(modelContextBuilder.build());
}
}