Replaces the content of one Collection with the content of another one, clearing the source Collection first, if they are not equal. - Java java.util

Java examples for java.util:Collection Operation

Description

Replaces the content of one Collection with the content of another one, clearing the source Collection first, if they are not equal.

Demo Code

/*******************************************************************************
 * Copyright (c) 2010 SAP AG./*w w  w .j ava2  s.  c o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Emil Simeonov - initial API and implementation.
 *    Dimitar Donchev - initial API and implementation.
 *    Dimitar Tenev - initial API and implementation.
 *    Nevena Manova - initial API and implementation.
 *    Georgi Konstantinov - initial API and implementation.
 *    Jakob Spies - initial API and implementation.
 *******************************************************************************/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Main{
    /**
     * Replaces the content of one {@link Collection} with the content of another one,
     * clearing the source {@link Collection} first,
     * if they are not equal.
     * @param <T> content type
     * @param source source {@link Collection}
     * @param target target {@link Collection}
     * @param equal equality relation for <code>T</code> objects
     * @pre source != null
     * @pre target != null
     */
    public static <T> void moveContents(final Collection<T> source,
            final Collection<T> target, final Equal<T> equal) {
        if (!Checker.isEqual(source, target, equal)) {
            clearSourceAndTargetAndCopy(source, target);
        }
    }
    /**
     * Replaces the content of one {@link Collection} with the content of another one
     * without checking equality. The target <em>and source</em> {@link Collection}s 
     * are emptied before the
     * target {@link Collection} is filled.
     * @param <T> content type
     * @param source source {@link Collection}
     * @param target target {@link Collection}
     * @pre source != null
     * @pre target != null
     */
    public static <T> void clearSourceAndTargetAndCopy(
            final Collection<T> source, final Collection<T> target) {
        Nil.checkNil(source, "source"); //$NON-NLS-1$
        Nil.checkNil(target, "target"); //$NON-NLS-1$

        final Collection<T> copy = duplicate(source);

        source.clear();
        target.clear();
        target.addAll(copy);
    }
    /**
     * Returns a new {@link ArrayList} with the same contents as <code>source</code>,
     * except for the case that <code>source</code> is nil or empty, in which <code>source</code>
     * itself is returned.
     * @param <T> element type
     * @param source the {@link Collection} to clone
     * @return a new {@link ArrayList} with the same contents as <code>source</code>,
     *    except for the case that <code>source</code> is nil or empty, in which <code>source</code>
     *    itself is returned
     */
    public static <T> Collection<T> duplicate(Collection<T> source) {
        final Collection<T> result;

        if (source == null || source.isEmpty()) {
            result = source;
        } else {
            result = new ArrayList<T>(source.size());
            result.addAll(source);
        }

        return result;
    }
}

Related Tutorials