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:org.enerj.apache.commons.collections.TestPredicateUtils.java

public void testAnyPredicate() {
    assertFalse(PredicateUtils.anyPredicate(new Predicate[] {}).evaluate(null));
    assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.truePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(false, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate() }).evaluate(null));
    Collection coll = new ArrayList();
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null));
    coll.clear();//from www. j a  v a 2  s . c om
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    assertEquals(false, PredicateUtils.anyPredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.falsePredicate());
    assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.truePredicate());
    assertTrue(PredicateUtils.anyPredicate(coll).evaluate(null));
    coll.clear();
    assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
}

From source file:org.enerj.apache.commons.collections.TestPredicateUtils.java

public void testNonePredicate() {
    assertTrue(PredicateUtils.nonePredicate(new Predicate[] {}).evaluate(null));
    assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.truePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(true, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate() }).evaluate(null));
    Collection coll = new ArrayList();
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null));
    coll.clear();//from   w  ww . j a  v a 2 s  . c  o m
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    assertEquals(true, PredicateUtils.nonePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.falsePredicate());
    assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.truePredicate());
    assertFalse(PredicateUtils.nonePredicate(coll).evaluate(null));
    coll.clear();
    assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
}

From source file:org.enerj.apache.commons.collections.TestPredicateUtils.java

public void testOnePredicate() {
    assertFalse(PredicateUtils.onePredicate(new Predicate[] {}).evaluate(null));
    assertEquals(false, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.truePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(false, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(true, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.truePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate() }).evaluate(null));
    assertEquals(true, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.truePredicate(), PredicateUtils.falsePredicate() }).evaluate(null));
    assertEquals(true, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.truePredicate() }).evaluate(null));
    assertEquals(false, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.falsePredicate(),
            PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate() }).evaluate(null));
    Collection coll = new ArrayList();
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.truePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
    coll.clear();//from  w w w .  ja  v  a 2  s  .c  o m
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.truePredicate());
    assertEquals(true, PredicateUtils.onePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    coll.add(PredicateUtils.falsePredicate());
    assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.falsePredicate());
    assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
    coll.clear();
    coll.add(PredicateUtils.truePredicate());
    assertTrue(PredicateUtils.onePredicate(coll).evaluate(null));
    coll.clear();
    assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
}

From source file:ubc.pavlab.aspiredb.server.service.LabelServiceTest.java

@Test
public void testMultipleUsersCreateSameLabelName() {

    Collection<LabelValueObject> lvos = null;

    super.runAsAdmin();

    LabelValueObject lvo = new LabelValueObject();

    lvo.setColour("red");
    lvo.setName("blah");
    lvo.setIsShown(true);/*from   ww w  .j  av  a2s .com*/

    // Admin created subject, try adding label
    Collection<Long> subjectIds = new ArrayList<Long>();
    subjectIds.add(subjectId);

    lvo = subjectService.addLabel(subjectIds, lvo);
    assertNotNull(lvo);
    Label l = labelDao.load(lvo.getId());
    aclUtil.checkHasAcl(l);
    assertTrue(SecurityUtil.getCurrentUsername() + " owns the label", securityService.isOwnedByCurrentUser(l));
    assertTrue(SecurityUtil.getCurrentUsername() + " can edit the label", securityService.isEditable(l));

    // Now let's try a normal user, try adding label
    super.runAsUser(this.username);

    assertFalse(SecurityUtil.getCurrentUsername() + " does not own the label",
            securityService.isOwnedByCurrentUser(l));
    Subject subject = persistentTestObjectHelper.createDetachedIndividualObject("userSubject");
    Long userSubjectId = subject.getId();
    subjectIds.clear();
    subjectIds.add(userSubjectId);
    try {
        // try adding admin label as a normal user
        lvo = subjectService.addLabel(subjectIds, l.toValueObject());
        fail("User can not use admin label");
    } catch (AccessDeniedException e) {

    }

    try {
        securityService.isEditable(l);
        fail(SecurityUtil.getCurrentUsername() + " can not edit the label");
    } catch (AccessDeniedException e) {

    }

    // try admin delete
    super.runAsAdmin();
    lvos = persistentTestObjectHelper.getLabelsForSubject(subjectId);
    assertEquals(1, lvos.size());
    try {
        labelService.deleteSubjectLabel(lvos.iterator().next());
    } catch (AccessDeniedException e) {
        fail("Admin created label");
    }

}

From source file:forge.game.mana.ManaPool.java

public final List<Mana> clearPool(boolean isEndOfPhase) {
    // isEndOfPhase parameter: true = end of phase, false = mana drain effect
    List<Mana> cleared = new ArrayList<Mana>();
    if (floatingMana.isEmpty()) {
        return cleared;
    }//from   ww w .j  a  v  a 2 s .  c  om

    if (isEndOfPhase
            && owner.getGame().getStaticEffects().getGlobalRuleChange(GlobalRuleChange.manapoolsDontEmpty)) {
        return cleared;
    }

    final boolean convertToColorless = owner.hasKeyword("Convert unused mana to Colorless");

    final List<Byte> keys = Lists.newArrayList(floatingMana.keySet());
    if (isEndOfPhase) {
        for (final Byte c : Lists.newArrayList(keys)) {
            final String captName = StringUtils.capitalize(MagicColor.toLongString(c));
            if (owner.hasKeyword(
                    captName + " mana doesn't empty from your mana pool as steps and phases end.")) {
                keys.remove(c);
            }
        }
    }
    if (convertToColorless) {
        keys.remove(Byte.valueOf(MagicColor.COLORLESS));
    }

    for (Byte b : keys) {
        Collection<Mana> cm = floatingMana.get(b);
        if (isEndOfPhase && !owner.getGame().getPhaseHandler().is(PhaseType.CLEANUP)) {
            final List<Mana> pMana = new ArrayList<Mana>();
            for (final Mana mana : cm) {
                if (mana.getManaAbility() != null && mana.getManaAbility().isPersistentMana()) {
                    pMana.add(mana);
                }
            }
            cm.removeAll(pMana);
            if (convertToColorless) {
                convertManaColor(b, MagicColor.COLORLESS);
                cm.addAll(pMana);
            } else {
                cleared.addAll(cm);
                cm.clear();
                floatingMana.putAll(b, pMana);
            }
        } else {
            if (convertToColorless) {
                convertManaColor(b, MagicColor.COLORLESS);
            } else {
                cleared.addAll(cm);
                cm.clear();
            }
        }
    }

    owner.updateManaForView();
    owner.getGame().fireEvent(new GameEventManaPool(owner, EventValueChangeType.Cleared, null));
    return cleared;
}

From source file:org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler.java

/**
 * Filters the {@code filterTarget} object (which must be either a collection, array,
 * or stream), by evaluating the supplied expression.
 * <p>/* www. ja  v a  2  s  . co  m*/
 * If a {@code Collection} is used, the original instance will be modified to contain
 * the elements for which the permission expression evaluates to {@code true}. For an
 * array, a new array instance will be returned.
 */
@SuppressWarnings("unchecked")
public Object filter(Object filterTarget, Expression filterExpression, EvaluationContext ctx) {
    MethodSecurityExpressionOperations rootObject = (MethodSecurityExpressionOperations) ctx.getRootObject()
            .getValue();
    final boolean debug = logger.isDebugEnabled();
    List retainList;

    if (debug) {
        logger.debug("Filtering with expression: " + filterExpression.getExpressionString());
    }

    if (filterTarget instanceof Collection) {
        Collection collection = (Collection) filterTarget;
        retainList = new ArrayList(collection.size());

        if (debug) {
            logger.debug("Filtering collection with " + collection.size() + " elements");
        }

        if (permissionCacheOptimizer != null) {
            permissionCacheOptimizer.cachePermissionsFor(rootObject.getAuthentication(), collection);
        }

        for (Object filterObject : (Collection) filterTarget) {
            rootObject.setFilterObject(filterObject);

            if (ExpressionUtils.evaluateAsBoolean(filterExpression, ctx)) {
                retainList.add(filterObject);
            }
        }

        if (debug) {
            logger.debug("Retaining elements: " + retainList);
        }

        collection.clear();
        collection.addAll(retainList);

        return filterTarget;
    }

    if (filterTarget.getClass().isArray()) {
        Object[] array = (Object[]) filterTarget;
        retainList = new ArrayList(array.length);

        if (debug) {
            logger.debug("Filtering array with " + array.length + " elements");
        }

        if (permissionCacheOptimizer != null) {
            permissionCacheOptimizer.cachePermissionsFor(rootObject.getAuthentication(), Arrays.asList(array));
        }

        for (Object o : array) {
            rootObject.setFilterObject(o);

            if (ExpressionUtils.evaluateAsBoolean(filterExpression, ctx)) {
                retainList.add(o);
            }
        }

        if (debug) {
            logger.debug("Retaining elements: " + retainList);
        }

        Object[] filtered = (Object[]) Array.newInstance(filterTarget.getClass().getComponentType(),
                retainList.size());
        for (int i = 0; i < retainList.size(); i++) {
            filtered[i] = retainList.get(i);
        }

        return filtered;
    }

    if (filterTarget instanceof Stream) {
        final Stream<?> original = (Stream<?>) filterTarget;

        return original.filter(filterObject -> {
            rootObject.setFilterObject(filterObject);
            return ExpressionUtils.evaluateAsBoolean(filterExpression, ctx);
        }).onClose(original::close);
    }

    throw new IllegalArgumentException(
            "Filter target must be a collection, array, or stream type, but was " + filterTarget);
}

From source file:org.orcid.persistence.dao.impl.ProfileDaoImpl.java

private void removeChildren(String orcid, Collection<? extends BaseEntity<?>> entities, String orcidPath) {
    if (entities != null && !entities.isEmpty()) {
        entityManager.createQuery("delete from " + entities.iterator().next().getClass().getName() + " where "
                + orcidPath + " = :orcid").setParameter("orcid", orcid).executeUpdate();
        entities.clear();
    }//from  ww w . j  ava  2s. c om
}

From source file:ubic.gemma.persistence.service.expression.designElement.CompositeSequenceDaoImpl.java

@Override
public Collection<CompositeSequence> load(Collection<Long> ids) {

    if (ids == null || ids.size() == 0) {
        return new HashSet<>();
    }//from w  ww  .  ja  v a 2  s . c  o  m

    //language=HQL
    final String queryString = "select cs from CompositeSequence cs where cs.id in (:ids)";
    org.hibernate.Query queryObject = this.getSessionFactory().getCurrentSession().createQuery(queryString);
    int batchSize = 2000;
    Collection<Long> batch = new HashSet<>();
    Collection<CompositeSequence> results = new HashSet<>();
    for (Long id : ids) {
        batch.add(id);

        if (batch.size() == batchSize) {
            queryObject.setParameterList("ids", batch);
            //noinspection unchecked
            results.addAll(queryObject.list());
            batch.clear();
        }
    }

    // tail end.
    if (batch.size() > 0) {
        queryObject.setParameterList("ids", batch);
        //noinspection unchecked
        results.addAll(queryObject.list());
    }
    return results;
}

From source file:org.apache.shindig.gadgets.http.HttpResponse.java

/**
 * Attempts to determine the encoding of the body. If it can't be determined, we use
 * DEFAULT_ENCODING instead./*from w w  w  .jav a  2 s  .c om*/
 *
 * @return The detected encoding or DEFAULT_ENCODING.
 */
private static Charset getAndUpdateEncoding(Multimap<String, String> headers, byte[] body) {
    if (body == null || body.length == 0) {
        return DEFAULT_ENCODING;
    }

    Collection<String> values = headers.get("Content-Type");
    if (!values.isEmpty()) {
        String contentType = values.iterator().next();
        String[] parts = StringUtils.split(contentType, ';');
        if (parts == null || parts.length == 0 || BINARY_CONTENT_TYPES.contains(parts[0])) {
            return DEFAULT_ENCODING;
        }
        if (parts.length == 2) {
            int offset = parts[1].toLowerCase().indexOf("charset=");
            if (offset != -1) {
                String charset = parts[1].substring(offset + 8).toUpperCase();
                // Some servers include quotes around the charset:
                //   Content-Type: text/html; charset="UTF-8"
                if (charset.length() >= 2 && charset.startsWith("\"") && charset.endsWith("\"")) {
                    charset = charset.substring(1, charset.length() - 1);
                }

                try {
                    return charsetForName(charset);
                } catch (IllegalArgumentException e) {
                    // fall through to detection
                }
            }
        }
        Charset encoding = EncodingDetector.detectEncoding(body, fastEncodingDetection, customEncodingDetector);
        // Record the charset in the content-type header so that its value can be cached
        // and re-used. This is a BIG performance win.
        values.clear();
        values.add(contentType + "; charset=" + encoding.name());

        return encoding;
    } else {
        // If no content type was specified, we'll assume an unknown binary type.
        return DEFAULT_ENCODING;
    }
}

From source file:org.kuali.kra.budget.rates.BudgetRatesServiceImpl.java

@SuppressWarnings("unchecked")
protected void filterRates(Budget budget, Collection allAbstractInstituteRates,
        Collection filteredAbstractInstituteRates) {
    filteredAbstractInstituteRates.clear();
    Date personSalaryEffectiveDate = getBudgetPersonSalaryEffectiveDate(budget);
    getRatesForProjectDates(budget, allAbstractInstituteRates, filteredAbstractInstituteRates,
            personSalaryEffectiveDate);//w  w  w.  j a  v a 2s .  c om
    getApplicableRates(budget, allAbstractInstituteRates, filteredAbstractInstituteRates,
            personSalaryEffectiveDate);
}