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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.
  108. *
  109. * @param newValue
  110. * the object to store.
  111. * @param
  112. * <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. * @return number of objects in map
  123. */
  124. public int size() {
  125. return size;
  126. }
  127. /** @return true if {@link #size()} is 0. */
  128. public boolean isEmpty() {
  129. return size == 0;
  130. }
  131. public Iterator<V> iterator() {
  132. return new Iterator<V>() {
  133. private int found;
  134. private int i;
  135. public boolean hasNext() {
  136. return found < size;
  137. }
  138. public V next() {
  139. while (i < obj_hash.length) {
  140. final V v = obj_hash[i++];
  141. if (v != null) {
  142. found++;
  143. return v;
  144. }
  145. }
  146. throw new NoSuchElementException();
  147. }
  148. public void remove() {
  149. throw new UnsupportedOperationException();
  150. }
  151. };
  152. }
  153. private final int index(final AnyObjectId id) {
  154. return (id.w1 >>> 1) % obj_hash.length;
  155. }
  156. private void insert(final V newValue) {
  157. int j = index(newValue);
  158. while (obj_hash[j] != null) {
  159. if (++j >= obj_hash.length)
  160. j = 0;
  161. }
  162. obj_hash[j] = newValue;
  163. }
  164. private void grow() {
  165. final V[] old_hash = obj_hash;
  166. final int old_hash_size = obj_hash.length;
  167. obj_hash = createArray(2 * old_hash_size);
  168. for (int i = 0; i < old_hash_size; i++) {
  169. final V obj = old_hash[i];
  170. if (obj != null)
  171. insert(obj);
  172. }
  173. }
  174. @SuppressWarnings("unchecked")
  175. private final V[] createArray(final int sz) {
  176. return (V[]) new ObjectId[sz];
  177. }
  178. }