From ed29dec1eafb46225df5f9614262acc40aac7142 Mon Sep 17 00:00:00 2001 From: Shawn Pearce Date: Fri, 4 Aug 2017 17:16:02 -0700 Subject: [PATCH] Expose LongMap in util package This is a useful primitive collection type like IntList. Change-Id: I04b9b2ba25247df056eb3a1725602f1be6d3b440 --- .../jgit/{transport => util}/LongMapTest.java | 2 +- .../eclipse/jgit/transport/PackParser.java | 1 + .../jgit/{transport => util}/LongMap.java | 42 ++++++++++++++----- 3 files changed, 34 insertions(+), 11 deletions(-) rename org.eclipse.jgit.test/tst/org/eclipse/jgit/{transport => util}/LongMapTest.java (99%) rename org.eclipse.jgit/src/org/eclipse/jgit/{transport => util}/LongMap.java (83%) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/LongMapTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/LongMapTest.java similarity index 99% rename from org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/LongMapTest.java rename to org.eclipse.jgit.test/tst/org/eclipse/jgit/util/LongMapTest.java index 1a86aaff33..054c61e2be 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/LongMapTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/LongMapTest.java @@ -41,7 +41,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.transport; +package org.eclipse.jgit.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java index db3578bdb4..2f6b271d87 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java @@ -82,6 +82,7 @@ import org.eclipse.jgit.lib.ObjectStream; import org.eclipse.jgit.lib.ProgressMonitor; import org.eclipse.jgit.util.BlockList; import org.eclipse.jgit.util.IO; +import org.eclipse.jgit.util.LongMap; import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.sha1.SHA1; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/LongMap.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/LongMap.java similarity index 83% rename from org.eclipse.jgit/src/org/eclipse/jgit/transport/LongMap.java rename to org.eclipse.jgit/src/org/eclipse/jgit/util/LongMap.java index 4d60202a6f..7b0b0c728a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/LongMap.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/LongMap.java @@ -41,15 +41,16 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.eclipse.jgit.transport; +package org.eclipse.jgit.util; /** - * Simple Map helper for {@link PackParser}. + * Simple Map. * * @param * type of the value instance. + * @since 4.9 */ -final class LongMap { +public class LongMap { private static final float LOAD_FACTOR = 0.75f; private Node[] table; @@ -60,16 +61,27 @@ final class LongMap { /** Next {@link #size} to trigger a {@link #grow()}. */ private int growAt; - LongMap() { + /** Initialize an empty LongMap. */ + public LongMap() { table = createArray(64); growAt = (int) (table.length * LOAD_FACTOR); } - boolean containsKey(final long key) { + /** + * @param key + * the key to find. + * @return {@code true} if {@code key} is present in the map. + */ + public boolean containsKey(long key) { return get(key) != null; } - V get(final long key) { + /** + * @param key + * the key to find. + * @return stored value of the key, or {@code null}. + */ + public V get(long key) { for (Node n = table[index(key)]; n != null; n = n.next) { if (n.key == key) return n.value; @@ -77,7 +89,12 @@ final class LongMap { return null; } - V remove(final long key) { + /** + * @param key + * key to remove from the map. + * @return old value of the key, or {@code null}. + */ + public V remove(long key) { Node n = table[index(key)]; Node prior = null; while (n != null) { @@ -95,7 +112,14 @@ final class LongMap { return null; } - V put(final long key, final V value) { + /** + * @param key + * key to store {@code value} under. + * @param value + * new value. + * @return prior value, or null. + */ + public V put(long key, V value) { for (Node n = table[index(key)]; n != null; n = n.next) { if (n.key == key) { final V o = n.value; @@ -145,9 +169,7 @@ final class LongMap { private static class Node { final long key; - V value; - Node next; Node(final long k, final V v) { -- 2.39.5