org.apache.commons.collections.Factory Java Examples

The following examples show how to use org.apache.commons.collections.Factory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrototypeFactory.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * 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 prototype  the object to clone each time in the factory
 * @return the <code>prototype</code> factory
 * @throws IllegalArgumentException if the prototype is null
 * @throws IllegalArgumentException if the prototype cannot be cloned
 */
public static Factory getInstance(Object prototype) {
    if (prototype == null) {
        return ConstantFactory.NULL_INSTANCE;
    }
    try {
        Method method = prototype.getClass().getMethod("clone", null);
        return new PrototypeCloneFactory(prototype, method);

    } catch (NoSuchMethodException ex) {
        try {
            prototype.getClass().getConstructor(new Class[] { prototype.getClass()});
            return new InstantiateFactory(
                prototype.getClass(),
                new Class[] { prototype.getClass()},
                new Object[] { prototype });

        } catch (NoSuchMethodException ex2) {
            if (prototype instanceof Serializable) {
                return new PrototypeSerializationFactory((Serializable) prototype);
            }
        }
    }
    throw new IllegalArgumentException("The prototype must be cloneable via a public clone method");
}
 
Example #2
Source File: InstantiateFactory.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * Factory method that performs validation.
 * 
 * @param classToInstantiate  the class to instantiate, not null
 * @param paramTypes  the constructor parameter types
 * @param args  the constructor arguments
 * @return a new instantiate factory
 */
public static Factory getInstance(Class classToInstantiate, Class[] paramTypes, Object[] args) {
    if (classToInstantiate == null) {
        throw new IllegalArgumentException("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(classToInstantiate);
    } else {
        paramTypes = (Class[]) paramTypes.clone();
        args = (Object[]) args.clone();
        return new InstantiateFactory(classToInstantiate, paramTypes, args);
    }
}
 
Example #3
Source File: CommonsTest.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * Lazy测试
 * 需要该元素的时候,才会生成
 */
@SuppressWarnings("unchecked")
private static void lazyTest(){
	List<String> lazy=LazyList.decorate(new ArrayList<>(), new Factory() {
		@Override
		public Object create() {
			return "Hello";
		}
	}); 
	//访问了第三个元素,此时0和1位null
	//get几就增加了几加一 , 输出依旧是 Hello 
	String str=lazy.get(2);
	System.out.println("str:"+str);//str:Hello 
	//加入的第四个元素
	lazy.add("world");
	//元素总个为4个
	System.out.println("lazy.size():"+lazy.size());//lazy.size():4
}
 
Example #4
Source File: LazyMap.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * 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 IllegalArgumentException if map or factory is null
 */
protected LazyMap(Map map, Factory factory) {
    super(map);
    if (factory == null) {
        throw new IllegalArgumentException("Factory must not be null");
    }
    this.factory = FactoryTransformer.getInstance(factory);
}
 
Example #5
Source File: LazyList.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * 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 IllegalArgumentException if list or factory is null
 */
protected LazyList(List list, Factory factory) {
    super(list);
    if (factory == null) {
        throw new IllegalArgumentException("Factory must not be null");
    }
    this.factory = factory;
}
 
Example #6
Source File: ConstantFactory.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that performs validation.
 *
 * @param constantToReturn  the constant object to return each time in the factory
 * @return the <code>constant</code> factory.
 */
public static Factory getInstance(Object constantToReturn) {
    if (constantToReturn == null) {
        return NULL_INSTANCE;
    }
    return new ConstantFactory(constantToReturn);
}
 
Example #7
Source File: RouteAssignmentCommand.java    From dddsample-core with MIT License 4 votes vote down vote up
public static Factory factory() {
  return LegCommand::new;
}
 
Example #8
Source File: FactoryTransformer.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method that performs validation.
 * 
 * @param factory  the factory to call, not null
 * @return the <code>factory</code> transformer
 * @throws IllegalArgumentException if the factory is null
 */
public static Transformer getInstance(Factory factory) {
    if (factory == null) {
        throw new IllegalArgumentException("Factory must not be null");
    }
    return new FactoryTransformer(factory);
}
 
Example #9
Source File: LazyMap.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a lazily instantiated map.
 * 
 * @param map  the map to decorate, must not be null
 * @param factory  the factory to use, must not be null
 * @throws IllegalArgumentException if map or factory is null
 */
public static Map decorate(Map map, Factory factory) {
    return new LazyMap(map, factory);
}
 
Example #10
Source File: LazySortedMap.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a lazily instantiated sorted map.
 * 
 * @param map  the map to decorate, must not be null
 * @param factory  the factory to use, must not be null
 * @throws IllegalArgumentException if map or factory is null
 */
public static SortedMap decorate(SortedMap map, Factory factory) {
    return new LazySortedMap(map, factory);
}
 
Example #11
Source File: LazySortedMap.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * 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 IllegalArgumentException if map or factory is null
 */
protected LazySortedMap(SortedMap map, Factory factory) {
    super(map, factory);
}
 
Example #12
Source File: LazyList.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a lazily instantiating list.
 * 
 * @param list  the list to decorate, must not be null
 * @param factory  the factory to use for creation, must not be null
 * @throws IllegalArgumentException if list or factory is null
 */
public static List decorate(List list, Factory factory) {
    return new LazyList(list, factory);
}
 
Example #13
Source File: ExceptionFactory.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory returning the singleton instance.
 * 
 * @return the singleton instance
 * @since Commons Collections 3.1
 */
public static Factory getInstance() {
    return INSTANCE;
}
 
Example #14
Source File: FactoryTransformer.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that performs no validation.
 * Use <code>getInstance</code> if you want that.
 * 
 * @param factory  the factory to call, not null
 */
public FactoryTransformer(Factory factory) {
    super();
    iFactory = factory;
}
 
Example #15
Source File: FactoryTransformer.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the factory.
 * 
 * @return the factory
 * @since Commons Collections 3.1
 */
public Factory getFactory() {
    return iFactory;
}