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:com.tussle.main.Utility.java

public static ProjectionVector combineProjections(Collection<ProjectionVector> vectors) {
    Iterator<ProjectionVector> i = vectors.iterator();
    if (vectors.size() == 0)
        return null;
    if (vectors.size() == 1)
        return i.next();
    ProjectionVector p0 = i.next();//w w  w . ja v a 2 s . co m
    ProjectionVector p1 = i.next();
    //Get bordering unit vectors
    double cos0 = p0.xNorm();
    double sin0 = p0.yNorm();
    double cos1 = p1.xNorm();
    double sin1 = p1.yNorm();
    //zeroth on the right, first on the left
    if (cos0 * sin1 < cos1 * sin0) {
        double tmpcos = cos1;
        double tmpsin = sin1;
        cos1 = cos0;
        sin1 = sin0;
        cos0 = tmpcos;
        sin0 = tmpsin;
    }
    while (i.hasNext()) {
        ProjectionVector next = i.next();
        double nextcos = next.xNorm();
        double nextsin = next.yNorm();
        if (nextcos * sin0 >= cos0 * nextsin && cos1 * nextsin >= nextcos * sin1) {
            //Case 0: Within cross product bounds
        } else if (nextcos * sin0 >= cos0 * nextsin) {
            //Case 1: Over the left, extend those bounds
            cos1 = nextcos;
            sin1 = nextsin;
        } else if (cos1 * nextsin >= nextcos * sin1) {
            //Case 2: Over the right, extend those bounds
            cos0 = nextcos;
            sin0 = nextsin;
        } else {
            //Case 3: something went horribly wrong
            return null;
        }
    }
    //Now... project all vectors onto the sum of the borders.
    double sumcos = cos0 + cos1;
    double sumsin = sin0 + sin1;
    double len = FastMath.hypot(sumcos, sumsin);
    if (len == 0)
        return null;
    sumcos /= len;
    sumsin /= len;
    double maxlen = Double.NEGATIVE_INFINITY;
    for (ProjectionVector v : vectors) {
        double scalarProj = (v.xComp() * sumcos + v.yComp() * sumsin) / (sumcos * sumcos + sumsin * sumsin);
        if (scalarProj > maxlen)
            maxlen = scalarProj;
    }
    return new ProjectionVector(sumcos, sumsin, maxlen);
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.student.ReadStudentGroupInformation.java

@Atomic
public static ISiteComponent run(String studentGroupCode) throws FenixServiceException {
    check(RolePredicates.STUDENT_PREDICATE);

    InfoSiteStudentGroup infoSiteStudentGroup = new InfoSiteStudentGroup();
    StudentGroup studentGroup = null;//  w  w  w .  j a  v a  2  s . c  o  m
    Grouping grouping = null;
    Collection groupAttendsList = null;

    studentGroup = FenixFramework.getDomainObject(studentGroupCode);

    if (studentGroup == null) {
        return null;
    }

    List studentGroupInformationList = new ArrayList();
    grouping = studentGroup.getGrouping();
    groupAttendsList = studentGroup.getAttendsSet();

    Iterator iter = groupAttendsList.iterator();
    InfoSiteStudentInformation infoSiteStudentInformation = null;
    Attends attend = null;

    while (iter.hasNext()) {
        infoSiteStudentInformation = new InfoSiteStudentInformation();

        attend = (Attends) iter.next();

        infoSiteStudentInformation.setNumber(attend.getRegistration().getNumber());

        infoSiteStudentInformation.setName(attend.getRegistration().getPerson().getName());

        infoSiteStudentInformation.setEmail(attend.getRegistration().getPerson().getEmail());

        infoSiteStudentInformation.setUsername(attend.getRegistration().getPerson().getUsername());

        studentGroupInformationList.add(infoSiteStudentInformation);

    }

    Collections.sort(studentGroupInformationList, new BeanComparator("number"));
    infoSiteStudentGroup.setInfoSiteStudentInformationList(studentGroupInformationList);
    infoSiteStudentGroup.setInfoStudentGroup(
            InfoStudentGroupWithAttendsAndGroupingAndShift.newInfoFromDomain(studentGroup));

    if (grouping.getMaximumCapacity() != null) {

        int vagas = grouping.getMaximumCapacity().intValue() - groupAttendsList.size();

        infoSiteStudentGroup.setNrOfElements(Integer.valueOf(vagas));
    } else {
        infoSiteStudentGroup.setNrOfElements("Sem limite");
    }

    return infoSiteStudentGroup;
}

From source file:net.ontopia.topicmaps.entry.XMLConfigSource.java

private static TopicMapSourceManager createRepository(Collection<TopicMapSourceIF> sources) {
    // assign default source ids and titles
    int counter = 1;
    Iterator<TopicMapSourceIF> iter = sources.iterator();
    while (iter.hasNext()) {
        TopicMapSourceIF source = iter.next();
        if (source.getId() == null && source.supportsCreate()) {
            String newId = source.getClass().getName() + "-" + (counter++);
            source.setId(newId);/*w  w  w.ja  va 2s. c om*/
            if (source.getTitle() == null)
                source.setTitle(newId);
        }
    }
    return new TopicMapSourceManager(sources);
}

From source file:EditBinFile.java

private static void printHelp(Options options) {

    Collection list = options.getOptions();
    Iterator iterator = list.iterator();

    // while loop
    String tmp = "";
    Option otmp;/*from   w ww .  ja v a  2s. c om*/
    while (iterator.hasNext()) {
        otmp = (Option) iterator.next();
        System.out.println(String.format("%-15s %s", "-" + otmp.getOpt(), otmp.getDescription()));
    }
}

From source file:com.stratio.deep.es.utils.UtilES.java

/**
 * converts from an entity class with deep's anotations to JSONObject.
 *
 * @param t   an instance of an object of type T to convert to JSONObject.
 * @param <T> the type of the object to convert.
 * @return the provided object converted to JSONObject.
 * @throws IllegalAccessException// w  w  w .j  a  va2s. c  o  m
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static <T> JSONObject getJsonFromObject(T t)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    Field[] fields = AnnotationUtils.filterDeepFields(t.getClass());

    JSONObject json = new JSONObject();

    for (Field field : fields) {
        Method method = Utils.findGetter(field.getName(), t.getClass());
        Object object = method.invoke(t);
        if (object != null) {
            if (Collection.class.isAssignableFrom(field.getType())) {
                Collection c = (Collection) object;
                Iterator iterator = c.iterator();
                List<JSONObject> innerJsonList = new ArrayList<>();

                while (iterator.hasNext()) {
                    innerJsonList.add(getJsonFromObject((IDeepType) iterator.next()));
                }
                json.put(AnnotationUtils.deepFieldName(field), innerJsonList);
            } else if (IDeepType.class.isAssignableFrom(field.getType())) {
                json.put(AnnotationUtils.deepFieldName(field), getJsonFromObject((IDeepType) object));
            } else {
                json.put(AnnotationUtils.deepFieldName(field), object);
            }
        }
    }

    return json;
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

private static void assignCollectionValue(Method getter, Object sourceValue, Class destType, Method setter,
        Object destInstance) {/*from  www.j  a  va2  s . co m*/
    System.out.println("*** Collection found, resolving its payload type...");
    if (sourceValue == null || ((Collection) sourceValue).isEmpty())
        return;

    Class sourceArgClass = detectSourceCollectionPayload(getter);
    Class destArgClass = detectDestCollectionPayload(setter);

    if (sourceArgClass != null && destArgClass != null) {
        System.out.println("Type sorted, populating values...");
        Collection sourceItems = (Collection) sourceValue;
        Iterator it = sourceItems.iterator();
        Collection destItems; // = null;
        switch (destType.getName()) {
        case "java.util.List":
            destItems = createListOfType(destArgClass);
            break;
        case "java.util.Set":
            destItems = createSetOfType(destArgClass);
            break;
        default:
            System.out.println("4: Unrecognized collection, can't populate values");
            return;
        }

        while (it.hasNext()) {
            Object element = transposeModel(sourceArgClass, destArgClass, it.next());
            destItems.add(element);
        }
        try {
            setter.invoke(destInstance, destItems);

        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    System.out.println("*** done");
}

From source file:Main.java

/**
 * Make the log more meanings//from  www . ja v a 2 s.  c  o  m
 * 
 * @param <E>
 * @param collec
 * @return
 */
public static <E> String toLog(String separator, Collection<E> collec) {
    // using StringBuffer instead of String because expect there are many append operation
    StringBuffer sb = new StringBuffer();

    if (collec == null) {
        return null;
    }
    if (collec.isEmpty()) {
        return collec.toString();
    }
    sb.append("[");
    for (Iterator<E> iterator = collec.iterator(); iterator.hasNext();) {
        E value = iterator.next();
        sb.append(separator).append(toString4Log(value)).append(separator);
        if (iterator.hasNext()) {
            sb.append(", ");
        }
    }

    sb.append("]");
    return sb.toString();
}

From source file:com.stratio.deep.es.utils.UtilES.java

/**
 * converts from an entity class with deep's anotations to JSONObject.
 *
 * @param t   an instance of an object of type T to convert to JSONObject.
 * @param <T> the type of the object to convert.
 * @return the provided object converted to JSONObject.
 * @throws IllegalAccessException//www  .ja v  a 2 s.c  om
 * @throws InstantiationException
 * @throws InvocationTargetException
 */
public static <T> LinkedMapWritable getLinkedMapWritableFromObject(T t)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    Field[] fields = AnnotationUtils.filterDeepFields(t.getClass());

    LinkedMapWritable linkedMapWritable = new LinkedMapWritable();

    for (Field field : fields) {
        Method method = Utils.findGetter(field.getName(), t.getClass());
        Object object = method.invoke(t);
        if (object != null) {
            if (Collection.class.isAssignableFrom(field.getType())) {
                Collection c = (Collection) object;
                Iterator iterator = c.iterator();
                List<LinkedMapWritable> innerJsonList = new ArrayList<>();

                while (iterator.hasNext()) {
                    innerJsonList.add(getLinkedMapWritableFromObject((IDeepType) iterator.next()));
                }
                // linkedMapWritable.put(new Text(AnnotationUtils.deepFieldName(field)), new
                // LinkedMapWritable[innerJsonList.size()]);
            } else if (IDeepType.class.isAssignableFrom(field.getType())) {
                linkedMapWritable.put(new Text(AnnotationUtils.deepFieldName(field)),
                        getLinkedMapWritableFromObject((IDeepType) object));
            } else {
                linkedMapWritable.put(new Text(AnnotationUtils.deepFieldName(field)),
                        getWritableFromObject(object));
            }
        }
    }

    return linkedMapWritable;
}

From source file:org.codehaus.groovy.grails.plugins.searchable.SearchableUtils.java

private static boolean isOrContains(String thing, final Object value) {
    Collection values = null;
    if (value instanceof Collection) {
        values = (Collection) value;
    } else {/*from  w ww  .j a  v  a  2s  .co m*/
        values = new HashSet() {
            {
                add(value);
            }
        };
    }
    for (Iterator iter = values.iterator(); iter.hasNext();) {
        String v = (String) iter.next();
        if (!PatternUtils.hasWildcards(v)) {
            if (v.equals(thing)) {
                return true;
            }
        } else {
            Pattern pattern = PatternUtils.makePatternFromWilcardString(v);
            if (pattern.matcher(thing).matches()) {
                return true;
            }
        }
    }
    return false;
}