/** * Factory method that performs validation. * <p> * Creates a Factory that will return a clone of the same prototype object * each time the factory is used. The prototype will be cloned using one of these * techniques (in order): * <ul> * <li>public clone method * <li>public copy constructor * <li>serialization clone * <ul> * * @param <T> the type the factory creates * @param prototype the object to clone each time in the factory * @return the <code>prototype</code> factory, or a {@link ConstantFactory#NULL_INSTANCE} if * the {@code prototype} is {@code null} * @throws IllegalArgumentException if the prototype cannot be cloned */ @SuppressWarnings("unchecked") public static <T> Factory<T> prototypeFactory(final T prototype) { if (prototype == null) { return ConstantFactory.<T>constantFactory(null); } try { final Method method = prototype.getClass().getMethod("clone", (Class[]) null); return new PrototypeCloneFactory<T>(prototype, method); } catch (final NoSuchMethodException ex) { try { prototype.getClass().getConstructor(new Class<?>[] { prototype.getClass() }); return new InstantiateFactory<T>( (Class<T>) prototype.getClass(), new Class<?>[] { prototype.getClass() }, new Object[] { prototype }); } catch (final NoSuchMethodException ex2) { if (prototype instanceof Serializable) { return (Factory<T>) new PrototypeSerializationFactory<Serializable>((Serializable) prototype); } } } throw new IllegalArgumentException("The prototype must be cloneable via a public clone method"); }
/** * Factory method that performs validation. * * @param <T> the type the factory creates * @param classToInstantiate the class to instantiate, not null * @param paramTypes the constructor parameter types, cloned * @param args the constructor arguments, cloned * @return a new instantiate factory * @throws NullPointerException if classToInstantiate is null * @throws IllegalArgumentException if paramTypes does not match args */ public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, final Object[] args) { if (classToInstantiate == null) { throw new NullPointerException("Class to instantiate must not be null"); } if (paramTypes == null && args != null || paramTypes != null && args == null || paramTypes != null && args != null && paramTypes.length != args.length) { throw new IllegalArgumentException("Parameter types must match the arguments"); } if (paramTypes == null || paramTypes.length == 0) { return new InstantiateFactory<T>(classToInstantiate); } return new InstantiateFactory<T>(classToInstantiate, paramTypes, args); }
/** * Creates a constant leave from a Factory. * @param <T> Type of the Collection entry. * @param <R> Return type. * @param p a Provider. * @return a new Node. */ public static <T,R> Node<T,R> createLeave(final Factory<R> p) { return new Node<T,R>() { @Override public R exec(T element) { return p.create(); } }; }
/** * Constructor that wraps (not copies). * * @param map the map to decorate, must not be null * @param factory the factory to use, must not be null * @throws NullPointerException if map or factory is null */ protected LazyMap(final Map<K,V> map, final Factory<? extends V> factory) { super(map); if (factory == null) { throw new NullPointerException("Factory must not be null"); } this.factory = FactoryTransformer.factoryTransformer(factory); }
/** * Creates a MultiValueMap which decorates the given <code>map</code> and * creates the value collections using the supplied <code>collectionFactory</code>. * * @param <C> the collection class type * @param map the map to decorate * @param collectionFactory the collection factory which must return a Collection instance */ @SuppressWarnings("unchecked") protected <C extends Collection<V>> MultiValueMap(final Map<K, ? super C> map, final Factory<C> collectionFactory) { super((Map<K, Object>) map); if (collectionFactory == null) { throw new IllegalArgumentException("The factory must not be null"); } this.collectionFactory = collectionFactory; }
/** * Constructor that wraps (not copies). * * @param list the list to decorate, must not be null * @param factory the factory to use for creation, must not be null * @throws NullPointerException if list or factory is null */ protected LazyList(final List<E> list, final Factory<? extends E> factory) { super(list); if (factory == null) { throw new IllegalArgumentException("Factory must not be null"); } this.factory = factory; }
/** * Factory method that performs validation. * * @param <T> the type of the constant * @param constantToReturn the constant object to return each time in the factory * @return the <code>constant</code> factory. */ @SuppressWarnings("unchecked") // The null factory works for all object types public static <T> Factory<T> constantFactory(final T constantToReturn) { if (constantToReturn == null) { return (Factory<T>) NULL_INSTANCE; } return new ConstantFactory<T>(constantToReturn); }
/** * Factory method to create a defaulting map. * <p> * The factory specified is called when a missing key is found. * The result will be returned as the result of the map get(key) method. * * @param <K> the key type * @param <V> the value type * @param map the map to decorate, must not be null * @param factory the factory to use to create entries, must not be null * @return a new defaulting map * @throws NullPointerException if map or factory is null * @since 4.0 */ public static <K, V> DefaultedMap<K, V> defaultedMap(final Map<K, V> map, final Factory<? extends V> factory) { if (factory == null) { throw new IllegalArgumentException("Factory must not be null"); } return new DefaultedMap<K, V>(map, FactoryTransformer.factoryTransformer(factory)); }
/** * Factory method that performs validation. * * @param <I> the input type * @param <O> the output type * @param factory the factory to call, not null * @return the <code>factory</code> transformer * @throws NullPointerException if the factory is null */ public static <I, O> Transformer<I, O> factoryTransformer(final Factory<? extends O> factory) { if (factory == null) { throw new NullPointerException("Factory must not be null"); } return new FactoryTransformer<I, O>(factory); }
/** * Factory method to create a lazily instantiated map. * * @param <K> the key type * @param <V> the value type * @param map the map to decorate, must not be null * @param factory the factory to use, must not be null * @return a new lazy map * @throws NullPointerException if map or factory is null * @since 4.0 */ public static <K, V> LazyMap<K, V> lazyMap(final Map<K, V> map, final Factory< ? extends V> factory) { return new LazyMap<K,V>(map, factory); }
/** * Factory method to create a lazily instantiated sorted map. * * @param <K> the key type * @param <V> the value type * @param map the map to decorate, must not be null * @param factory the factory to use, must not be null * @return a new lazy sorted map * @throws NullPointerException if map or factory is null * @since 4.0 */ public static <K, V> LazySortedMap<K, V> lazySortedMap(final SortedMap<K, V> map, final Factory<? extends V> factory) { return new LazySortedMap<K,V>(map, factory); }
/** * Constructor that wraps (not copies). * * @param map the map to decorate, must not be null * @param factory the factory to use, must not be null * @throws NullPointerException if map or factory is null */ protected LazySortedMap(final SortedMap<K,V> map, final Factory<? extends V> factory) { super(map, factory); }
/** * Creates a map which decorates the given <code>map</code> and * creates the value collections using the supplied <code>collectionFactory</code>. * * @param <K> the key type * @param <V> the value type * @param <C> the collection class type * @param map the map to decorate * @param collectionFactory the collection factory (must return a Collection object). * @return a new multi-value map * @since 4.0 */ public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(final Map<K, ? super C> map, final Factory<C> collectionFactory) { return new MultiValueMap<K, V>(map, collectionFactory); }
/** * Factory method to create a lazily instantiating list. * * @param <E> the type of the elements in the list * @param list the list to decorate, must not be null * @param factory the factory to use for creation, must not be null * @return a new lazy list * @throws NullPointerException if list or factory is null * @since 4.0 */ public static <E> LazyList<E> lazyList(final List<E> list, final Factory<? extends E> factory) { return new LazyList<E>(list, factory); }
/** * Factory returning the singleton instance. * * @param <T> the type the factory creates * @return the singleton instance * @since 3.1 */ @SuppressWarnings("unchecked") // the static instance works for all types public static <T> Factory<T> exceptionFactory() { return (Factory<T>) INSTANCE; }
/** * Constructor that performs no validation. * Use <code>factoryTransformer</code> if you want that. * * @param factory the factory to call, not null */ public FactoryTransformer(final Factory<? extends O> factory) { super(); iFactory = factory; }
/** * Gets the factory. * * @return the factory * @since 3.1 */ public Factory<? extends O> getFactory() { return iFactory; }