Example usage for org.apache.commons.collections CollectionUtils typedCollection

List of usage examples for org.apache.commons.collections CollectionUtils typedCollection

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils typedCollection.

Prototype

public static Collection typedCollection(Collection collection, Class type) 

Source Link

Document

Returns a typed collection backed by the given collection.

Usage

From source file:com.feilong.commons.core.util.CollectionsUtilTest.java

/**
 * Test./*from   w w  w. j a  v a 2s.c om*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test() {
    try {
        Class clz = User.class;
        Collection collection = CollectionUtils.typedCollection(new ArrayList(), clz);
        collection.add(clz.newInstance());

        log.info(collection.size() + "");
        for (Object object : collection) {
            User user = (User) object;
            log.info(user.getName());
        }

        log.info("hahahah");

        Collection<User> collection2 = collection;
        log.info(collection2.size() + "");
        for (Object object : collection2) {
            User user = (User) object;
            log.info(user.getName());
        }

    } catch (InstantiationException e) {
        log.error(e.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        log.error(e.getClass().getName(), e);
    }
}

From source file:org.openengsb.core.persistence.internal.DefaultConfigPersistenceService.java

@Override
@SuppressWarnings("unchecked")
public <ConfigType extends ConfigItem<?>> List<ConfigType> load(Map<String, String> metadata)
        throws PersistenceException, InvalidConfigurationException {
    List<ConfigType> configItems = new ArrayList<ConfigType>();
    Collection<ConfigItem<?>> result = CollectionUtils.typedCollection(backendService.load(metadata),
            ConfigItem.class);
    for (ConfigItem<?> configItem : result) {
        configItems.add((ConfigType) configItem);
    }/*from   w  w  w .  ja v  a2  s .c o  m*/
    return configItems;
}

From source file:org.openengsb.core.test.AbstractOsgiMockServiceTest.java

private <T> ServiceReference<T> putService(T service, Dictionary<String, Object> props) {
    @SuppressWarnings("unchecked")
    ServiceReference<T> serviceReference = mock(ServiceReference.class);
    long serviceId = --this.serviceId;
    LOGGER.info("registering service with ID: " + serviceId);
    props.put(Constants.SERVICE_ID, serviceId);
    services.put(serviceReference, service);
    synchronized (serviceReferences) {
        serviceReferences.put(serviceReference, props);
    }/*  w ww .j a v a 2  s  .c om*/
    when(serviceReference.getProperty(anyString())).thenAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return serviceReferences.get(invocation.getMock()).get(invocation.getArguments()[0]);
        }
    });
    when(serviceReference.getBundle()).thenReturn(bundle);
    when(serviceReference.getPropertyKeys()).thenAnswer(new Answer<String[]>() {
        @Override
        public String[] answer(InvocationOnMock invocation) throws Throwable {
            Dictionary<String, Object> dictionary = serviceReferences.get(invocation.getMock());
            List<?> list = EnumerationUtils.toList(dictionary.keys());
            @SuppressWarnings("unchecked")
            Collection<String> typedCollection = CollectionUtils.typedCollection(list, String.class);
            return typedCollection.toArray(new String[0]);
        }
    });
    return serviceReference;
}