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:ResourceBundleSupport.java

/**
 * Returns a new collection containing clones of all the items in the
 * specified collection.//from   w  ww .  j a  v a  2s .  com
 *
 * @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) ObjectUtilities.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:org.atemsource.atem.impl.common.attribute.collection.AbstractCollectionAttributeImpl.java

public void clear(Object entity) {
    Collection<J> elements = getElements(entity);
    if (elements != null) {
        elements.clear();
    } else {/*from w ww .j  av  a2  s  .  com*/
        try {
            setValue(entity, createEmptyCollection());
        } catch (Exception e) {
            throw new TechnicalException("cannot instantiate collection", e);
        }
    }
}

From source file:org.jdto.mergers.PropertyCollectionMerger.java

private Collection getCollectionToReturn(Collection value) {
    try {/*ww  w. j a  v  a  2 s . c o m*/
        Collection ret = null;

        if (value instanceof Cloneable) {
            ret = (Collection) ObjectUtils.clone(value);
            ret.clear();
        } else {
            //try to create a new instance of the collection
            ret = BeanClassUtils.createInstance(value.getClass());
        }
        return ret;
    } catch (Exception ex) {
        logger.error("Exception while trying to instantiate collection", ex);
        throw new RuntimeException(ex);
    }
}

From source file:org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingConverter.java

protected void setPropertyWhichMayBeAHibernateCollection(Object instance, String propertyName, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (value instanceof Collection) {
        //We need to handle collections in a way that Hibernate can track.
        Collection<?> newCollection = (Collection<?>) value;
        Object oldValue = PropertyUtils.getProperty(instance, propertyName);
        if (oldValue instanceof Collection) {
            Collection collection = (Collection) oldValue;
            collection.clear();
            collection.addAll(newCollection);
        } else {/* www .ja va  2 s. c om*/
            PropertyUtils.setProperty(instance, propertyName, value);
        }
    } else {
        PropertyUtils.setProperty(instance, propertyName, value);
    }
}

From source file:com.zero.service.impl.BaseServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void copyProperties(Object source, Object target) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null) {
            PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }//  ww  w.  j a va  2  s.c om
                    Object sourceValue = readMethod.invoke(source);
                    Object targetValue = readMethod.invoke(target);
                    if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                        Collection collection = (Collection) targetValue;
                        collection.clear();
                        collection.addAll((Collection) sourceValue);
                    } else {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, sourceValue);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:hr.fer.spocc.util.CharacterEscaper.java

/**
 * Unescapes the given string. The indexes of the characters
 * which were obtained by unescaping are added to the specified
 * collection (which will be cleared beforehand).
 * //from   ww  w . j  av a 2 s . c  o  m
 * @param s the string to be unescaped
 * @param escapedIndexes the collection which will contain the indexes
 * of the unescaped characters
 * @return the unescaped string
 * @throws IllegalArgumentException if the string cannot be unescaped
 */
public String unescape(String s, Collection<Integer> escapedIndexes) throws IllegalArgumentException {
    if (escapedIndexes != null)
        escapedIndexes.clear();

    boolean escapeStarted = false;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (c == escapeCharacter && !escapeStarted) {
            escapeStarted = true;
            continue;
        }
        if (escapeStarted) {
            escapeStarted = false;
            Character unescaped = unescapeMap.get(c);
            Validate.notNull(unescaped, "unknown unescape pattern after on position " + i + " in string: " + s);
            if (unescaped != null && escapedIndexes != null) {
                escapedIndexes.add(sb.length());
            }
            sb.append(unescaped != null ? unescaped : c);
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:org.carewebframework.vista.ui.notification.NotificationService.java

/**
 * Creates a list of recipients from a list of raw data.
 * /*ww w  .  ja v  a 2  s . com*/
 * @param recipientData List of raw data as returned by lookup.
 * @param isGroup If true, the list represents mail groups. If false, it represents users.
 * @param filter The text used in the lookup. It will be used to limit the returned results.
 * @param result List of recipients.
 */
private void toRecipients(List<String> recipientData, boolean isGroup, String filter,
        Collection<Recipient> result) {
    result.clear();

    for (String data : recipientData) {
        Recipient recipient = new Recipient(data, isGroup);

        if (StringUtils.startsWithIgnoreCase(recipient.getName(), filter)) {
            result.add(recipient);
        } else {
            break;
        }
    }
}

From source file:net.groupbuy.service.impl.BaseServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
    List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null
                && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
            PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }//  w  w w . jav  a2s. com
                    Object sourceValue = readMethod.invoke(source);
                    Object targetValue = readMethod.invoke(target);
                    if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                        Collection collection = (Collection) targetValue;
                        collection.clear();
                        collection.addAll((Collection) sourceValue);
                    } else {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, sourceValue);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:org.hyperic.hq.measurement.server.session.Measurement.java

public void setBaseline(Baseline b) {
    final Collection baselines = getBaselinesBag();
    if (!baselines.isEmpty())
        baselines.clear();

    if (b != null)
        baselines.add(b);//  ww w.j av a  2 s .c  o m
}

From source file:com.vaynberg.wicket.select2.Select2MultiChoice.java

@Override
public void updateModel() {
    Collection<T> choices = getModelObject();
    Collection<T> selection = getConvertedInput();

    if (choices == null) {
        setModelObject(selection);//  w  w  w.j a  v a  2s. c  om
    } else {
        choices.clear();
        choices.addAll(selection);
        setModelObject(choices);
    }
}