Example usage for java.util Collection addAll

List of usage examples for java.util Collection addAll

Introduction

In this page you can find the example usage for java.util Collection 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:net.ontopia.topicmaps.db2tm.Execute.java

public static void main(String[] argv) throws Exception {

    // Initialize logging
    CmdlineUtils.initializeLogging();/*from ww w .j  a  v  a  2  s  .co  m*/

    // Register logging options
    CmdlineOptions options = new CmdlineOptions("Execute", argv);
    CmdlineUtils.registerLoggingOptions(options);
    OptionsListener ohandler = new OptionsListener();
    options.addLong(ohandler, "tm", 't', true);
    options.addLong(ohandler, "baseuri", 'b', true);
    options.addLong(ohandler, "out", 'o', true);
    options.addLong(ohandler, "relations", 'r', true);
    options.addLong(ohandler, "force-rescan", 'f', true);

    // Parse command line options
    try {
        options.parse();
    } catch (CmdlineOptions.OptionsException e) {
        System.err.println("Error: " + e.getMessage());
        System.exit(1);
    }

    // Get command line arguments
    String[] args = options.getArguments();

    if (args.length < 2) {
        usage();
        System.exit(3);
    }

    // Arguments
    String operation = args[0];
    String cfgfile = args[1];

    if (!"add".equals(operation) && !"sync".equals(operation) && !"remove".equals(operation)) {
        usage();
        System.err.println("Operation '" + operation + "' not supported.");
        System.exit(3);
    }

    try {
        // Read mapping file
        log.debug("Reading relation mapping file {}", cfgfile);
        RelationMapping mapping = RelationMapping.read(new File(cfgfile));

        // open topic map
        String tmurl = ohandler.tm;
        log.debug("Opening topic map {}", tmurl);
        TopicMapIF topicmap;
        if (tmurl == null || "tm:in-memory:new".equals(tmurl))
            topicmap = new InMemoryTopicMapStore().getTopicMap();
        else if ("tm:rdbms:new".equals(tmurl))
            topicmap = new RDBMSTopicMapStore().getTopicMap();
        else {
            TopicMapReaderIF reader = ImportExportUtils.getReader(tmurl);
            topicmap = reader.read();
        }
        TopicMapStoreIF store = topicmap.getStore();

        // base locator
        String outfile = ohandler.out;
        LocatorIF baseloc = (outfile == null ? store.getBaseAddress() : new URILocator(new File(outfile)));
        if (baseloc == null && tmurl != null)
            baseloc = (ohandler.baseuri == null ? new URILocator(tmurl) : new URILocator(ohandler.baseuri));

        // figure out which relations to actually process
        Collection<String> relations = null;
        if (ohandler.relations != null) {
            String[] relnames = StringUtils.split(ohandler.relations, ",");
            if (relnames.length > 0) {
                relations = new HashSet<String>(relnames.length);
                relations.addAll(Arrays.asList(relnames));
            }
        }

        try {
            // Process data sources in mapping
            if ("add".equals(operation))
                Processor.addRelations(mapping, relations, topicmap, baseloc);
            else if ("sync".equals(operation)) {
                boolean rescan = ohandler.forceRescan != null
                        && Boolean.valueOf(ohandler.forceRescan).booleanValue();
                Processor.synchronizeRelations(mapping, relations, topicmap, baseloc, rescan);
            } else if ("remove".equals(operation))
                Processor.removeRelations(mapping, relations, topicmap, baseloc);
            else
                throw new UnsupportedOperationException("Unsupported operation: " + operation);

            // export topicmap
            if (outfile != null) {
                log.debug("Exporting topic map to {}", outfile);
                TopicMapWriterIF writer = ImportExportUtils.getWriter(new File(outfile));
                writer.write(topicmap);
            }

            // commit transaction
            store.commit();
            log.debug("Transaction committed.");
        } catch (Exception t) {
            log.error("Error occurred while running operation '" + operation + "'", t);
            // abort transaction
            store.abort();
            log.debug("Transaction aborted.");
            throw t;
        } finally {
            if (store.isOpen())
                store.close();
        }

    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (cause instanceof DB2TMException)
            System.err.println("Error: " + e.getMessage());
        else
            throw e;
    }
}

From source file:Util.java

public static <T> void addArrayToCollection(T[] array, Collection<T> collection) {
    collection.addAll(Arrays.asList(array));
}

From source file:Main.java

public static <T> void addAllValues(Collection<T> c, Map<?, T> map) {
    c.addAll(map.values());
}

From source file:Main.java

public static <T> void addAllKeys(Collection<T> c, Map<T, ?> map) {
    c.addAll(map.keySet());
}

From source file:Main.java

public static void addAllIfNotNull(Collection dest, Collection src) {
    if (src != null) {
        dest.addAll(src);
    }// www .j av a  2 s  .  co m
}

From source file:Main.java

public static <T> void replaceValuesInCollection(Collection<T> from, Collection<T> to) {
    to.clear();/*from  w  w  w .  j  av a 2s. com*/
    if (from != null) {
        to.addAll(from);
    }
}

From source file:Main.java

/**
 * Remove all object from collection to other collection
 * @param <T>/*from ww w .ja v  a  2  s . c o m*/
 * @param from source collection
 * @param to target collection
 */
public static <T> void moveAllObject(Collection<T> from, Collection<T> to) {
    if (!from.isEmpty()) {
        to.addAll(from);
        from.clear();
    }
}

From source file:Main.java

public static <T> void addAll(Collection<T> collection, Collection<T> items) {
    if (items != null) {
        collection.addAll(items);
    }//from  ww  w. j  a va  2s  .com
}

From source file:Main.java

/** adds all items from src to dst */
public static <T> void addAll(final Collection<T> src, final Collection<T> dst) {
    if (src != null)
        dst.addAll(src);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Collection<? extends Object> getUnion(Collection<? extends Object> list1,
        Collection<? extends Object> list2) {
    @SuppressWarnings("rawtypes")
    Collection union = new HashSet(list1);
    union.addAll(list2);
    return union;
}