All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.navercorp.fixturemonkey.resolver.DefaultArbitraryBuilderCandidate Maven / Gradle / Ivy

The newest version!
/*
 * Fixture Monkey
 *
 * Copyright (c) 2021-present NAVER Corp.
 *
 * 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
 *
 *      http://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 com.navercorp.fixturemonkey.resolver;

import static java.util.Objects.requireNonNull;

import java.util.function.Function;
import java.util.function.UnaryOperator;

import com.navercorp.fixturemonkey.ArbitraryBuilder;
import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.type.TypeReference;
import com.navercorp.fixturemonkey.api.type.Types;
import com.navercorp.fixturemonkey.buildergroup.ArbitraryBuilderCandidate;

class DefaultArbitraryBuilderCandidate implements ArbitraryBuilderCandidate {

	private final TypeReference typeReference;

	private final Function> arbitraryBuilderRegisterer;

	private DefaultArbitraryBuilderCandidate(
		TypeReference typeReference,
		Function> arbitraryBuilderRegisterer
	) {
		this.typeReference = typeReference;
		this.arbitraryBuilderRegisterer = arbitraryBuilderRegisterer;
	}

	public static  Builder of(Class classType) {
		return new Builder()
			.classType(classType);
	}

	public static  Builder of(TypeReference typeReference) {
		return new Builder()
			.typeReference(typeReference);
	}

	@Override
	public Class getClassType() {
		return Types.getActualType(
			requireNonNull(this.typeReference).getAnnotatedType()
		);
	}

	@Override
	public Function> getArbitraryBuilderRegisterer() {
		return arbitraryBuilderRegisterer;
	}

	static class Builder {
		private TypeReference typeReference;

		private UnaryOperator> builderSpec;

		public Builder register(UnaryOperator> builderSpec) {
			this.builderSpec = builderSpec;
			return this;
		}

		public DefaultArbitraryBuilderCandidate buildWithFixedValue(T value) {
			if (typeReference == null) {
				throw new IllegalArgumentException("typeReference must exist for fixed value");
			}

			return new DefaultArbitraryBuilderCandidate<>(
				typeReference,
				(fixtureMonkey) -> fixtureMonkey.giveMeBuilder(value)
			);
		}

		public DefaultArbitraryBuilderCandidate build() {
			if (typeReference == null) {
				throw new IllegalArgumentException("typeReference must exist.");
			}

			if (builderSpec == null) {
				throw new IllegalArgumentException("builderSpec must be registered.");
			}

			Function> registerer =
				(fixtureMonkey) ->
					builderSpec.apply(fixtureMonkey.giveMeBuilder(typeReference));

			return new DefaultArbitraryBuilderCandidate<>(typeReference, registerer);
		}

		private Builder classType(Class classType) {
			this.typeReference = new TypeReference(classType) {
			};
			return this;
		}

		private Builder typeReference(TypeReference typeReference) {
			this.typeReference = typeReference;
			return this;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy