You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectIdSubclassMap.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import java.util.Iterator;
  47. import java.util.NoSuchElementException;
  48. /**
  49. * Fast, efficient map specifically for {@link ObjectId} subclasses.
  50. * <p>
  51. * This map provides an efficient translation from any ObjectId instance to a
  52. * cached subclass of ObjectId that has the same value.
  53. * <p>
  54. * Raw value equality is tested when comparing two ObjectIds (or subclasses),
  55. * not reference equality and not <code>.equals(Object)</code> equality. This
  56. * allows subclasses to override <code>equals</code> to supply their own
  57. * extended semantics.
  58. *
  59. * @param <V>
  60. * type of subclass of ObjectId that will be stored in the map.
  61. */
  62. public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> {
  63. private int size;
  64. private V[] obj_hash;
  65. /** Create an empty map. */
  66. public ObjectIdSubclassMap() {
  67. obj_hash = createArray(32);
  68. }
  69. /** Remove all entries from this map. */
  70. public void clear() {
  71. size = 0;
  72. obj_hash = createArray(32);
  73. }
  74. /**
  75. * Lookup an existing mapping.
  76. *
  77. * @param toFind
  78. * the object identifier to find.
  79. * @return the instance mapped to toFind, or null if no mapping exists.
  80. */
  81. public V get(final AnyObjectId toFind) {
  82. int i = index(toFind);
  83. V obj;
  84. while ((obj = obj_hash[i]) != null) {
  85. if (AnyObjectId.equals(obj, toFind))
  86. return obj;
  87. if (++i == obj_hash.length)
  88. i = 0;
  89. }
  90. return null;
  91. }
  92. /**
  93. * Returns true if this map contains the specified object.
  94. *
  95. * @param toFind
  96. * object to find.
  97. * @return true if the mapping exists for this object; false otherwise.
  98. */
  99. public boolean contains(final AnyObjectId toFind) {
  100. return get(toFind) != null;
  101. }
  102. /**
  103. * Store an object for future lookup.
  104. * <p>
  105. * An existing mapping for <b>must not</b> be in this map. Callers must
  106. * first call {@link #get(AnyObjectId)} to verify there is no current
  107. * mapping prior to adding a new mapping, or use
  108. * {@link #addIfAbsent(ObjectId)}.
  109. *
  110. * @param newValue
  111. * the object to store.
  112. * @param <Q>
  113. * type of instance to store.
  114. */
  115. public <Q extends V> void add(final Q newValue) {
  116. if (obj_hash.length - 1 <= size * 2)
  117. grow();
  118. insert(newValue);
  119. size++;
  120. }
  121. /**
  122. * Store an object for future lookup.
  123. * <p>
  124. * Stores {@code newValue}, but only if there is not already an object for
  125. * the same object name. Callers can tell if the value is new by checking
  126. * the return value with reference equality:
  127. *
  128. * <pre>
  129. * V obj = ...;
  130. * boolean wasNew = map.addIfAbsent(obj) == obj;
  131. * </pre>
  132. *
  133. * @param newValue
  134. * the object to store.
  135. * @return {@code newValue} if stored, or the prior value already stored and
  136. * that would have been returned had the caller used
  137. * {@code get(newValue)} first.
  138. * @param <Q>
  139. * type of instance to store.
  140. */
  141. public <Q extends V> V addIfAbsent(final Q newValue) {
  142. int i = index(newValue);
  143. V obj;
  144. while ((obj = obj_hash[i]) != null) {
  145. if (AnyObjectId.equals(obj, newValue))
  146. return obj;
  147. if (++i == obj_hash.length)
  148. i = 0;
  149. }
  150. if (obj_hash.length - 1 <= size * 2) {
  151. grow();
  152. insert(newValue);
  153. } else {
  154. obj_hash[i] = newValue;
  155. }
  156. size++;
  157. return newValue;
  158. }
  159. /**
  160. * @return number of objects in map
  161. */
  162. public int size() {
  163. return size;
  164. }
  165. /** @return true if {@link #size()} is 0. */
  166. public boolean isEmpty() {
  167. return size == 0;
  168. }
  169. public Iterator<V> iterator() {
  170. return new Iterator<V>() {
  171. private int found;
  172. private int i;
  173. public boolean hasNext() {
  174. return found < size;
  175. }
  176. public V next() {
  177. while (i < obj_hash.length) {
  178. final V v = obj_hash[i++];
  179. if (v != null) {
  180. found++;
  181. return v;
  182. }
  183. }
  184. throw new NoSuchElementException();
  185. }
  186. public void remove() {
  187. throw new UnsupportedOperationException();
  188. }
  189. };
  190. }
  191. private final int index(final AnyObjectId id) {
  192. return (id.w1 >>> 1) % obj_hash.length;
  193. }
  194. private void insert(final V newValue) {
  195. int j = index(newValue);
  196. while (obj_hash[j] != null) {
  197. if (++j >= obj_hash.length)
  198. j = 0;
  199. }
  200. obj_hash[j] = newValue;
  201. }
  202. private void grow() {
  203. final V[] old_hash = obj_hash;
  204. final int old_hash_size = obj_hash.length;
  205. obj_hash = createArray(2 * old_hash_size);
  206. for (int i = 0; i < old_hash_size; i++) {
  207. final V obj = old_hash[i];
  208. if (obj != null)
  209. insert(obj);
  210. }
  211. }
  212. @SuppressWarnings("unchecked")
  213. private final V[] createArray(final int sz) {
  214. return (V[]) new ObjectId[sz];
  215. }
  216. }