Example usage for java.util Collection add

List of usage examples for java.util Collection add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Ensures that this collection contains the specified element (optional operation).

Usage

From source file:Main.java

/**
 * Convert the enumeration to a collection.
 *
 * @param enumeration The enumeration to convert to Collection.
 * @param <T>         The type of object stored in the enumeration.
 *
 * @return A Collection containing all the elements of the enumeration.
 */// ww w  .  j  ava2  s.  c o  m
public static <T> Collection<T> toCollection(Enumeration<T> enumeration) {
    Collection<T> collection = newList(25);

    while (enumeration.hasMoreElements()) {
        collection.add(enumeration.nextElement());
    }

    return collection;
}

From source file:Main.java

/**
 * Adds all elements in the iteration to the given collection.
 * @deprecated Replaced by {@link Collection#addAll(java.util.Collection<? extends E>)}
 *
 * @param collection the collection to add to
 * @param iterator   the iterator of elements to add, may not be null
 * @throws NullPointerException if the collection or iterator is null
 *//*from   w  w  w . j a  va 2s  . c om*/
public static <E> void addAll(Collection<E> collection, Iterator<? extends E> iterator) {
    while (iterator.hasNext()) {
        collection.add(iterator.next());
    }
}

From source file:Main.java

/**
 * Synchronises the addition of an object to a many2many relationship. For example, if you have: 
 * Product.orders and Order.products, you can implement:
 * /*from w ww .ja v a  2s.com*/
 * <pre>boolean Product.addOrder( Order prod )</pre>
 * 
 * as: 
 * 
 * <pre>return addManyToMany ( this, order, "addProduct", this.orders ); // or this.getOrders()</pre>
 *  
 * where this.orders is the set internal to Product that backs the corresponding
 * relation. This call invokes this.orders.add ( order ) and, in case this operation returns true (i.e., the order
 * was not already inside the collection), it invokes order.addProduct ( this ) too. 
 * 
 * You should implement Order.product() in a dual way. The check that the element was added to the set avoids 
 * infinite loops. 
 *  
 * @param obj the object to which added is added
 * @param added the added object
 * @param inverseRelationAddMethod the name of the method to be called to add obj to added, using the inverse side of 
 * the relation obj-added relation.
 * @param internalCollection the collection for the side from obj to added to which added is actually added
 * @return true if the added was really added to obj.
 */
public static <O, A> boolean addMany2Many(O obj, A added, String inverseRelationAddMethod,
        Collection<A> internalCollection) {
    Exception theEx = null;
    try {
        if (!internalCollection.add(added))
            return false;
        Method inverseAdder = added.getClass().getMethod(inverseRelationAddMethod, obj.getClass());
        inverseAdder.invoke(added, obj);
    } catch (NoSuchMethodException ex) {
        theEx = ex;
    } catch (SecurityException ex) {
        theEx = ex;
    } catch (IllegalAccessException ex) {
        theEx = ex;
    } catch (IllegalArgumentException ex) {
        theEx = ex;
    } catch (InvocationTargetException ex) {
        theEx = ex;
    } finally {
        if (theEx != null)
            throw new IllegalArgumentException("Internal error: " + theEx.getMessage(), theEx);
    }
    return true;
}

From source file:fi.hsl.parkandride.core.service.PasswordValidator.java

public static void validate(String password, Collection<Violation> violations) {
    if (!StringUtils.hasText(password) || !PATTERN.matcher(password).matches()) {
        violations.add(new Violation("BadPassword", "password",
                "Password with length of 8-15 must contain at least one digit, one lowercase and one uppercase character"));
    }//from w  ww .ja  v a  2  s . co m
}

From source file:org.atomsphere.management.authentication.AuthenticationUtils.java

/**
 * Accepts list of Roles and return array[] of ACEGI GrantedAuthorityImpl
 *//*from www  .  j  av  a  2  s .co m*/
public static Collection<GrantedAuthority> toGrantedAuthority(User user) {
    if (user.getAuthorities() != null && user.getAuthorities().size() > 0) {
        return user.getAuthorities();
    }

    Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

    if (user.isAdministrator()) {
        grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_ADMIN));
    } else {
        grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_USER));
    }

    return grantedAuthorities;
}

From source file:Main.java

/**
 * Returns a collection with the common elements in c1 and c2
 * @param <T>//  w w  w  .j  a  va  2 s  . co m
 * @param c1
 * @param c2
 * @return
 */
public static <T> Collection<T> intersect(Collection<T> c1, Collection<T> c2) {

    if (c1.isEmpty() || c2.isEmpty()) {
        return Collections.emptySet();
    }

    if (!(c2 instanceof Set) && (c1 instanceof Set || c1.size() > c2.size())) {
        //swap
        Collection<T> tmp = c1;
        c1 = c2;
        c2 = tmp;
    }

    Collection<T> result = new HashSet<T>();
    for (T obj : c1) {
        if (c2.contains(obj)) {
            result.add(obj);
        }
    }

    return result;
}

From source file:Main.java

/**
 * Ensures that all elements of the given collection can be cast to a desired type.
 * // ww  w. j  a v a2  s  . c  o m
 * @param collection the collection to check
 * @param type the desired type
 * @return a collection of the desired type
 * @throws ClassCastException if an element cannot be cast to the desired type
 */
@SuppressWarnings("unchecked")
public static <E> Collection<E> checkCollection(Collection<?> collection, Class<E> type) {
    if (DEBUG) {
        Collection<E> copy = new ArrayList<E>(collection.size());
        for (Object element : collection) {
            copy.add(type.cast(element));
        }
        return copy;
    }
    return (Collection<E>) collection;
}

From source file:de.ingrid.portal.security.util.SecurityHelper.java

/**
 * Get merged permissions from user and his roles.
 * /*from   w  ww.  j  a  v  a2  s.c o  m*/
 * @param p
 * @param roles
 * @param permissionManager
 * @return
 */
public static Permissions getMergedPermissions(Principal p, Collection<Role> roles,
        PermissionManager permissionManager) {

    Permissions result = null;
    Collection<Principal> principals = new ArrayList<Principal>();
    principals.add(p);
    Iterator<Role> roleIterator = roles.iterator();
    while (roleIterator.hasNext()) {
        // check for role based permission to show the user
        Role role = roleIterator.next();
        principals.add(role);
    }
    result = permissionManager.getPermissions(principals.toArray(new Principal[principals.size()]));
    return result;
}

From source file:Main.java

/**
 * @throws IllegalArgumentException if an add operation "soft fails" (does not modify the collection)
 * @see Collection#addAll(Collection)//from ww  w  .  j a  v  a2 s .  c o m
 */
public static <E> void addAllForce(Collection<E> collection, Iterator<? extends E> add) {
    while (add.hasNext()) {
        E next = add.next();
        Preconditions.checkArgument(collection.add(next), "collection did not accept next element %s: %s", next,
                collection);
    }
}

From source file:Main.java

public static <T> Collection<? extends T> split(Collection<? extends T> d, int n) {
    Collection<T> tmp = new ArrayList<>();
    Iterator it = d.iterator();//from  w ww  .j  a  v  a  2s  .  c o m
    int k = Math.max(0, n);
    while (it.hasNext() && k > 0) {
        tmp.add((T) it.next());
        k--;
    }
    return tmp;
}