Example usage for java.util Collection clear

List of usage examples for java.util Collection clear

Introduction

In this page you can find the example usage for java.util Collection clear.

Prototype

void clear();

Source Link

Document

Removes all of the elements from this collection (optional operation).

Usage

From source file:Main.java

public static void clear(final Collection<?> collection) {
    if (isNotEmpty(collection)) {
        collection.clear();
    }/*from   w  ww. ja v a  2  s. c  o  m*/
}

From source file:Main.java

public static <T> void rotate(Collection<T> collection, int rotateStep) {
    List<T> list = new LinkedList<T>();
    list.addAll(collection);//from  w  ww  . ja v  a  2s .c  om
    collection.clear();
    if (rotateStep > 0) {
        if (rotateStep > list.size())
            rotateStep %= list.size();
        collection.addAll(list.subList(list.size() - rotateStep, list.size()));
        collection.addAll(list.subList(0, list.size() - rotateStep));
    } else {
        if (Math.abs(rotateStep) > list.size())
            rotateStep %= list.size();
        rotateStep = Math.abs(rotateStep);
        collection.addAll(list.subList(rotateStep, list.size()));
        collection.addAll(list.subList(0, rotateStep));
    }
}

From source file:it.geosolutions.geoserver.jms.impl.utils.BeanUtils.java

/**
 * This is a 'smart' (perform checks for some special cases) update function which should be used to copy of the properties for objects
 * of the catalog and configuration./*from  w  ww.j av a 2s .  c om*/
 * 
 * @param <T> the type of the bean to update
 * @param info the bean instance to update
 * @param properties the list of string of properties to update
 * @param values the list of new values to update
 * 
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
public static <T> void smartUpdate(final T info, final List<String> properties, final List<Object> values)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Iterator<String> itPropertyName = properties.iterator();
    final Iterator<Object> itValue = values.iterator();
    while (itPropertyName.hasNext() && itValue.hasNext()) {
        String propertyName = itPropertyName.next();
        final Object value = itValue.next();

        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(info, propertyName);
        // return null if there is no such descriptor
        if (pd == null) {
            // this is a special case used by the NamespaceInfoImpl setURI
            // the propertyName coming from the ModificationProxy is set to 'uRI'
            // lets set it to uri
            propertyName = propertyName.toUpperCase();
            pd = PropertyUtils.getPropertyDescriptor(info, propertyName);
            if (pd == null) {
                return;
            }
        }
        if (pd.getWriteMethod() != null) {
            PropertyUtils.setProperty(info, propertyName, value);
        } else {
            // T interface do not declare setter method for this property
            // lets use getter methods to get the property reference
            final Object property = PropertyUtils.getProperty(info, propertyName);

            // check type of property to apply new value
            if (Collection.class.isAssignableFrom(pd.getPropertyType())) {
                final Collection<?> liveCollection = (Collection<?>) property;
                liveCollection.clear();
                liveCollection.addAll((Collection) value);
            } else if (Map.class.isAssignableFrom(pd.getPropertyType())) {
                final Map<?, ?> liveMap = (Map<?, ?>) property;
                liveMap.clear();
                liveMap.putAll((Map) value);
            } else {
                if (CatalogUtils.LOGGER.isLoggable(java.util.logging.Level.SEVERE))
                    CatalogUtils.LOGGER.severe("Skipping unwritable property " + propertyName
                            + " with property type " + pd.getPropertyType());
            }
        }
    }
}

From source file:Main.java

/**
 * Returns a new collection containing clones of all the items in the
 * specified collection./*  w  w w . j a  v  a2 s  .c o  m*/
 * 
 * @param collection
 *          the collection (<code>null</code> not permitted).
 * @return A new collection containing clones of all the items in the
 *         specified collection.
 * @throws CloneNotSupportedException
 *           if any of the items in the collection cannot be cloned.
 */
public static Collection deepClone(final Collection collection) throws CloneNotSupportedException {

    if (collection == null) {
        throw new IllegalArgumentException("Null 'collection' argument.");
    }
    // all JDK-Collections are cloneable ...
    // and if the collection is not clonable, then we should throw
    // a CloneNotSupportedException anyway ...
    final Collection result = (Collection) clone(collection);
    result.clear();
    final Iterator iterator = collection.iterator();
    while (iterator.hasNext()) {
        final Object item = iterator.next();
        if (item != null) {
            result.add(clone(item));
        } else {
            result.add(null);
        }
    }
    return result;
}

From source file:Main.java

public static final void distinctCollection(Collection<? extends Object> collection) {
    if (collection != null && !collection.isEmpty()) {
        Set<? extends Object> distinctSet = new HashSet<Object>(collection);

        collection.clear();

        ((Collection<Object>) collection).addAll(distinctSet);
    }//from  ww w .  j a  v  a 2  s. com
}

From source file:com.projity.util.DataUtils.java

public static void extractObjectsOfClassFromNodeList(Collection result, Collection nodeList,
        Class objectClass) {/*from  w  w w .j  a  v  a  2 s. c  o  m*/
    result.clear();
    Iterator i = nodeList.iterator();
    Object nodeObject;

    while (i.hasNext()) {
        nodeObject = ((Node) i.next()).getImpl();
        nodeObject = DataUtils.extractObjectOfClass(nodeObject, objectClass);
        if (nodeObject != null) {
            if (!result.contains(nodeObject)) // only add if not already in there
                result.add(nodeObject);
        }

    }
}

From source file:com.hihframework.core.utils.CollectionUtils.java

/**
 * ?ID?,???./*from   ww  w  .  j a  v a2s  .c  o m*/
 * http?????idhibernate???????
 * ?
 * ???ID?,ID?ID??.
 * 
 * @param collection
 *            ??
 * @param checkedIds
 *            ?
 * @param idName
 *            ID??
 * @param clazz
 *            ?
 */
public static <T, ID> void mergeByCheckedIds(Collection<T> collection, Collection<ID> checkedIds, String idName,
        Class<T> clazz) throws Exception {

    if (checkedIds == null) {
        collection.clear();
        return;
    }

    Iterator<T> it = collection.iterator();

    while (it.hasNext()) {
        T obj = it.next();
        if (checkedIds.contains(PropertyUtils.getProperty(obj, idName))) {
            checkedIds.remove(PropertyUtils.getProperty(obj, idName));
        } else {
            it.remove();
        }
    }

    for (ID id : checkedIds) {
        T obj = clazz.newInstance();
        PropertyUtils.setProperty(obj, idName, id);
        collection.add(obj);
    }
}

From source file:com.anhth12.lambda.common.random.RandomManager.java

/**
 * <em>Only call in test code.</em> Causes all known instances of {@link RandomGenerator},
 * and future ones, to be started from a fixed seed. This is useful for making
 * tests deterministic.//from w w  w. ja va 2s. com
 */
public static void useTestSeed() {
    useTestSeed = true;
    Collection<RandomGenerator> instances = INSTANCES.get();
    if (instances != null) {
        synchronized (instances) {
            for (RandomGenerator random : instances) {
                random.setSeed(TEST_SEED);
            }
            instances.clear();
        }
        INSTANCES.clear();
    }
}

From source file:Main.java

/**
 * Recycles the given nodes.//from  ww  w .j ava 2  s.c  o m
 *
 * @param nodes The nodes to recycle.
 */
public static void recycleNodes(Collection<AccessibilityNodeInfoCompat> nodes) {
    if (nodes == null) {
        return;
    }

    for (AccessibilityNodeInfoCompat node : nodes) {
        if (node != null) {
            node.recycle();
        }
    }

    nodes.clear();
}

From source file:Main.java

/**
 * Removes duplicate objects from the Collection.
 * @param p_collection a Collection// w ww. j av  a  2  s  .com
 * @returns true if one or more duplicate objects were removed.
 */
public static boolean removeDuplicates(Collection p_collection) {
    if (p_collection == null) {
        return false;
    }
    HashSet set = new HashSet(p_collection.size());
    Iterator it = p_collection.iterator();
    while (it.hasNext()) {
        set.add(it.next());
    }
    if (set.size() != p_collection.size()) {
        p_collection.clear();
        p_collection.addAll(set);
        return true;
    }
    return false;
}