Example usage for java.util Collection iterator

List of usage examples for java.util Collection iterator

Introduction

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

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this collection.

Usage

From source file:grails.plugin.searchable.internal.SearchableUtils.java

/**
 * Is the given class an emebedded property of another domain class?
 * @param grailsDomainClass the GrailsDomainClass to check as n embedded property
 * @param grailsDomainClasses all GrailsDomainClasses
 * @return true if the given class is an embedded property of another class
 *///from   ww w.  ja  v  a2s.  c  o  m
public static boolean isEmbeddedPropertyOfOtherDomainClass(GrailsDomainClass grailsDomainClass,
        Collection grailsDomainClasses) {
    for (Iterator iter = grailsDomainClasses.iterator(); iter.hasNext();) {
        GrailsDomainClass other = (GrailsDomainClass) iter.next();
        if (grailsDomainClass == other) {
            continue;
        }
        GrailsDomainClassProperty[] domainClassProperties = other.getProperties();
        for (int i = 0; i < domainClassProperties.length; i++) {
            GrailsDomainClass referencedDomainClass = domainClassProperties[i].getReferencedDomainClass();
            if (referencedDomainClass != null && referencedDomainClass == grailsDomainClass
                    && domainClassProperties[i].isEmbedded()) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns an arbitrary element from the collection
 *//*from  w  w  w .  j ava  2s  .c o  m*/
public static <T> T getAnElement(Collection<T> coll) {
    if (coll == null) {
        throw new IllegalArgumentException("Called with null collection");
    } else if (coll.size() == 0) {
        return null;
    }
    if (coll instanceof List) {
        return ((List<T>) coll).get(0);
    }
    Iterator<T> it = coll.iterator();
    return it.next();
}

From source file:net.sf.zekr.common.util.CollectionUtils.java

/**
 * @param col collection parameter to be returned as array
 * @param methodName the method name to be called on each item. The method's signature should have no
 *           argument, and return <code>String</code>.
 * @return an array of <code>collection.eachElement.toString()</code>.
 * @throws InvocationTargetException//from   w ww .j a v  a 2 s  .c o m
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 */
public static String[] toStringArray(Collection<?> col, String methodName)
        throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    String[] s = new String[col.size()];
    int i = 0;
    for (Iterator<?> iter = col.iterator(); iter.hasNext(); i++) {
        Object element = iter.next();
        s[i] = (String) element.getClass().getMethod(methodName, new Class[] {}).invoke(element,
                new Object[] {});
    }
    return s;
}

From source file:Main.java

/**
 * Implementation of the OCL//from ww  w.j a v a 2 s.  c  om
 * <ul>
 * <li><tt>OrderedSet::at(index : Integer) : T</tt></li>
 * <li><tt>Sequence::at(index : Integer) : T</tt></li>
 * </ul>
 * operations.
 * 
 * @param self the source collection
 * @param index the 1-based (in OCL fashion) index
 * @return the object at the specified index of the source collection
 * 
 * @throws IndexOutOfBoundsException if the index is out of bounds
 */
public static <E> E at(Collection<E> self, int index) {
    index = index - 1;

    if (index < 0 || index >= self.size()) {
        throw new IndexOutOfBoundsException("index: " + (index + 1) + ", size: " //$NON-NLS-1$ //$NON-NLS-2$
                + self.size());
    }

    int curr = 0;
    for (Iterator<E> it = self.iterator(); it.hasNext();) {
        E object = it.next();
        if (curr++ == index) {
            return object;
        }
    }
    return null; // undefined
}

From source file:info.magnolia.cms.util.ContentUtil.java

/**
 * Get all subnodes recursively and add them to the nodes collection.
 * @param nodes/*from w  ww  . ja v  a 2s .c om*/
 * @param node
 * @param types
 * @return the list
 */
private static List collectAllChildren(List nodes, Content node, ItemType[] types) {
    for (int i = 0; i < types.length; i++) {
        ItemType type = types[i];

        Collection children = node.getChildren(type);
        for (Iterator iter = children.iterator(); iter.hasNext();) {
            Content child = (Content) iter.next();
            nodes.add(child);
            collectAllChildren(nodes, child, types);
        }
    }
    return nodes;
}

From source file:Main.java

public static <E> E getSmallestNotNull(final Comparator<? super E> comparator,
        final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallestNotNull(comparator, (List<? extends E>) c);
    }/*from w  ww  . ja  v a 2  s . c  om*/

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element == null) {
            continue;
        }
        if ((result == null) || (comparator.compare(element, result) < 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:Main.java

public static <E> E getGreatestNotNull(final Comparator<? super E> comparator,
        final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatestNotNull(comparator, (List<? extends E>) c);
    }/*from  ww w  . j a  va2 s  .c o  m*/

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element == null) {
            continue;
        }
        if ((result == null) || (comparator.compare(element, result) > 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:mx.edu.um.mateo.general.utils.SpringSecurityUtils.java

/**
 * Get the current user's authorities./*from w w  w . j  a  va  2  s  . co  m*/
 * 
 * @return a list of authorities (empty if not authenticated).
 */
public static Collection<GrantedAuthority> getPrincipalAuthorities() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return Collections.emptyList();
    }

    @SuppressWarnings("unchecked")
    Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>) authentication.getAuthorities();
    if (authorities == null) {
        return Collections.emptyList();
    }

    // remove the fake role if it's there
    Collection<GrantedAuthority> copy = new ArrayList<>(authorities);
    for (Iterator<GrantedAuthority> iter = copy.iterator(); iter.hasNext();) {
        if (iter.next().getAuthority().equals(NO_ROLE)) {
            iter.remove();
        }
    }

    return copy;
}

From source file:info.magnolia.cms.util.ContentUtil.java

/**
 * Get the children using a filter//from w w  w  . j  a  v a  2 s  . c  o  m
 * @param nodes collection of already found nodes
 * @param node
 * @param filter the filter to use
 * @return
 */
private static List collectAllChildren(List nodes, Content node, ContentFilter filter) {
    // get filtered sub nodes first
    Collection children = node.getChildren(filter);
    for (Iterator iter = children.iterator(); iter.hasNext();) {
        Content child = (Content) iter.next();
        nodes.add(child);
    }

    // get all children to find recursively
    Collection allChildren = node.getChildren(allwaysTrueContentFilter);

    // recursion
    for (Iterator iter = allChildren.iterator(); iter.hasNext();) {
        Content child = (Content) iter.next();
        collectAllChildren(nodes, child, filter);
    }

    return nodes;
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.manager.executionCourseManagement.SeperateExecutionCourse.java

private static void transferShifts(final ExecutionCourse originExecutionCourse,
        final ExecutionCourse destinationExecutionCourse, final List<Shift> shiftsToTransfer) {
    for (final Shift shift : shiftsToTransfer) {

        Collection<CourseLoad> courseLoads = shift.getCourseLoadsSet();
        for (Iterator<CourseLoad> iter = courseLoads.iterator(); iter.hasNext();) {
            CourseLoad courseLoad = iter.next();
            CourseLoad newCourseLoad = destinationExecutionCourse
                    .getCourseLoadByShiftType(courseLoad.getType());
            if (newCourseLoad == null) {
                newCourseLoad = new CourseLoad(destinationExecutionCourse, courseLoad.getType(),
                        courseLoad.getUnitQuantity(), courseLoad.getTotalQuantity());
            }//from ww w  .ja  va  2s.c o m
            iter.remove();
            shift.removeCourseLoads(courseLoad);
            shift.addCourseLoads(newCourseLoad);
        }

    }
}