From: Illia Date: Mon, 26 Oct 2020 16:15:39 +0000 (+0200) Subject: Add LazyCollections.lazyAdd for Sets X-Git-Tag: v4.0.14~2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F86%2F93686%2F1;p=yangtools.git Add LazyCollections.lazyAdd for Sets This will be used for GenerateType field, which is set of specified getters. JIRA: MDSAL-426 Change-Id: I691aa13c88147828890e0639441f607515053af8 Signed-off-by: Illia (cherry picked from commit 7caf3d717451ea9a98b19ab58e175281b5029055) --- diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/LazyCollections.java b/common/util/src/main/java/org/opendaylight/yangtools/util/LazyCollections.java index 603039b2ff..10d53b7716 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/LazyCollections.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/LazyCollections.java @@ -9,7 +9,9 @@ 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 @@ -44,4 +46,30 @@ public final class LazyCollections { 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; + } }