Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/collect/HashMapping.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.collect; | |
24 | |
25 import java.util.*; | |
26 | |
27 /** | |
28 * This class provides a skeletal implementation of the {@link Mapping} interface, to minimize the effort required to | |
29 * implement this interface. | |
30 * <p> | |
31 * This class also includes a number of factory methods for creating {@code Mapping} instances with various properties. | |
32 */ | |
33 public abstract class HashMapping<K, V> implements Mapping<K, V> { | |
34 | |
35 private final HashEquivalence<K> equivalence; | |
36 | |
37 /** | |
38 * Determines if two given keys are equal. | |
39 * <p> | |
40 * Subclasses override this method to define equivalence without delegating to a {@link HashEquivalence} object. | |
41 */ | |
42 protected boolean equivalent(K key1, K key2) { | |
43 return equivalence.equivalent(key1, key2); | |
44 } | |
45 | |
46 /** | |
47 * Computes a hash code for a given key. | |
48 * <p> | |
49 * Subclasses override this method to compute a hash code without delegating to a {@link HashEquivalence} object. | |
50 */ | |
51 protected int hashCode(K key) { | |
52 // Don't guard against a negative number here as the caller needs to convert the hash code into a valid index | |
53 // which will involve range checking anyway | |
54 return equivalence.hashCode(key); | |
55 } | |
56 | |
57 /** | |
58 * Creates a hash table. | |
59 * | |
60 * @param equivalence | |
61 * the semantics to be used for comparing keys. If {@code null} is provided, then {@link HashEquality} is | |
62 * used. | |
63 */ | |
64 protected HashMapping(HashEquivalence<K> equivalence) { | |
65 if (equivalence == null) { | |
66 final Class<HashEquality<K>> type = null; | |
67 this.equivalence = HashEquality.instance(type); | |
68 } else { | |
69 this.equivalence = equivalence; | |
70 } | |
71 } | |
72 | |
73 public boolean containsKey(K key) { | |
74 return get(key) != null; | |
75 } | |
76 | |
77 protected abstract class HashMappingIterable<Type> implements IterableWithLength<Type> { | |
78 public int size() { | |
79 return HashMapping.this.length(); | |
80 } | |
81 } | |
82 | |
83 /** | |
84 * Gets an iterator over the values in this mapping by looking up each {@linkplain #keys() key}. | |
85 * <p> | |
86 * Subclasses will most likely override this method with a more efficient implementation. | |
87 */ | |
88 public IterableWithLength<V> values() { | |
89 return new HashMappingIterable<V>() { | |
90 private final IterableWithLength<K> keys = keys(); | |
91 public Iterator<V> iterator() { | |
92 return new Iterator<V>() { | |
93 private final Iterator<K> keyIterator = keys.iterator(); | |
94 | |
95 public boolean hasNext() { | |
96 return keyIterator.hasNext(); | |
97 } | |
98 | |
99 public V next() { | |
100 return get(keyIterator.next()); | |
101 } | |
102 | |
103 public void remove() { | |
104 throw new UnsupportedOperationException(); | |
105 } | |
106 }; | |
107 } | |
108 }; | |
109 } | |
110 | |
111 public static <K, V> Mapping<K, V> createMapping(HashEquivalence<K> equivalence) { | |
112 return new OpenAddressingHashMapping<K, V>(equivalence); | |
113 } | |
114 | |
115 public static <K, V> Mapping<K, V> createIdentityMapping() { | |
116 final Class<HashIdentity<K>> type = null; | |
117 return createMapping(HashIdentity.instance(type)); | |
118 } | |
119 | |
120 public static <K, V> Mapping<K, V> createEqualityMapping() { | |
121 final Class<HashEquality<K>> type = null; | |
122 return createMapping(HashEquality.instance(type)); | |
123 } | |
124 | |
125 public static <K, V> Mapping<K, V> createVariableMapping(HashEquivalence<K> equivalence) { | |
126 return new ChainedHashMapping<K, V>(equivalence); | |
127 } | |
128 | |
129 public static <K, V> Mapping<K, V> createVariableIdentityMapping() { | |
130 final Class<HashIdentity<K>> type = null; | |
131 return createVariableMapping(HashIdentity.instance(type)); | |
132 } | |
133 | |
134 public static <K, V> Mapping<K, V> createVariableEqualityMapping() { | |
135 final Class<HashEquality<K>> type = null; | |
136 return createVariableMapping(HashEquality.instance(type)); | |
137 } | |
138 } |