diff options
author | Chris Aniszczyk <caniszczyk@gmail.com> | 2011-01-28 12:45:39 -0500 |
---|---|---|
committer | Code Review <codereview-daemon@eclipse.org> | 2011-01-28 12:45:39 -0500 |
commit | a880233d7fa7a1085aa5e0a124760efcaf47e327 (patch) | |
tree | 0fda16c8c39173efdfe419b6318b16afa5499977 /org.eclipse.jgit | |
parent | bf69401feeea459d10b0cfc9cbe9bdb5d8f38bf0 (diff) | |
parent | 17dc6bdafd6399a94f2b64357ea48873f6b71b2a (diff) | |
download | jgit-a880233d7fa7a1085aa5e0a124760efcaf47e327.tar.gz jgit-a880233d7fa7a1085aa5e0a124760efcaf47e327.zip |
Merge "ObjectIdSubclassMap: Support duplicate additions"
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java | 47 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/storage/file/CachedObjectDirectory.java | 3 |
2 files changed, 45 insertions, 5 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java index 612e00ea25..ad78acdb8e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java @@ -114,12 +114,12 @@ public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> { * <p> * An existing mapping for <b>must not</b> be in this map. Callers must * first call {@link #get(AnyObjectId)} to verify there is no current - * mapping prior to adding a new mapping. + * mapping prior to adding a new mapping, or use + * {@link #addIfAbsent(ObjectId)}. * * @param newValue * the object to store. - * @param - * <Q> + * @param <Q> * type of instance to store. */ public <Q extends V> void add(final Q newValue) { @@ -130,6 +130,47 @@ public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> { } /** + * Store an object for future lookup. + * <p> + * Stores {@code newValue}, but only if there is not already an object for + * the same object name. Callers can tell if the value is new by checking + * the return value with reference equality: + * + * <pre> + * V obj = ...; + * boolean wasNew = map.addIfAbsent(obj) == obj; + * </pre> + * + * @param newValue + * the object to store. + * @return {@code newValue} if stored, or the prior value already stored and + * that would have been returned had the caller used + * {@code get(newValue)} first. + * @param <Q> + * type of instance to store. + */ + public <Q extends V> V addIfAbsent(final Q newValue) { + int i = index(newValue); + V obj; + + while ((obj = obj_hash[i]) != null) { + if (AnyObjectId.equals(obj, newValue)) + return obj; + if (++i == obj_hash.length) + i = 0; + } + + if (obj_hash.length - 1 <= size * 2) { + grow(); + insert(newValue); + } else { + obj_hash[i] = newValue; + } + size++; + return newValue; + } + + /** * @return number of objects in map */ public int size() { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/CachedObjectDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/CachedObjectDirectory.java index 001ae288a1..d5f2ac53cb 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/CachedObjectDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/CachedObjectDirectory.java @@ -228,8 +228,7 @@ class CachedObjectDirectory extends FileObjectDatabase { switch (result) { case INSERTED: case EXISTS_LOOSE: - if (!unpackedObjects.contains(objectId)) - unpackedObjects.add(objectId); + unpackedObjects.addIfAbsent(objectId); break; case EXISTS_PACKED: |