private void initialize(List<ContextResolver> contextResolvers) { Map<Type, Map<MediaType, List<ContextResolver>>> rs = new HashMap<>(); for (ContextResolver provider : contextResolvers) { List<MediaType> ms = MediaTypes.createFrom(provider.getClass().getAnnotation(Produces.class)); Type type = getParameterizedType(provider.getClass()); Map<MediaType, List<ContextResolver>> mr = rs.get(type); if (mr == null) { mr = new HashMap<>(); rs.put(type, mr); } for (MediaType m : ms) { List<ContextResolver> crl = mr.get(m); if (crl == null) { crl = new ArrayList<>(); mr.put(m, crl); } crl.add(provider); } } // Reduce set of two or more context resolvers for same type and // media type for (Map.Entry<Type, Map<MediaType, List<ContextResolver>>> e : rs.entrySet()) { Map<MediaType, ContextResolver> mr = new KeyComparatorHashMap<>(4, MessageBodyFactory.MEDIA_TYPE_KEY_COMPARATOR); resolver.put(e.getKey(), mr); cache.put(e.getKey(), new ConcurrentHashMap<>(4)); for (Map.Entry<MediaType, List<ContextResolver>> f : e.getValue().entrySet()) { mr.put(f.getKey(), reduce(f.getValue())); } } }
/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for this key, the old * value is replaced. * * @param key key with which the specified value is to be associated. * @param value value to be associated with the specified key. * @return previous value associated with specified key, or <tt>null</tt> * if there was no mapping for key. A <tt>null</tt> return can * also indicate that the HashMap previously associated * <tt>null</tt> with the specified key. */ @Override public V put(final K key, final V value) { final K k = maskNull(key); final int hash = keyComparatorHash(k); final int i = indexFor(hash, table.length); for (Entry<K, V> e = table[i]; e != null; e = e.next) { if (e.hash == hash && keyComparatorEq(k, e.key)) { final V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, k, value, i); return null; }
/** * Removes all of the mappings from this map. * The map will be empty after this call returns. */ @Override public void clear() { super.clear(); header.before = header.after = header; }
/** * Returns the entry associated with the specified key in the * HashMap. Returns null if the HashMap contains no mapping * for this key. */ Entry<K, V> getEntry(final K key) { final K k = maskNull(key); final int hash = keyComparatorHash(k); final int i = indexFor(hash, table.length); Entry<K, V> e = table[i]; while (e != null && !(e.hash == hash && keyComparatorEq(k, e.key))) { e = e.next; } return e; }
/** * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and * values themselves are not cloned. * * @return a shallow copy of this map. */ @Override public Object clone() { KeyComparatorHashMap<K, V> result = null; try { result = (KeyComparatorHashMap<K, V>) super.clone(); result.table = new Entry[table.length]; result.entrySet = null; result.modCount = 0; result.size = 0; result.init(); result.putAllForCreate(this); } catch (final CloneNotSupportedException e) { // assert false; } return result; }
/** * Reconstitute the <tt>HashMap</tt> instance from a stream (i.e., * deserialize it). */ private void readObject(final java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { // Read in the threshold, loadfactor, and any hidden stuff s.defaultReadObject(); // Read in number of buckets and allocate the bucket array; final int numBuckets = s.readInt(); table = new Entry[numBuckets]; init(); // Give subclass a chance to do its thing. // Read in ss (number of Mappings) final int ss = s.readInt(); // Read the keys and values, and put the mappings in the HashMap for (int i = 0; i < ss; i++) { final K key = (K) s.readObject(); final V value = (V) s.readObject(); putForCreate(key, value); } }
final K k = maskNull(entry.getKey()); final int hash = keyComparatorHash(k); final int i = indexFor(hash, table.length); Entry<K, V> prev = table[i]; Entry<K, V> e = prev;
/** * Transfer all entries from current table to newTable. */ void transfer(final Entry<K, V>[] newTable) { final Entry<K, V>[] src = table; final int newCapacity = newTable.length; for (int j = 0; j < src.length; j++) { Entry<K, V> e = src[j]; if (e != null) { src[j] = null; do { final Entry<K, V> next = e.next; final int i = indexFor(e.hash, newCapacity); e.next = newTable[i]; newTable[i] = e; e = next; } while (e != null); } } }
/** * Returns <tt>true</tt> if this map maps one or more keys to the * specified value. * * @param value value whose presence in this map is to be tested. * @return <tt>true</tt> if this map maps one or more keys to the * specified value. */ @Override public boolean containsValue(final Object value) { if (value == null) { return containsNullValue(); } final Entry[] tab = table; for (int i = 0; i < tab.length; i++) { for (Entry e = tab[i]; e != null; e = e.next) { if (value.equals(e.value)) { return true; } } } return false; }
/** * Save the state of the <tt>HashMap</tt> instance to a stream (i.e., * serialize it). * * @serialData The <i>capacity</i> of the HashMap (the length of the * bucket array) is emitted (int), followed by the * <i>ss</i> of the HashMap (the number of key-value * mappings), followed by the key (Object) and value (Object) * for each key-value mapping represented by the HashMap * The key-value mappings are emitted in the order that they * are returned by <tt>entrySet().iterator()</tt>. */ private void writeObject(final java.io.ObjectOutputStream s) throws IOException { final Iterator<Map.Entry<K, V>> i = entrySet().iterator(); // Write out the threshold, loadfactor, and any hidden stuff s.defaultWriteObject(); // Write out number of buckets s.writeInt(table.length); // Write out ss (number of Mappings) s.writeInt(size); // Write out keys and values (alternating) while (i.hasNext()) { final Map.Entry<K, V> e = i.next(); s.writeObject(e.getKey()); s.writeObject(e.getValue()); } }
this.keyComparator = keyComparator; init();
/** * Reconstitute the <tt>HashMap</tt> instance from a stream (i.e., * deserialize it). */ private void readObject(final java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { // Read in the threshold, loadfactor, and any hidden stuff s.defaultReadObject(); // Read in number of buckets and allocate the bucket array; final int numBuckets = s.readInt(); table = new Entry[numBuckets]; init(); // Give subclass a chance to do its thing. // Read in ss (number of Mappings) final int ss = s.readInt(); // Read the keys and values, and put the mappings in the HashMap for (int i = 0; i < ss; i++) { final K key = (K) s.readObject(); final V value = (V) s.readObject(); putForCreate(key, value); } }
/** * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and * values themselves are not cloned. * * @return a shallow copy of this map. */ @Override public Object clone() { KeyComparatorHashMap<K, V> result = null; try { result = (KeyComparatorHashMap<K, V>) super.clone(); result.table = new Entry[table.length]; result.entrySet = null; result.modCount = 0; result.size = 0; result.init(); result.putAllForCreate(this); } catch (final CloneNotSupportedException e) { // assert false; } return result; }
final K k = maskNull(entry.getKey()); final int hash = keyComparatorHash(k); final int i = indexFor(hash, table.length); Entry<K, V> prev = table[i]; Entry<K, V> e = prev;
/** * Transfer all entries from current table to newTable. */ void transfer(final Entry<K, V>[] newTable) { final Entry<K, V>[] src = table; final int newCapacity = newTable.length; for (int j = 0; j < src.length; j++) { Entry<K, V> e = src[j]; if (e != null) { src[j] = null; do { final Entry<K, V> next = e.next; final int i = indexFor(e.hash, newCapacity); e.next = newTable[i]; newTable[i] = e; e = next; } while (e != null); } } }
/** * Returns <tt>true</tt> if this map maps one or more keys to the * specified value. * * @param value value whose presence in this map is to be tested. * @return <tt>true</tt> if this map maps one or more keys to the * specified value. */ @Override public boolean containsValue(final Object value) { if (value == null) { return containsNullValue(); } final Entry[] tab = table; for (int i = 0; i < tab.length; i++) { for (Entry e = tab[i]; e != null; e = e.next) { if (value.equals(e.value)) { return true; } } } return false; }