/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  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
 */
package org.opendaylight.yangtools.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Utility methods for lazily instantiated collections. These are useful for situations when we start off with an empty
 * collection (where Collections.empty() * can be reused), but need to add more things.
 */
public final class LazyCollections {
    private LazyCollections() {
        // Hidden on purpose
    }

    /**
     * Add an element to a list, potentially transforming the list.
     *
     * @param <E> the type of elements in the list
     * @param list Current list
     * @param obj Object that needs to be added
     * @return new list
     */
    public static <E> List<E> lazyAdd(final List<E> list, final E obj) {
        final List<E> ret;

        switch (list.size()) {
            case 0:
                return Collections.singletonList(obj);
            case 1:
                ret = new ArrayList<>(2);
                ret.addAll(list);
                break;
            default:
                ret = list;
        }

        ret.add(obj);
        return ret;
    }
}

@@ -418,19 +418,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -1422,24 +1422,686 @@
 %0A        %7D%0A%0A
+        ret.add(obj);%0A        return ret;%0A    %7D%0A%0A    /**%0A     * Add an element to a set, potentially transforming the set.%0A     *%0A     * @param %3CE%3E the type of elements in the set%0A     * @param set Current set%0A     * @param obj Object that needs to be added%0A     * @return new set%0A     */%0A    public static %3CE%3E Set%3CE%3E lazyAdd(final Set%3CE%3E set, final E obj) %7B%0A        final Set%3CE%3E ret;%0A%0A        switch (set.size()) %7B%0A            case 0:%0A                return Collections.singleton(obj);%0A            case 1:%0A                ret = new HashSet%3C%3E(4);%0A                ret.addAll(set);%0A                break;%0A            default:%0A                ret = set;%0A        %7D%0A%0A
         ret.

Add LazyCollections.lazyAdd for Sets