X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=common%2Futil%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Futil%2FImmutableOffsetMap.java;h=844086df746c1c314f3a2625dd1aa268543cc0ef;hb=4f66528ebcf1603108218b8ebf48147d8ed3ad38;hp=aadc32ac64affb913cfaa4fdd89850c400f49fc8;hpb=f069fc0c4ef1a95ef7ca81a2d83301a48bf18fb8;p=yangtools.git diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java b/common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java index aadc32ac64..844086df74 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java @@ -13,6 +13,7 @@ import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import com.google.common.collect.ImmutableMap; import com.google.common.collect.UnmodifiableIterator; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -35,6 +36,10 @@ import org.eclipse.jdt.annotation.Nullable; * a backing array. This is useful for situations where the same key set is shared across a multitude of maps, as this * class uses a global cache to share the key-to-offset mapping. * + *

+ * In case the set of keys is statically known, you can use {@link ImmutableOffsetMapTemplate} to efficiently create + * {@link ImmutableOffsetMap} instances. + * * @param the type of keys maintained by this map * @param the type of mapped values */ @@ -43,7 +48,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase extends ImmutableOffsetMap { private static final long serialVersionUID = 1L; - Ordered(final Map offsets, final V[] objects) { + Ordered(final ImmutableMap offsets, final V[] objects) { super(offsets, objects); } @@ -62,7 +67,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase extends ImmutableOffsetMap { private static final long serialVersionUID = 1L; - Unordered(final Map offsets, final V[] objects) { + Unordered(final ImmutableMap offsets, final V[] objects) { super(offsets, objects); } @@ -82,8 +87,8 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase offsets; - private final transient V[] objects; + private final transient @NonNull ImmutableMap offsets; + private final transient @NonNull V[] objects; private transient int hashCode; /** @@ -93,7 +98,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase offsets, final @NonNull V[] objects) { + ImmutableOffsetMap(final ImmutableMap offsets, final V[] objects) { this.offsets = requireNonNull(offsets); this.objects = requireNonNull(objects); checkArgument(offsets.size() == objects.length); @@ -105,19 +110,16 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase keys, V[] values) throws IOException; /** - * Create an {@link ImmutableOffsetMap} as a copy of an existing map. This - * is actually not completely true, as this method returns an - * {@link ImmutableMap} for empty and singleton inputs, as those are more - * memory-efficient. This method also recognizes {@link ImmutableOffsetMap} - * on input, and returns it back without doing anything else. It also - * recognizes {@link MutableOffsetMap} (as returned by - * {@link #toModifiableMap()}) and makes an efficient copy of its contents. - * All other maps are converted to an {@link ImmutableOffsetMap} with the - * same iteration order as input. + * Create an {@link ImmutableOffsetMap} as a copy of an existing map. This is actually not completely true, as this + * method returns an {@link ImmutableMap} for empty and singleton inputs, as those are more memory-efficient. This + * method also recognizes {@link ImmutableOffsetMap} and {@link SharedSingletonMap} on input, and returns it back + * without doing anything else. It also recognizes {@link MutableOffsetMap} (as returned by + * {@link #toModifiableMap()}) and makes an efficient copy of its contents. All other maps are converted to an + * {@link ImmutableOffsetMap} with the same iteration order as input. * - * @param map - * Input map, may not be null. + * @param map Input map, may not be null. * @return An isolated, immutable copy of the input map + * @throws NullPointerException if {@code map} or any of its elements is null. */ public static @NonNull Map orderedCopyOf(final @NonNull Map map) { final Map common = commonCopy(map); @@ -132,7 +134,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase offsets = OffsetMapCache.orderedOffsets(map.keySet()); + final ImmutableMap offsets = OffsetMapCache.orderedOffsets(map.keySet()); @SuppressWarnings("unchecked") final V[] array = (V[]) new Object[offsets.size()]; for (Entry e : map.entrySet()) { @@ -143,19 +145,16 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase @NonNull Map unorderedCopyOf(final @NonNull Map map) { final Map common = commonCopy(map); @@ -163,14 +162,13 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase e = map.entrySet().iterator().next(); return SharedSingletonMap.unorderedOf(e.getKey(), e.getValue()); } - final Map offsets = OffsetMapCache.unorderedOffsets(map.keySet()); + final ImmutableMap offsets = OffsetMapCache.unorderedOffsets(map.keySet()); @SuppressWarnings("unchecked") final V[] array = (V[]) new Object[offsets.size()]; for (Entry e : map.entrySet()) { @@ -284,8 +282,8 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase implements UnmodifiableMapPhase it = offsets.keySet().iterator(); int offset = 0; while (it.hasNext()) { - sb.append(it.next()); - sb.append('='); - sb.append(objects[offset++]); + sb.append(it.next()).append('=').append(objects[offset++]); if (it.hasNext()) { sb.append(", "); @@ -342,11 +338,11 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase offsets() { + final @NonNull ImmutableMap offsets() { return offsets; } - final V[] objects() { + final @NonNull V[] objects() { return objects; } @@ -354,7 +350,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase> iterator() { final Iterator> it = offsets.entrySet().iterator(); - return new UnmodifiableIterator>() { + return new UnmodifiableIterator<>() { @Override public boolean hasNext() { return it.hasNext(); @@ -385,7 +381,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase implements UnmodifiableMapPhase map, final Field field, final Object value) - throws IOException { + @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", + justification = "https://github.com/spotbugs/spotbugs/issues/811") + private static void setField(final @NonNull ImmutableOffsetMap map, final @NonNull Field field, + final Object value) throws IOException { try { field.set(map, value); } catch (IllegalArgumentException | IllegalAccessException e) { @@ -407,7 +405,7 @@ public abstract class ImmutableOffsetMap implements UnmodifiableMapPhase keys = new ArrayList<>(s);