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

org.bbottema.loremipsumobjects.ClassBindings Maven / Gradle / Ivy

/*
 * Copyright (C) 2019 Benny Bottema ([email protected])
 *
 * 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 org.bbottema.loremipsumobjects;

import org.bbottema.loremipsumobjects.typefactories.ClassBasedFactory;
import org.bbottema.loremipsumobjects.typefactories.ConstructorBasedFactory;
import org.bbottema.loremipsumobjects.typefactories.LoremIpsumObjectFactory;
import org.bbottema.loremipsumobjects.typefactories.MethodBasedFactory;
import org.bbottema.loremipsumobjects.typefactories.RandomBooleanFactory;
import org.bbottema.loremipsumobjects.typefactories.RandomPrimitiveFactory;
import org.bbottema.loremipsumobjects.typefactories.RandomStringFactory;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Stores a list of classes / interfaces and their associated deferred types. This list is used to tell {@link LoremIpsumObjectCreator} which specific
 * implementation it should use to produce new dummy instances for a certain type. This is most useful to make sure Dummy Creator can create
 * dummy objects for interface / abstract types it encounters.
 * 

* Deferred types are produced by {@link LoremIpsumObjectFactory} implementations. Default factories are in place for strings, primitives, enums. In * addition,there are factories that can be configured to invoke a specific {@link Method} ({@link MethodBasedFactory}) or a specific * {@link Constructor} ({@link ConstructorBasedFactory}). Finally, there is a factory to return a fixed instance. The * {@link ClassBasedFactory} is used automatically internally if no class binding can be found for a particular type. Examples are: *

    *
  • List -> ArrayList (deferred to first succesfully invoked class constructor)
  • *
  • List -> LinkedList (idem)
  • *
  • Integer -> 4443 (which is autoboxed to an Integer and then acts as deferred instance)
  • *
  • Foo -> FooFactory.class.getMethod('createFoo') (deferred to method call)
  • *
  • Apple -> Apple.class.getConstructor(String.class) (deferred to constructor call)
  • *
  • Apple -> new AppleFactory() (deferred to object factory call)
  • *
*/ public class ClassBindings { private final HashMap, LoremIpsumObjectFactory> bindings = new HashMap<>(); /** * Initializes with basic bindings for primitives, arrays and strings. */ public ClassBindings() { add(Long.TYPE, new RandomPrimitiveFactory<>(Long.TYPE)); add(Integer.TYPE, new RandomPrimitiveFactory<>(Integer.TYPE)); add(Float.TYPE, new RandomPrimitiveFactory<>(Float.TYPE)); add(Boolean.TYPE, new RandomPrimitiveFactory<>(Boolean.TYPE)); add(Character.TYPE, new RandomPrimitiveFactory<>(Character.TYPE)); add(Byte.TYPE, new RandomPrimitiveFactory<>(Byte.TYPE)); add(Short.TYPE, new RandomPrimitiveFactory<>(Short.TYPE)); add(Double.TYPE, new RandomPrimitiveFactory<>(Double.TYPE)); add(String.class, new RandomStringFactory()); add(Boolean.class, new RandomBooleanFactory()); } /** * Binds a {@link LoremIpsumObjectFactory} to a specific Class instance. * * @param clazz The class to bind the dummy factory to. * @param factory The factory to bind the the given class. * @throws IllegalArgumentException Thrown if {@link LoremIpsumObjectFactory#isValidForType(Class)} returns false or throws an * IllegalArgumentException itself. * @see LoremIpsumObjectFactory#isValidForType(Class) */ public void add(final Class clazz, final LoremIpsumObjectFactory factory) { try { if (factory.isValidForType(clazz)) { bindings.put(clazz, factory); } else { // factory didn't throw an exception, so we'll do it ourself throw new IllegalArgumentException(); } } catch (final IllegalArgumentException e) { // note: exception is also thrown by LoremIpsumObjectFactory.isValidForType throw new IllegalArgumentException(String.format("dummy factory [%s] is not valid for class type [%s]", factory, clazz), e); } } /** * This method returns a binding made for the given class. This binding might be of one of the following type: Constructor * Implementation of a Interface Method Object */ @SuppressWarnings("unchecked") @Nullable public LoremIpsumObjectFactory find(final Class _class) { return (LoremIpsumObjectFactory) bindings.get(_class); } /** * You can call this method to build some default bindings for common classes. This includes List.class, Map.class, Set.class. *

* These are in addition to the basic bindings added in {@link #ClassBindings()}. */ public static ClassBindings defaultBindings() { final ClassBindings classBindings = new ClassBindings(); classBindings.add(List.class, new ClassBasedFactory<>(ArrayList.class)); classBindings.add(Map.class, new ClassBasedFactory<>(HashMap.class)); classBindings.add(Set.class, new ClassBasedFactory<>(HashSet.class)); return classBindings; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy