org.opendaylight.yangtools.util.LazyCollections Maven / Gradle / Ivy
/*
* 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.HashSet;
import java.util.List;
import java.util.Set;
/**
* 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 the type of elements in the list
* @param list Current list
* @param obj Object that needs to be added
* @return new list
*/
public static List lazyAdd(final List list, final E obj) {
final List 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;
}
/**
* Add an element to a set, potentially transforming the set.
*
* @param the type of elements in the set
* @param set Current set
* @param obj Object that needs to be added
* @return new set
*/
public static Set lazyAdd(final Set set, final E obj) {
final Set ret;
switch (set.size()) {
case 0:
return Collections.singleton(obj);
case 1:
ret = new HashSet<>(4);
ret.addAll(set);
break;
default:
ret = set;
}
ret.add(obj);
return ret;
}
}