From 296649cfc8a7af334db7cb18606da37dbc45c3dd Mon Sep 17 00:00:00 2001 From: nmittler Date: Mon, 13 Jul 2015 14:53:20 -0700 Subject: [PATCH] Make PrimitiveCollections generated for all primitive maps. Motivation: We should support XXXCollections methods for all primitive map types. Modifications: Removed PrimitiveCollections and added a template for XXXCollections. Result: Fixes #4001 --- .../codec/http2/DefaultHttp2Connection.java | 6 +- .../codec/http2/DefaultHttp2FrameWriter.java | 3 +- .../netty/util/collection/package-info.java | 20 ------ .../util/collection/KCollections.template} | 69 +++++++++---------- 4 files changed, 38 insertions(+), 60 deletions(-) delete mode 100644 common/src/main/java/io/netty/util/collection/package-info.java rename common/src/main/{java/io/netty/util/collection/PrimitiveCollections.java => templates/io/netty/util/collection/KCollections.template} (78%) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java index c437282f62..51b38c8b7e 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java @@ -34,9 +34,9 @@ import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_REMOTE; import static io.netty.util.internal.ObjectUtil.checkNotNull; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http2.Http2Stream.State; +import io.netty.util.collection.IntCollections; import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectMap; -import io.netty.util.collection.PrimitiveCollections; import io.netty.util.internal.EmptyArrays; import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.SystemPropertyUtil; @@ -292,7 +292,7 @@ public class DefaultHttp2Connection implements Http2Connection { private State state; private short weight = DEFAULT_PRIORITY_WEIGHT; private DefaultStream parent; - private IntObjectMap children = PrimitiveCollections.emptyIntObjectMap(); + private IntObjectMap children = IntCollections.emptyMap(); private int totalChildWeights; private int prioritizableForTree = 1; private boolean resetSent; @@ -539,7 +539,7 @@ public class DefaultHttp2Connection implements Http2Connection { } private void initChildrenIfEmpty() { - if (children == PrimitiveCollections.emptyIntObjectMap()) { + if (children == IntCollections.emptyMap()) { initChildren(); } } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java index 20fc26baa2..628703b571 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java @@ -59,7 +59,6 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http2.Http2CodecUtil.SimpleChannelPromiseAggregator; import io.netty.handler.codec.http2.Http2FrameWriter.Configuration; -import io.netty.util.collection.CharObjectMap; /** * A {@link Http2FrameWriter} that supports all frame types defined by the HTTP/2 specification. @@ -213,7 +212,7 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize int payloadLength = SETTING_ENTRY_LENGTH * settings.size(); ByteBuf buf = ctx.alloc().buffer(FRAME_HEADER_LENGTH + settings.size() * SETTING_ENTRY_LENGTH); writeFrameHeaderInternal(buf, payloadLength, SETTINGS, new Http2Flags(), 0); - for (CharObjectMap.PrimitiveEntry entry : settings.entries()) { + for (Http2Settings.PrimitiveEntry entry : settings.entries()) { writeUnsignedShort(entry.key(), buf); writeUnsignedInt(entry.value(), buf); } diff --git a/common/src/main/java/io/netty/util/collection/package-info.java b/common/src/main/java/io/netty/util/collection/package-info.java deleted file mode 100644 index b5f6029edb..0000000000 --- a/common/src/main/java/io/netty/util/collection/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2014 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -/** - * Utility classes for commonly used collections. - */ -package io.netty.util.collection; diff --git a/common/src/main/java/io/netty/util/collection/PrimitiveCollections.java b/common/src/main/templates/io/netty/util/collection/KCollections.template similarity index 78% rename from common/src/main/java/io/netty/util/collection/PrimitiveCollections.java rename to common/src/main/templates/io/netty/util/collection/KCollections.template index 4cf1f53623..a5a5534e41 100644 --- a/common/src/main/java/io/netty/util/collection/PrimitiveCollections.java +++ b/common/src/main/templates/io/netty/util/collection/KCollections.template @@ -22,47 +22,46 @@ import java.util.NoSuchElementException; import java.util.Set; /** - * Utility methods for primitive collections. + * Utilities for @k@-based primitive collections. */ -public final class PrimitiveCollections { +public final class @K@Collections { - private static final IntObjectMap EMPTY_INT_OBJECT_MAP = new EmptyIntObjectMap(); + private static final @K@ObjectMap EMPTY_MAP = new EmptyMap(); - private PrimitiveCollections() { + private @K@Collections() { } /** - * Returns an unmodifiable empty {@link IntObjectMap}. + * Returns an unmodifiable empty {@link @K@ObjectMap}. */ @SuppressWarnings("unchecked") - public static IntObjectMap emptyIntObjectMap() { - return (IntObjectMap) EMPTY_INT_OBJECT_MAP; + public static @K@ObjectMap emptyMap() { + return (@K@ObjectMap) EMPTY_MAP; } /** * Creates an unmodifiable wrapper around the given map. */ - public static IntObjectMap unmodifiableIntObjectMap(final IntObjectMap map) { - return new UnmodifiableIntObjectMap(map); + public static @K@ObjectMap unmodifiableMap(final @K@ObjectMap map) { + return new UnmodifiableMap(map); } /** * An empty map. All operations that attempt to modify the map are unsupported. */ - private static final class EmptyIntObjectMap implements IntObjectMap { - + private static final class EmptyMap implements @K@ObjectMap { @Override - public Object get(int key) { + public Object get(@k@ key) { return null; } @Override - public Object put(int key, Object value) { + public Object put(@k@ key, Object value) { throw new UnsupportedOperationException("put"); } @Override - public Object remove(int key) { + public Object remove(@k@ key) { return null; } @@ -87,12 +86,12 @@ public final class PrimitiveCollections { } @Override - public Set keySet() { + public Set<@O@> keySet() { return Collections.emptySet(); } @Override - public boolean containsKey(int key) { + public boolean containsKey(@k@ key) { return false; } @@ -112,7 +111,7 @@ public final class PrimitiveCollections { } @Override - public Object put(Integer key, Object value) { + public Object put(@O@ key, Object value) { throw new UnsupportedOperationException(); } @@ -122,7 +121,7 @@ public final class PrimitiveCollections { } @Override - public void putAll(Map m) { + public void putAll(Map m) { throw new UnsupportedOperationException(); } @@ -132,39 +131,39 @@ public final class PrimitiveCollections { } @Override - public Set> entrySet() { + public Set> entrySet() { return Collections.emptySet(); } } /** - * An unmodifiable wrapper around a {@link IntObjectMap}. + * An unmodifiable wrapper around a {@link @K@ObjectMap}. * * @param the value type stored in the map. */ - private static final class UnmodifiableIntObjectMap implements IntObjectMap { - private final IntObjectMap map; - private Set keySet; - private Set> entrySet; + private static final class UnmodifiableMap implements @K@ObjectMap { + private final @K@ObjectMap map; + private Set<@O@> keySet; + private Set> entrySet; private Collection values; private Iterable> entries; - UnmodifiableIntObjectMap(IntObjectMap map) { + UnmodifiableMap(@K@ObjectMap map) { this.map = map; } @Override - public V get(int key) { + public V get(@k@ key) { return map.get(key); } @Override - public V put(int key, V value) { + public V put(@k@ key, V value) { throw new UnsupportedOperationException("put"); } @Override - public V remove(int key) { + public V remove(@k@ key) { throw new UnsupportedOperationException("remove"); } @@ -184,7 +183,7 @@ public final class PrimitiveCollections { } @Override - public boolean containsKey(int key) { + public boolean containsKey(@k@ key) { return map.containsKey(key); } @@ -204,7 +203,7 @@ public final class PrimitiveCollections { } @Override - public V put(Integer key, V value) { + public V put(@O@ key, V value) { throw new UnsupportedOperationException("put"); } @@ -214,7 +213,7 @@ public final class PrimitiveCollections { } @Override - public void putAll(Map m) { + public void putAll(Map m) { throw new UnsupportedOperationException("putAll"); } @@ -233,7 +232,7 @@ public final class PrimitiveCollections { } @Override - public Set keySet() { + public Set<@O@> keySet() { if (keySet == null) { keySet = Collections.unmodifiableSet(map.keySet()); } @@ -241,7 +240,7 @@ public final class PrimitiveCollections { } @Override - public Set> entrySet() { + public Set> entrySet() { if (entrySet == null) { entrySet = Collections.unmodifiableSet(map.entrySet()); } @@ -289,14 +288,14 @@ public final class PrimitiveCollections { * Unmodifiable wrapper for an entry. */ private class EntryImpl implements PrimitiveEntry { - final PrimitiveEntry entry; + private final PrimitiveEntry entry; EntryImpl(PrimitiveEntry entry) { this.entry = entry; } @Override - public int key() { + public @k@ key() { return entry.key(); }