Example usage for org.apache.commons.collections Bag addAll

List of usage examples for org.apache.commons.collections Bag addAll

Introduction

In this page you can find the example usage for org.apache.commons.collections Bag addAll.

Prototype

boolean addAll(Collection<? extends E> c);

Source Link

Document

Adds all of the elements in the specified collection to this collection (optional operation).

Usage

From source file:com.pureinfo.tgirls.utils.counts.CountsJob.java

public void execute() {
    logger.debug("to process counts.");

    CountsProcess cache = CountsProcess.getInstance();
    Bag bag = new HashBag();
    synchronized (cache) {
        bag.addAll(cache.getBag());
        cache.clear();/*from  w  ww.  j a v a 2s .c om*/
    }

    ISession session = null;
    try {
        session = LocalContextHelper.currentSession();
        Set<String> bagKeySet = bag.uniqueSet();
        for (Iterator<String> iterator = bagKeySet.iterator(); iterator.hasNext();) {
            String type = null;
            try {
                type = iterator.next();
                Counts counts = new Counts();
                counts.setType(type);
                counts.setCounts(bag.getCount(type));
                counts.setCreateTime();
                session.save(counts);
            } catch (Exception e) {
                logger.error("error when save counts:" + type, e);
            }

        }

    } catch (Exception e) {
        logger.error("error occur.", e);
    } finally {
        bag.clear();
        if (session != null)
            try {
                session.close();
            } catch (Exception e) {
                logger.error("error when close session.", e);
            }
    }

}

From source file:de.innovationgate.webgate.api.jdbc.WGDatabaseImpl.java

/**
 * @param list//from www .  j av  a2  s. c o m
 * @return
 */
private Collection cloneCollection(Collection col) {

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

    if (col instanceof List) {
        List list = new ArrayList();
        list.addAll((List) col);
        return list;
    } else if (col instanceof Set) {
        Set set = new HashSet();
        set.addAll((Set) col);
        return set;
    } else if (col instanceof Bag) {
        Bag bag = new HashBag();
        bag.addAll((Bag) col);
        return bag;
    } else {
        throw new IllegalArgumentException("Cannot clone collections of type: " + col.getClass().getName());
    }

}

From source file:org.squashtest.tm.service.internal.testcase.TestCaseCallTreeFinder.java

/**
 * returns an extended graph of ALL the test cases that can be reached by the source test cases, navigating on the call test steps
 * in both directions. This graph include the callers and the called test cases, recursively including their callers and test cases
 * etc until all relevant test cases are included in the resulting graph.
 *
 * @param calledIds/*from  w ww.  j a v a2  s .  c  o  m*/
 * @return
 */
// note :  for each node, call steps are searched both upward and downward. As a result every edge will
// be found twice (once on the up, once on the down). We then must build the graph by using only one edge over two
// that's why we use a bag here, and then we halve the cardinality.
public LibraryGraph<NamedReference, SimpleNode<NamedReference>> getExtendedGraph(List<Long> sourceIds) {

    // remember which nodes were processed (so that we can spare less DB calls in the worst cases scenarios)
    Set<Long> treated = new HashSet<>();
    treated.addAll(sourceIds);

    // the temporary result variable
    Bag allpairs = new HashBag();

    // a temporary buffer variable
    List<Long> currentNodes = new LinkedList<>(sourceIds);

    // phase 1 : data collection

    while (!currentNodes.isEmpty()) {

        List<NamedReferencePair> currentPair = testCaseDao.findTestCaseCallsUpstream(currentNodes);
        currentPair.addAll(testCaseDao.findTestCaseCallsDownstream(currentNodes));

        allpairs.addAll(currentPair);

        /*
         * collect the caller ids in the currentPair for the next loop, with the following restrictions :
         * 1) if the "caller" slot of the Object[] is not null,
         * 2) if the "called" slot is not null,
         * 2) if that node was not already processed
         *
         * then we can add that id.
         */

        List<Long> nextNodes = new LinkedList<>();

        for (NamedReferencePair pair : currentPair) {

            // no caller or no called -> no need for further processing
            if (pair.getCaller() == null || pair.getCalled() == null) {
                continue;
            }

            Long callerkey = pair.getCaller().getId();
            if (!treated.contains(callerkey)) {
                nextNodes.add(callerkey);
                treated.add(callerkey);
            }

            Long calledkey = pair.getCalled().getId();
            if (!treated.contains(calledkey)) {
                nextNodes.add(calledkey);
                treated.add(calledkey);
            }

        }

        currentNodes = nextNodes;

    }

    // phase 2 : halve the number of edges as explained in the comment above the method
    // every edges will appear two times, except for "boundaries" (ie caller is null or called is null),
    // for which the cardinality is 1.
    for (NamedReferencePair pair : (Set<NamedReferencePair>) allpairs.uniqueSet()) {
        int cardinality = allpairs.getCount(pair);
        if (cardinality > 1) {
            allpairs.remove(pair, cardinality / 2);
        }
    }

    // phase 3 : make that graph
    LibraryGraph<NamedReference, SimpleNode<NamedReference>> graph = new LibraryGraph<>();

    for (NamedReferencePair pair : (Iterable<NamedReferencePair>) allpairs) {
        graph.addEdge(node(pair.getCaller()), node(pair.getCalled()));
    }

    return graph;

}