Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ObjectIdSubclassMap.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 static final int INITIAL_TABLE_SIZE = 32;
  64. private int size;
  65. private int grow;
  66. private int mask;
  67. private V[] table;
  68. /** Create an empty map. */
  69. public ObjectIdSubclassMap() {
  70. initTable(INITIAL_TABLE_SIZE);
  71. }
  72. /** Remove all entries from this map. */
  73. public void clear() {
  74. size = 0;
  75. initTable(INITIAL_TABLE_SIZE);
  76. }
  77. /**
  78. * Lookup an existing mapping.
  79. *
  80. * @param toFind
  81. * the object identifier to find.
  82. * @return the instance mapped to toFind, or null if no mapping exists.
  83. */
  84. public V get(final AnyObjectId toFind) {
  85. int i = index(toFind);
  86. V obj;
  87. while ((obj = table[i]) != null) {
  88. if (AnyObjectId.equals(obj, toFind))
  89. return obj;
  90. if (++i == table.length)
  91. i = 0;
  92. }
  93. return null;
  94. }
  95. /**
  96. * Returns true if this map contains the specified object.
  97. *
  98. * @param toFind
  99. * object to find.
  100. * @return true if the mapping exists for this object; false otherwise.
  101. */
  102. public boolean contains(final AnyObjectId toFind) {
  103. return get(toFind) != null;
  104. }
  105. /**
  106. * Store an object for future lookup.
  107. * <p>
  108. * An existing mapping for <b>must not</b> be in this map. Callers must
  109. * first call {@link #get(AnyObjectId)} to verify there is no current
  110. * mapping prior to adding a new mapping, or use
  111. * {@link #addIfAbsent(ObjectId)}.
  112. *
  113. * @param newValue
  114. * the object to store.
  115. * @param <Q>
  116. * type of instance to store.
  117. */
  118. public <Q extends V> void add(final Q newValue) {
  119. if (++size == grow)
  120. grow();
  121. insert(newValue);
  122. }
  123. /**
  124. * Store an object for future lookup.
  125. * <p>
  126. * Stores {@code newValue}, but only if there is not already an object for
  127. * the same object name. Callers can tell if the value is new by checking
  128. * the return value with reference equality:
  129. *
  130. * <pre>
  131. * V obj = ...;
  132. * boolean wasNew = map.addIfAbsent(obj) == obj;
  133. * </pre>
  134. *
  135. * @param newValue
  136. * the object to store.
  137. * @return {@code newValue} if stored, or the prior value already stored and
  138. * that would have been returned had the caller used
  139. * {@code get(newValue)} first.
  140. * @param <Q>
  141. * type of instance to store.
  142. */
  143. public <Q extends V> V addIfAbsent(final Q newValue) {
  144. int i = index(newValue);
  145. V obj;
  146. while ((obj = table[i]) != null) {
  147. if (AnyObjectId.equals(obj, newValue))
  148. return obj;
  149. if (++i == table.length)
  150. i = 0;
  151. }
  152. if (++size == grow) {
  153. grow();
  154. insert(newValue);
  155. } else {
  156. table[i] = newValue;
  157. }
  158. return newValue;
  159. }
  160. /**
  161. * @return number of objects in map
  162. */
  163. public int size() {
  164. return size;
  165. }
  166. /** @return true if {@link #size()} is 0. */
  167. public boolean isEmpty() {
  168. return size == 0;
  169. }
  170. public Iterator<V> iterator() {
  171. return new Iterator<V>() {
  172. private int found;
  173. private int i;
  174. public boolean hasNext() {
  175. return found < size;
  176. }
  177. public V next() {
  178. while (i < table.length) {
  179. final V v = table[i++];
  180. if (v != null) {
  181. found++;
  182. return v;
  183. }
  184. }
  185. throw new NoSuchElementException();
  186. }
  187. public void remove() {
  188. throw new UnsupportedOperationException();
  189. }
  190. };
  191. }
  192. private final int index(final AnyObjectId id) {
  193. return id.w1 & mask;
  194. }
  195. private void insert(final V newValue) {
  196. int j = index(newValue);
  197. while (table[j] != null) {
  198. if (++j >= table.length)
  199. j = 0;
  200. }
  201. table[j] = newValue;
  202. }
  203. private void grow() {
  204. final V[] oldTable = table;
  205. final int oldSize = table.length;
  206. initTable(oldSize << 1);
  207. for (int i = 0; i < oldSize; i++) {
  208. final V obj = oldTable[i];
  209. if (obj != null)
  210. insert(obj);
  211. }
  212. }
  213. private void initTable(int sz) {
  214. grow = sz >> 1;
  215. mask = sz - 1;
  216. table = createArray(sz);
  217. }
  218. @SuppressWarnings("unchecked")
  219. private final V[] createArray(final int sz) {
  220. return (V[]) new ObjectId[sz];
  221. }
  222. }