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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /**
  48. * Fast, efficient map specifically for {@link ObjectId} subclasses.
  49. * <p>
  50. * This map provides an efficient translation from any ObjectId instance to a
  51. * cached subclass of ObjectId that has the same value.
  52. * <p>
  53. * Raw value equality is tested when comparing two ObjectIds (or subclasses),
  54. * not reference equality and not <code>.equals(Object)</code> equality. This
  55. * allows subclasses to override <code>equals</code> to supply their own
  56. * extended semantics.
  57. *
  58. * @param <V>
  59. * type of subclass of ObjectId that will be stored in the map.
  60. */
  61. public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> {
  62. private int size;
  63. private V[] obj_hash;
  64. /** Create an empty map. */
  65. public ObjectIdSubclassMap() {
  66. obj_hash = createArray(32);
  67. }
  68. /** Remove all entries from this map. */
  69. public void clear() {
  70. size = 0;
  71. obj_hash = createArray(32);
  72. }
  73. /**
  74. * Lookup an existing mapping.
  75. *
  76. * @param toFind
  77. * the object identifier to find.
  78. * @return the instance mapped to toFind, or null if no mapping exists.
  79. */
  80. public V get(final AnyObjectId toFind) {
  81. int i = index(toFind);
  82. V obj;
  83. while ((obj = obj_hash[i]) != null) {
  84. if (AnyObjectId.equals(obj, toFind))
  85. return obj;
  86. if (++i == obj_hash.length)
  87. i = 0;
  88. }
  89. return null;
  90. }
  91. /**
  92. * Returns true if this map contains the specified object.
  93. *
  94. * @param toFind
  95. * object to find.
  96. * @return true if the mapping exists for this object; false otherwise.
  97. */
  98. public boolean contains(final AnyObjectId toFind) {
  99. return get(toFind) != null;
  100. }
  101. /**
  102. * Store an object for future lookup.
  103. * <p>
  104. * An existing mapping for <b>must not</b> be in this map. Callers must
  105. * first call {@link #get(AnyObjectId)} to verify there is no current
  106. * mapping prior to adding a new mapping.
  107. *
  108. * @param newValue
  109. * the object to store.
  110. * @param
  111. * <Q>
  112. * type of instance to store.
  113. */
  114. public <Q extends V> void add(final Q newValue) {
  115. if (obj_hash.length - 1 <= size * 2)
  116. grow();
  117. insert(newValue);
  118. size++;
  119. }
  120. /**
  121. * @return number of objects in map
  122. */
  123. public int size() {
  124. return size;
  125. }
  126. /** @return true if {@link #size()} is 0. */
  127. public boolean isEmpty() {
  128. return size == 0;
  129. }
  130. public Iterator<V> iterator() {
  131. return new Iterator<V>() {
  132. private int found;
  133. private int i;
  134. public boolean hasNext() {
  135. return found < size;
  136. }
  137. public V next() {
  138. while (i < obj_hash.length) {
  139. final V v = obj_hash[i++];
  140. if (v != null) {
  141. found++;
  142. return v;
  143. }
  144. }
  145. throw new IllegalStateException();
  146. }
  147. public void remove() {
  148. throw new UnsupportedOperationException();
  149. }
  150. };
  151. }
  152. private final int index(final AnyObjectId id) {
  153. return (id.w1 >>> 1) % obj_hash.length;
  154. }
  155. private void insert(final V newValue) {
  156. int j = index(newValue);
  157. while (obj_hash[j] != null) {
  158. if (++j >= obj_hash.length)
  159. j = 0;
  160. }
  161. obj_hash[j] = newValue;
  162. }
  163. private void grow() {
  164. final V[] old_hash = obj_hash;
  165. final int old_hash_size = obj_hash.length;
  166. obj_hash = createArray(2 * old_hash_size);
  167. for (int i = 0; i < old_hash_size; i++) {
  168. final V obj = old_hash[i];
  169. if (obj != null)
  170. insert(obj);
  171. }
  172. }
  173. @SuppressWarnings("unchecked")
  174. private final V[] createArray(final int sz) {
  175. return (V[]) new ObjectId[sz];
  176. }
  177. }