List of usage examples for java.util Collection clear
void clear();
From source file:org.nuclos.common.collection.CollectionUtils.java
/** * splits <code>iterable</code> into the objects that satisfy or don't satisfy the given predicate, respectively. * @param iterable is not changed./* ww w. ja v a 2 s . c o m*/ * @param predicate * @param collTrue on exit, contains the objects from <code>iterable</code> that satisfy the <code>predicate</code>. * @param collFalse on exit, contains the objects from <code>iterable</code> that don't satisfy the <code>predicate</code>. * @precondition iterable != null * @precondition predicate != null * @precondition collTrue != null * @precondition collFalse != null */ public static <E> void split(Iterable<E> iterable, Predicate<? super E> predicate, Collection<? super E> collTrue, Collection<? super E> collFalse) { collTrue.clear(); collFalse.clear(); for (E e : iterable) { // Unfortunately, the compiler complains about this one: // (predicate.evaluate(e) ? collTrue : collFalse).add(e); if (predicate.evaluate(e)) { collTrue.add(e); } else { collFalse.add(e); } } }
From source file:org.apache.ode.bpel.engine.cron.CronScheduler.java
public void cancelProcessCronJobs(QName pid, boolean undeployed) { assert pid != null; if (__log.isDebugEnabled()) __log.debug("Cancelling PROCESS CRON jobs for: " + pid); Collection<TerminationListener> listenersToTerminate = new ArrayList<TerminationListener>(); synchronized (_terminationListenersByPid) { Collection<TerminationListener> listeners = _terminationListenersByPid.get(pid); if (listeners != null) { listenersToTerminate.addAll(listeners); listeners.clear(); }/*w w w . ja v a 2 s . com*/ if (undeployed) { _terminationListenersByPid.remove(pid); } } // terminate existing cron jobs if there are synchronized (pid) { for (TerminationListener listener : listenersToTerminate) { listener.terminate(); } } }
From source file:org.ohmage.request.clazz.ClassSearchRequest.java
@Override public void service() { LOGGER.info("Servicing the class search request."); if (!authenticate(AllowNewAccount.NEW_ACCOUNT_DISALLOWED)) { return;//from w w w . j av a 2s. c o m } try { LOGGER.info("Checking that the user is an admin."); UserServices.instance().verifyUserIsAdmin(getUser().getUsername()); LOGGER.info("Searching for the classes that satisfy the parameters."); Collection<String> classIds = ClassServices.instance().classIdSearch(classId, className, classDescription); totalNumResults = classIds.size(); if (numToSkip >= classIds.size()) { classIds.clear(); } else if (numToReturn >= 0) { List<String> sortedClassIds = new ArrayList<String>(classIds); Collections.sort(sortedClassIds); int lastIndex = numToSkip + numToReturn; if (lastIndex > totalNumResults) { lastIndex = totalNumResults; } classIds = sortedClassIds.subList(numToSkip, lastIndex); } LOGGER.info("Gathering the detailed information about the classes."); classes.putAll(ClassServices.instance().getClassesInformation(getUser().getUsername(), classIds, null, null, null, true)); LOGGER.info("Gathering the IDs for the campaigns associated with each class."); for (Clazz clazz : classes.keySet()) { classToCampaignIdsMap.put(clazz, CampaignClassServices.instance().getCampaignIdsForClass(clazz.getId())); } } catch (ServiceException e) { e.failRequest(this); e.logException(LOGGER); } }
From source file:mitm.djigzo.web.pages.crl.CRLImport.java
public void onSuccess() throws CertificateException, NoSuchProviderException, SecurityFactoryFactoryException, CRLException, IOException, WebServiceCheckedException { importCount = new Integer(0); /*// ww w. jav a2 s . c o m * It should already be checked that the file contains certificates. */ Collection<X509CRL> crls = CRLUtils.readX509CRLs(file.getStream()); totalCount = crls.size(); Collection<X509CRL> batch = new LinkedList<X509CRL>(); for (X509CRL crl : crls) { if (accept(crl)) { batch.add(crl); if (batch.size() == maxBatchSize) { addCRLs(batch); batch.clear(); } } } /* * Check if there are still some certificates left to add (happens when the number * of certificates is not a multiple of maxBatchSize) */ if (batch.size() > 0) { addCRLs(batch); } }
From source file:ubic.gemma.persistence.service.genome.biosequence.BioSequenceDaoImpl.java
@Override public Map<Gene, Collection<BioSequence>> findByGenes(Collection<Gene> genes) { if (genes == null || genes.isEmpty()) return new HashMap<>(); Map<Gene, Collection<BioSequence>> results = new HashMap<>(); int batchSize = 500; if (genes.size() <= batchSize) { this.findByGenesBatch(genes, results); return results; }/* w w w .ja va 2s . c o m*/ Collection<Gene> batch = new HashSet<>(); for (Gene gene : genes) { batch.add(gene); if (batch.size() == batchSize) { this.findByGenesBatch(genes, results); batch.clear(); } } if (!batch.isEmpty()) { this.findByGenesBatch(genes, results); } return results; }
From source file:ubic.gemma.persistence.service.genome.biosequence.BioSequenceDaoImpl.java
@Override public Collection<BioSequence> thaw(final Collection<BioSequence> bioSequences) { if (bioSequences.isEmpty()) return new HashSet<>(); Collection<BioSequence> result = new HashSet<>(); Collection<BioSequence> batch = new HashSet<>(); for (BioSequence g : bioSequences) { batch.add(g);/* w w w.j a v a2 s. c om*/ if (batch.size() == 100) { result.addAll(this.doThawBatch(batch)); batch.clear(); } } if (!batch.isEmpty()) { result.addAll(this.doThawBatch(batch)); } return result; }
From source file:org.ambraproject.article.service.IngesterImpl.java
/** * Update an existing article by copying properties from the new one over. Note that we can't call saveOrUpdate, * since the new article is not a persistent instance, but has all the properties that we want. * <p/>/* w w w .j av a 2 s . c om*/ * See <a href="http://stackoverflow.com/questions/4779239/update-persistent-object-with-transient-object-using-hibernate">this * post on stack overflow</a> for more information * <p/> * For collections, we clear the old property and add all the new entries, relying on 'delete-orphan' to delete the * old objects. The downside of this approach is that it results in a delete statement for each entry in the old * collection, and an insert statement for each entry in the new collection. There a couple of things we could do to * optimize this: <ol> <li>Write a sql statement to delete the old entries in one go</li> <li>copy over collection * properties recursively instead of clearing the old collection. e.g. for {@link Article#assets}, instead of * clearing out the old list, we would find the matching asset by DOI and Extension, and update its properties</li> * </ol> * <p/> * Option number 2 is messy and a lot of code (I've done it before) * * @param article the new article, parsed from the xml * @param existingArticle the article pulled up from the database * @throws IngestException if there's a problem copying properties or updating */ @SuppressWarnings("unchecked") private void updateArticle(final Article article, final Article existingArticle) throws IngestException { log.debug("ReIngesting (force ingest) article: {}", existingArticle.getDoi()); //Hibernate deletes orphans after inserting the new rows, which violates a unique constraint on (doi, extension) for assets //this temporary change gets around the problem, before the old assets are orphaned and deleted hibernateTemplate.execute(new HibernateCallback() { @Override public Object doInHibernate(Session session) throws HibernateException, SQLException { session.createSQLQuery("update articleAsset " + "set doi = concat('old-',doi), " + "extension = concat('old-',extension) " + "where articleID = :articleID") .setParameter("articleID", existingArticle.getID()).executeUpdate(); return null; } }); final BeanWrapper source = new BeanWrapperImpl(article); final BeanWrapper destination = new BeanWrapperImpl(existingArticle); try { //copy properties for (final PropertyDescriptor property : destination.getPropertyDescriptors()) { final String name = property.getName(); if (!name.equals("ID") && !name.equals("created") && !name.equals("lastModified") && !name.equals("class")) { //Collections shouldn't be dereferenced but have elements added //See http://www.onkarjoshi.com/blog/188/hibernateexception-a-collection-with-cascade-all-delete-orphan-was-no-longer-referenced-by-the-owning-entity-instance/ if (Collection.class.isAssignableFrom(property.getPropertyType())) { Collection orig = (Collection) destination.getPropertyValue(name); orig.clear(); Collection sourcePropertyValue = (Collection) source.getPropertyValue(name); if (sourcePropertyValue != null) { orig.addAll((Collection) source.getPropertyValue(name)); } } else { //just set the new value destination.setPropertyValue(name, source.getPropertyValue(name)); } } } //Circular relationship in related articles for (ArticleRelationship articleRelationship : existingArticle.getRelatedArticles()) { articleRelationship.setParentArticle(existingArticle); } } catch (Exception e) { throw new IngestException("Error copying properties for article " + article.getDoi(), e); } hibernateTemplate.update(existingArticle); }
From source file:ubic.gemma.core.analysis.service.GeneMultifunctionalityPopulationServiceImpl.java
@Override public void updateMultifunctionality(Taxon taxon) { Collection<Gene> genes = geneService.loadAll(taxon); if (genes.isEmpty()) { GeneMultifunctionalityPopulationServiceImpl.log.warn("No genes found for " + taxon); return;//w ww .j a v a 2 s . c om } Map<Gene, Collection<String>> gomap = this.fetchGoAnnotations(genes); Map<Gene, Multifunctionality> mfs = this.computeMultifunctionality(gomap); GeneMultifunctionalityPopulationServiceImpl.log .info("Saving multifunctionality for " + genes.size() + " genes"); Collection<Gene> batch = new HashSet<>(); int batchSize = 200; int i = 0; for (Gene g : genes) { batch.add(g); if (batch.size() == batchSize) { this.saveBatch(batch, mfs); batch.clear(); } if (++i % 1000 == 0) { GeneMultifunctionalityPopulationServiceImpl.log.info("Updated " + i + " genes/" + genes.size()); } } if (!batch.isEmpty()) { this.saveBatch(batch, mfs); } GeneMultifunctionalityPopulationServiceImpl.log.info("Done"); }
From source file:forge.game.mana.ManaPool.java
private void convertManaColor(final byte originalColor, final byte toColor) { List<Mana> convert = new ArrayList<Mana>(); Collection<Mana> cm = floatingMana.get(originalColor); for (Mana m : cm) { convert.add(new Mana(toColor, m.getSourceCard(), m.getManaAbility())); }/*from w w w .j a v a 2 s. c om*/ cm.clear(); floatingMana.putAll(toColor, convert); owner.updateManaForView(); }
From source file:ubic.gemma.analysis.service.GeneMultifunctionalityPopulationServiceImpl.java
@Override public void updateMultifunctionality(Taxon taxon) { Collection<Gene> genes = geneService.loadAll(taxon); if (genes.isEmpty()) { log.warn("No genes found for " + taxon); return;/*ww w. java2 s . c o m*/ } Map<Gene, Collection<String>> gomap = fetchGoAnnotations(genes); Map<Gene, Multifunctionality> mfs = computeMultifunctionality(gomap); log.info("Saving multifunctionality for " + genes.size() + " genes"); Collection<Gene> batch = new HashSet<Gene>(); int batchSize = 200; int i = 0; for (Gene g : genes) { batch.add(g); if (batch.size() == batchSize) { saveBatch(batch, mfs); batch.clear(); } if (++i % 1000 == 0) { log.info("Updated " + i + " genes/" + genes.size()); } } if (!batch.isEmpty()) { saveBatch(batch, mfs); } log.info("Done"); }