org.apache.commons.collections.Factory类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(118)

本文整理了Java中org.apache.commons.collections.Factory类的一些代码示例,展示了Factory类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Factory类的具体详情如下:
包路径:org.apache.commons.collections.Factory
类名称:Factory

Factory介绍

[英]Defines a functor interface implemented by classes that create objects.

A Factory creates an object without using an input parameter. If an input parameter is required, then Transformer is more appropriate.

Standard implementations of common factories are provided by FactoryUtils. These include factories that return a constant, a copy of a prototype or a new instance.
[中]定义由创建对象的类实现的函子接口。
Factory在不使用输入参数的情况下创建对象。如果需要输入参数,则变压器更合适。
常见工厂的标准实现由FactoryUtils提供。其中包括返回常量、原型副本或新实例的工厂。

代码示例

代码示例来源:origin: commons-collections/commons-collections

/**
 * Transforms the input by ignoring the input and returning the result of
 * calling the decorated factory.
 * 
 * @param input  the input object to transform
 * @return the transformed result
 */
public Object transform(Object input) {
  return iFactory.create();
}

代码示例来源:origin: wildfly/wildfly

/**
 * Transforms the input by ignoring the input and returning the result of
 * calling the decorated factory.
 * 
 * @param input  the input object to transform
 * @return the transformed result
 */
public Object transform(Object input) {
  return iFactory.create();
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Creates a new instance of the map value Collection container
 * using the factory.
 * <p>
 * This method can be overridden to perform your own processing
 * instead of using the factory.
 *
 * @param size  the collection size that is about to be added
 * @return the new collection
 */
protected Collection createCollection(int size) {
  return (Collection) collectionFactory.create();
}

代码示例来源:origin: wildfly/wildfly

/**
 * Creates a new instance of the map value Collection container
 * using the factory.
 * <p>
 * This method can be overridden to perform your own processing
 * instead of using the factory.
 *
 * @param size  the collection size that is about to be added
 * @return the new collection
 */
protected Collection createCollection(int size) {
  return (Collection) collectionFactory.create();
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Transforms the input to result by cloning it.
 * 
 * @param input  the input object to transform
 * @return the transformed result
 */
public Object transform(Object input) {
  if (input == null) {
    return null;
  }
  return PrototypeFactory.getInstance(input).create();
}

代码示例来源:origin: wildfly/wildfly

/**
 * Transforms the input to result by cloning it.
 * 
 * @param input  the input object to transform
 * @return the transformed result
 */
public Object transform(Object input) {
  if (input == null) {
    return null;
  }
  return PrototypeFactory.getInstance(input).create();
}

代码示例来源:origin: commons-collections/commons-collections

if (object == null) {
  object = factory.create();
  getList().set(index, object);
  return object;
Object object = factory.create();
getList().add(object);
return object;

代码示例来源:origin: wildfly/wildfly

if (object == null) {
  object = factory.create();
  getList().set(index, object);
  return object;
Object object = factory.create();
getList().add(object);
return object;

代码示例来源:origin: commons-collections/commons-collections

public void testInstantiateFactoryComplex() {
  TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
  // 2nd Jan 1970
  Factory factory = FactoryUtils.instantiateFactory(Date.class,
    new Class[] {Integer.TYPE, Integer.TYPE, Integer.TYPE},
    new Object[] {new Integer(70), new Integer(0), new Integer(2)});
  assertNotNull(factory);
  Object created = factory.create();
  assertTrue(created instanceof Date);
  // long time of 1 day (== 2nd Jan 1970)
  assertEquals(new Date(1000 * 60 * 60 * 24), created);
}

代码示例来源:origin: commons-collections/commons-collections

public void testPrototypeFactoryPublicCloneMethod() throws Exception {
  Date proto = new Date();
  Factory factory = PrototypeFactory.getInstance(proto);
  assertNotNull(factory);
  Object created = factory.create();
  assertTrue(proto != created);
  assertEquals(proto, created);
  
  // check serialisation works - if enabled
  System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "true");
  try {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(buffer);
    out.writeObject(factory);
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Object dest = in.readObject();
    in.close();
  } finally {
    System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "false");
  }
}

代码示例来源:origin: commons-collections/commons-collections

public void testPrototypeFactoryPublicSerialization() throws Exception {
  Integer proto = new Integer(9);
  Factory factory = FactoryUtils.prototypeFactory(proto);
  assertNotNull(factory);
  Object created = factory.create();
  assertTrue(proto != created);
  assertEquals(proto, created);
  
  // check serialisation works - if enabled
  System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "true");
  try {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(buffer);
    out.writeObject(factory);
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Object dest = in.readObject();
    in.close();
  } finally {
    System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "false");
  }
}

代码示例来源:origin: commons-collections/commons-collections

public void testConstantFactoryConstant() {
  Integer constant = new Integer(9);
  Factory factory = FactoryUtils.constantFactory(constant);
  assertNotNull(factory);
  Object created = factory.create();
  assertSame(constant, created);
}

代码示例来源:origin: commons-collections/commons-collections

public void testPrototypeFactoryPublicCopyConstructor() throws Exception {
  Mock1 proto = new Mock1(6);
  Factory factory = PrototypeFactory.getInstance(proto);
  assertNotNull(factory);
  Object created = factory.create();
  assertTrue(proto != created);
  assertEquals(proto, created);
  
  // check serialisation works - if enabled
  System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "true");
  try {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(buffer);
    try {
      out.writeObject(factory);
    } catch (NotSerializableException ex) {
      out.close();
    }
    factory = FactoryUtils.prototypeFactory(new Mock2("S"));
    buffer = new ByteArrayOutputStream();
    out = new ObjectOutputStream(buffer);
    out.writeObject(factory);
    out.close();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    Object dest = in.readObject();
    in.close();
  } finally {
    System.setProperty(FunctorUtils.UNSAFE_SERIALIZABLE_PROPERTY, "false");
  }
}

代码示例来源:origin: commons-collections/commons-collections

public void testConstantFactoryNull() {
  Factory factory = FactoryUtils.constantFactory(null);
  assertNotNull(factory);
  Object created = factory.create();
  assertNull(created);
}

代码示例来源:origin: commons-collections/commons-collections

public void testNullFactory() {
  Factory factory = FactoryUtils.nullFactory();
  assertNotNull(factory);
  Object created = factory.create();
  assertNull(created);
}

代码示例来源:origin: commons-collections/commons-collections

public void testInstantiateFactorySimple() {
  Factory factory = FactoryUtils.instantiateFactory(Mock3.class);
  assertNotNull(factory);
  Object created = factory.create();
  assertEquals(0, ((Mock3) created).getValue());
  created = factory.create();
  assertEquals(1, ((Mock3) created).getValue());
}

代码示例来源:origin: commons-collections/commons-collections

public void testPrototypeFactoryPublicSerializationError() {
  Mock2 proto = new Mock2(new Object());
  Factory factory = FactoryUtils.prototypeFactory(proto);
  assertNotNull(factory);
  try {
    Object created = factory.create();
    
  } catch (FunctorException ex) {
    assertTrue(ex.getCause() instanceof IOException);
    return;
  }
  fail();
}

代码示例来源:origin: commons-collections/commons-collections

public void testExceptionFactory() {
  assertNotNull(FactoryUtils.exceptionFactory());
  assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory());
  try {
    FactoryUtils.exceptionFactory().create();
  } catch (FunctorException ex) {
    try {
      FactoryUtils.exceptionFactory().create();
    } catch (FunctorException ex2) {
      return;
    }
  }
  fail();
}

代码示例来源:origin: com.alibaba.citrus.tool/antx-autoexpand

/**
 * Transforms the input by ignoring the input and returning the result of
 * calling the decorated factory.
 * 
 * @param input  the input object to transform
 * @return the transformed result
 */
public Object transform(Object input) {
  return iFactory.create();
}

代码示例来源:origin: org.apache.openjpa/openjpa-all

/**
 * Creates a new instance of the map value Collection container
 * using the factory.
 * <p>
 * This method can be overridden to perform your own processing
 * instead of using the factory.
 *
 * @param size  the collection size that is about to be added
 * @return the new collection
 */
protected Collection createCollection(int size) {
  return (Collection) collectionFactory.create();
}

相关文章