Example usage for java.util Collections synchronizedCollection

List of usage examples for java.util Collections synchronizedCollection

Introduction

In this page you can find the example usage for java.util Collections synchronizedCollection.

Prototype

public static <T> Collection<T> synchronizedCollection(Collection<T> c) 

Source Link

Document

Returns a synchronized (thread-safe) collection backed by the specified collection.

Usage

From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java

/**
 * Clean up elements in slim3GtxObjMapThreadLocal member field to only ones corresponding to 
 * active GlobalTransaction instances in order to prevent memory leak. 
 * Thereby, caution needs to be required to call this. There will be timing that GlobalTransaction 
 * instances is inactive but the transaction on that GlobalTransaction instances is not completed yet. 
 * For an example, GlobalTransaction object may become inactive after encountering 
 * ConcurrentModificationException exception at its commit before corresponding transaction becomes  
 * complete state after roll back attempt for that exception. 
 *///from  w  w  w  .  j ava  2s .  c  o  m
protected void sweepSlim3GtxObjMapThreadLocal() {
    Collection<GlobalTransaction> globalTransactions = Collections
            .synchronizedCollection(Datastore.getActiveGlobalTransactions());
    Set<String> gtxIdSet = new HashSet<String>();
    for (GlobalTransaction gtxObj : globalTransactions) {
        gtxIdSet.add(gtxObj.getId());
    } // for
    synchronized (slim3GtxObjMapThreadLocal) {
        if (gtxIdSet.size() > 0) {
            //TODO I need to test the case that slim3GtxObjMapThreadLocal contains less elements than gtxIdSet
            slim3GtxObjMapThreadLocal.get().keySet().retainAll(gtxIdSet);
        } else {
            slim3GtxObjMapThreadLocal.get().clear();
        }
    } // synchronized
}

From source file:org.corpus_tools.salt.util.SaltUtil.java

/**
 * This method stores a Salt model in the dot syntax (see:
 * http://www.graphviz.org/) in a file. The stored dot graph can be
 * visualized via the Graphviz toolbox (see: http://www.graphviz.org/) into
 * a bunch of graphical formats like jpeg, png, svg etc.. <br/>
 * In case of a {@link SaltProject} like the following is stored:
 * /* w w  w .  j a  va  2s .c om*/
 * <pre>
 * |-----------------------------------------------|
 * | SaltProject:                                  |
 * |-----------------------------------------------|
 * | corpus-structure 0      | corpus-structure 1  |
 * |                         |                     |
 * |          c1             |       c1            |
 * |        /     \          |       |             |
 * |       c2      c3        |       d1            |
 * |   /   |   \   |   \     |                     |
 * |  d1   d2  d3  d4  d5    |                     |
 * |-----------------------------------------------|
 * </pre>
 * 
 * A structure like the following is created:
 * 
 * <pre>
 * 
 *  |-0
 *  | |-c1
 *  | | |-c2
 *  | |   |-d1.dot
 *  | |   |-d2.dot
 *  | |   |-d3.dot
 *  | | |-c3
 *  | |   |-d4.dot
 *  | |   |-d5.dot
 *  | |-0.dot
 *  |-1
 *    |-c1
 *    | |-d1.dot
 *    |-1.dot
 * </pre>
 */
public static void save_DOT(Object saltObject, URI location) {
    if (location == null) {
        throw new SaltResourceException(
                "Exception in storing Salt model to dot file, because no uri was given.");
    }
    if (saltObject == null) {
        throw new SaltResourceException(
                "Exception in storing Salt model to dot file. Cannot write more than one content per file.");
    }

    if (saltObject instanceof SCorpus) {
        SCorpus sCorpus = (SCorpus) saltObject;
        if (sCorpus.getGraph() != null) {
            saltObject = sCorpus.getGraph();
        } else {
            throw new SaltResourceException(
                    "Cannot save Salt model to DOT format, because the given " + SCorpus.class.getSimpleName()
                            + " is not part of a " + SCorpusGraph.class.getSimpleName() + " container");
        }
    } else if (saltObject instanceof SDocument) {
        SDocument sDocument = (SDocument) saltObject;
        if (sDocument.getDocumentGraph() != null) {
            saltObject = sDocument.getDocumentGraph();
        } else {
            throw new SaltResourceException(
                    "Cannot save Salt model to DOT format, because the given " + SDocument.class.getSimpleName()
                            + " does not contain a " + SDocumentGraph.class.getSimpleName() + " content");
        }
    }

    // if content is a SDocumentGraph or SCorpusGraph, outputURI does not
    // have to be changed
    if (saltObject instanceof SCorpusGraph) {
        saveCorpusGraph_DOT((SCorpusGraph) saltObject, location);
    } else if (saltObject instanceof SDocumentGraph) {
        saveDocumentGraph_DOT((SDocumentGraph) saltObject, location);
    }
    // if it is a SaltProject, different URIs for the different components
    // of the project are needed
    else if (saltObject instanceof SaltProject) {
        Collection<SCorpusGraph> corpGraphs = Collections
                .synchronizedCollection(((SaltProject) saltObject).getCorpusGraphs());
        Integer corpIndex = 0;
        for (SCorpusGraph sCorpusGraph : corpGraphs) {
            URI corpUri = location;
            saveCorpusGraph_DOT(sCorpusGraph, corpUri);

            if (corpGraphs.size() > 1) {
                corpUri = corpUri.appendSegment(corpIndex.toString());
            }
            for (int docIndex = 0; docIndex < sCorpusGraph.getDocuments().size(); docIndex++) {
                SDocument sDocument = sCorpusGraph.getDocuments().get(docIndex);
                if (sDocument.getDocumentGraph() != null) {
                    URI docURI = corpUri;
                    for (String seg : sDocument.getPath().segments()) {
                        docURI = docURI.appendSegment(seg);
                    }
                    SDocumentGraph sDocGraph = sDocument.getDocumentGraph();
                    saveDocumentGraph_DOT(sDocGraph, docURI.appendFileExtension(SaltUtil.FILE_ENDING_DOT));
                    // when calling saveResource(), the sCorpusGraph content
                    // will be attached to the resource and therefore
                    // removed from list of SaltProject, therefore the graph
                    // must be artificially added again
                    sDocument.setDocumentGraph(sDocGraph);
                }
            }
            corpIndex++;
        }
    } else {
        throw new SaltResourceException("Cannot save Salt model to DOT format, because content is neither "
                + SCorpusGraph.class.getSimpleName() + ", " + SDocumentGraph.class.getSimpleName() + " nor "
                + SaltProject.class.getSimpleName() + " content. The given content is of type: '"
                + saltObject.getClass() + "'.");
    }
}