1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

upgraded CharArrayMap to support generics, also enabled Java 5 in cdt.core.tests

This commit is contained in:
Mike Kucera 2008-01-02 15:42:53 +00:00
parent 583b8dadc0
commit b388adcbe5
5 changed files with 173 additions and 133 deletions

View file

@ -7,7 +7,7 @@
<classpathentry kind="src" path="suite"/> <classpathentry kind="src" path="suite"/>
<classpathentry kind="src" path="regression"/> <classpathentry kind="src" path="regression"/>
<classpathentry kind="src" path="templateengine"/> <classpathentry kind="src" path="templateengine"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View file

@ -1,9 +1,9 @@
#Mon Oct 09 10:54:22 CEST 2006 #Wed Jan 02 10:16:52 EST 2008
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.4 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.4 org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
@ -15,7 +15,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
@ -59,4 +59,4 @@ org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=di
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.4 org.eclipse.jdt.core.compiler.source=1.5

View file

@ -64,9 +64,9 @@ public class CharArrayMapTest extends TestCase {
private static long timeMap(char[][] keys) { private static long timeMap(char[][] keys) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
CharArrayMap/*<Integer>*/ map = new CharArrayMap/*<Integer>*/(keys.length); CharArrayMap<Integer> map = new CharArrayMap<Integer>(keys.length);
for(int i = 0; i < keys.length; i++) { for(int i = 0; i < keys.length; i++) {
map.put(keys[i], new Integer(i)); map.put(keys[i], i);
} }
assertEquals(keys.length, map.size()); assertEquals(keys.length, map.size());
for(int i = 0; i < keys.length; i++) { for(int i = 0; i < keys.length; i++) {
@ -96,14 +96,14 @@ public class CharArrayMapTest extends TestCase {
char[] key3 = "third key".toCharArray(); char[] key3 = "third key".toCharArray();
char[] key4 = "forth key".toCharArray(); char[] key4 = "forth key".toCharArray();
CharArrayMap/*<Integer>*/ map = new CharArrayMap/*<Integer>*/(); CharArrayMap<Integer> map = new CharArrayMap<Integer>();
assertTrue(map.isEmpty()); assertTrue(map.isEmpty());
assertEquals(0, map.size()); assertEquals(0, map.size());
map.put(key1, new Integer(1)); map.put(key1, 1);
map.put(key2, new Integer(2)); map.put(key2, 2);
map.put(key3, new Integer(3)); map.put(key3, 3);
map.put(key4, new Integer(4)); map.put(key4, 4);
assertFalse(map.isEmpty()); assertFalse(map.isEmpty());
assertEquals(4, map.size()); assertEquals(4, map.size());
@ -118,19 +118,19 @@ public class CharArrayMapTest extends TestCase {
assertTrue(map.containsKey(key3)); assertTrue(map.containsKey(key3));
assertTrue(map.containsKey(key4)); assertTrue(map.containsKey(key4));
assertTrue(map.containsValue(new Integer(1))); assertTrue(map.containsValue(1));
assertTrue(map.containsValue(new Integer(2))); assertTrue(map.containsValue(2));
assertTrue(map.containsValue(new Integer(3))); assertTrue(map.containsValue(3));
assertTrue(map.containsValue(new Integer(4))); assertTrue(map.containsValue(4));
Set/*<Integer>*/ values = new HashSet/*<Integer>*/(); Set<Integer> values = new HashSet<Integer>();
values.add(new Integer(1)); values.add(1);
values.add(new Integer(2)); values.add(2);
values.add(new Integer(3)); values.add(3);
values.add(new Integer(4)); values.add(4);
Collection c = map.values();
for(Iterator iter = c.iterator(); iter.hasNext();) { for(int i : map.values()) {
assertTrue(values.remove(iter.next())); assertTrue(values.remove(i));
} }
// remove a mapping // remove a mapping
@ -138,7 +138,7 @@ public class CharArrayMapTest extends TestCase {
assertEquals(3, map.size()); assertEquals(3, map.size());
assertNull(map.get(key1)); assertNull(map.get(key1));
assertFalse(map.containsKey(key1)); assertFalse(map.containsKey(key1));
assertFalse(map.containsValue(new Integer(1))); assertFalse(map.containsValue(1));
assertNull(map.remove(key1)); // its already removed assertNull(map.remove(key1)); // its already removed
map.clear(); map.clear();
@ -153,17 +153,17 @@ public class CharArrayMapTest extends TestCase {
assertTrue(map.containsValue(null)); assertTrue(map.containsValue(null));
// overrideing values should // overrideing values should
map.put(key1, new Integer(100)); map.put(key1, 100);
assertEquals(1, map.size()); assertEquals(1, map.size());
assertEquals(new Integer(100), map.get(key1)); assertEquals(new Integer(100), map.get(key1));
assertTrue(map.containsValue(new Integer(100))); assertTrue(map.containsValue(100));
assertFalse(map.containsValue(null)); assertFalse(map.containsValue(null));
// override the value // override the value
map.put(key1, new Integer(200)); map.put(key1, 200);
assertEquals(1, map.size()); assertEquals(1, map.size());
assertEquals(new Integer(200), map.get(key1)); assertEquals(new Integer(200), map.get(key1));
assertTrue(map.containsValue(new Integer(200))); assertTrue(map.containsValue(200));
assertFalse(map.containsValue(new Integer(100))); assertFalse(map.containsValue(100));
} }
@ -189,13 +189,13 @@ public class CharArrayMapTest extends TestCase {
keys[6] = "carcass".toCharArray(); keys[6] = "carcass".toCharArray();
CharArrayMap/*<Integer>*/ map = new CharArrayMap/*<Integer>*/(); CharArrayMap<Integer> map = new CharArrayMap<Integer>();
assertTrue(map.isEmpty()); assertTrue(map.isEmpty());
assertEquals(0, map.size()); assertEquals(0, map.size());
for(int i = 0; i < slices.length; i++) { for(int i = 0; i < slices.length; i++) {
Slice slice = slices[i]; Slice slice = slices[i];
map.put(slice.chars, slice.start, slice.length, new Integer(i)); map.put(slice.chars, slice.start, slice.length, i);
} }
assertFalse(map.isEmpty()); assertFalse(map.isEmpty());
@ -208,16 +208,16 @@ public class CharArrayMapTest extends TestCase {
assertEquals(new Integer(i), map.get(keys[i])); assertEquals(new Integer(i), map.get(keys[i]));
assertTrue(map.containsKey(slice.chars, slice.start, slice.length)); assertTrue(map.containsKey(slice.chars, slice.start, slice.length));
assertTrue(map.containsKey(keys[i])); assertTrue(map.containsKey(keys[i]));
assertTrue(map.containsValue(new Integer(i))); assertTrue(map.containsValue(i));
} }
Set/*<Integer>*/ values = new HashSet/*<Integer>*/(); Set<Integer> values = new HashSet<Integer>();
for(int i = 0; i < keys.length; i++) { for(int i = 0; i < keys.length; i++) {
values.add(new Integer(i)); values.add(i);
} }
Collection c = map.values();
for(Iterator iter = c.iterator(); iter.hasNext();) { for(int i : map.values()) {
assertTrue(values.remove(iter.next())); assertTrue(values.remove(i));
} }
// remove the last two keys // remove the last two keys
@ -233,7 +233,7 @@ public class CharArrayMapTest extends TestCase {
assertEquals(new Integer(i), map.get(keys[i])); assertEquals(new Integer(i), map.get(keys[i]));
assertTrue(map.containsKey(slice.chars, slice.start, slice.length)); assertTrue(map.containsKey(slice.chars, slice.start, slice.length));
assertTrue(map.containsKey(keys[i])); assertTrue(map.containsKey(keys[i]));
assertTrue(map.containsValue(new Integer(i))); assertTrue(map.containsValue(i));
} }
map.clear(); map.clear();
@ -244,7 +244,7 @@ public class CharArrayMapTest extends TestCase {
public void testProperFail() { public void testProperFail() {
char[] hello = "hello".toCharArray(); char[] hello = "hello".toCharArray();
CharArrayMap/*<Integer>*/ map = new CharArrayMap/*<Integer>*/(); CharArrayMap<Integer> map = new CharArrayMap<Integer>();
Integer value = new Integer(9); Integer value = new Integer(9);
@ -333,7 +333,7 @@ public class CharArrayMapTest extends TestCase {
try { try {
new CharArrayMap/*<Integer>*/(-1); new CharArrayMap<Integer>(-1);
} catch(IllegalArgumentException _) {} } catch(IllegalArgumentException _) {}
} }
} }

View file

@ -22,7 +22,7 @@ import java.util.Map;
* section of a char[] as the key (a slice), and one that uses * section of a char[] as the key (a slice), and one that uses
* the entire char[] as the key. * the entire char[] as the key.
* *
* ex) * ex:
* char[] key = "one two three".toCharArray(); * char[] key = "one two three".toCharArray();
* map.put(key, 4, 3, new Integer(99)); * map.put(key, 4, 3, new Integer(99));
* map.get(key, 4, 3); // returns 99 * map.get(key, 4, 3); // returns 99
@ -31,7 +31,7 @@ import java.util.Map;
* *
* @author Mike Kucera * @author Mike Kucera
*/ */
public final class CharArrayMap/*<V>*/ { public final class CharArrayMap<V> implements ICharArrayMap<V> {
/** /**
* Wrapper class used as keys in the map. The purpose * Wrapper class used as keys in the map. The purpose
@ -63,7 +63,7 @@ public final class CharArrayMap/*<V>*/ {
this.start = 0; this.start = 0;
} }
public boolean equals(Object x) { @Override public boolean equals(Object x) {
if(this == x) if(this == x)
return true; return true;
if(!(x instanceof Key)) if(!(x instanceof Key))
@ -81,7 +81,7 @@ public final class CharArrayMap/*<V>*/ {
return true; return true;
} }
public int hashCode() { @Override public int hashCode() {
int result = 17; int result = 17;
for(int i = start; i < start+length; i++) { for(int i = start; i < start+length; i++) {
result = 37 * result + buffer[i]; result = 37 * result + buffer[i];
@ -89,7 +89,7 @@ public final class CharArrayMap/*<V>*/ {
return result; return result;
} }
public String toString() { @Override public String toString() {
return "'" + new String(buffer, start, length) + "'@(" + start + "," + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ return "'" + new String(buffer, start, length) + "'@(" + start + "," + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
} }
@ -97,29 +97,28 @@ public final class CharArrayMap/*<V>*/ {
/** /**
* Used to enforce preconditions. Note that the NPE thats thrown by * Used to enforce preconditions.
* mutator methods is thrown from the Key constructor. *
* Note that the NPE thrown by mutator methods is thrown from the Key constructor.
*
* @throws IndexOutOfBoundsException if boundaries are wrong in any way
*/ */
private static void checkBoundaries(char[] chars, int start, int length) { private static void checkBoundaries(char[] chars, int start, int length) {
if(start < 0) if(start < 0 || length < 0 || start >= chars.length || start + length > chars.length)
throw new IndexOutOfBoundsException("start must be non-negative, got: " + start);//$NON-NLS-1$ throw new IndexOutOfBoundsException("Buffer length: " + chars.length + //$NON-NLS-1$
if(length < 0) ", Start index: " + start + //$NON-NLS-1$
throw new IndexOutOfBoundsException("length must be non-negative, got: " + length);//$NON-NLS-1$ ", Length: " + length); //$NON-NLS-1$
if(start >= chars.length)
throw new IndexOutOfBoundsException("start is out of bounds, got: " + start);//$NON-NLS-1$
if(start + length > chars.length)
throw new IndexOutOfBoundsException("end is out of bounds, got: " + (start+length));//$NON-NLS-1$
} }
private final Map/*<Key,V>*/ map; private final Map<Key,V> map;
/** /**
* Constructs an empty CharArrayMap with default initial capacity. * Constructs an empty CharArrayMap with default initial capacity.
*/ */
public CharArrayMap() { public CharArrayMap() {
map = new HashMap/*<Key,V>*/(); map = new HashMap<Key,V>();
} }
/** /**
@ -127,127 +126,74 @@ public final class CharArrayMap/*<V>*/ {
* @throws IllegalArgumentException if the initial capacity is negative * @throws IllegalArgumentException if the initial capacity is negative
*/ */
public CharArrayMap(int initialCapacity) { public CharArrayMap(int initialCapacity) {
map = new HashMap/*<Key,V>*/(initialCapacity); map = new HashMap<Key,V>(initialCapacity);
} }
/** public void put(char[] chars, int start, int length, V value) {
* Creates a new mapping in this map, uses the given array slice as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
public void put(char[] chars, int start, int length, /*V*/Object value) {
checkBoundaries(chars, start, length); checkBoundaries(chars, start, length);
map.put(new Key(chars, start, length), value); map.put(new Key(chars, start, length), value);
} }
/**
* Creates a new mapping in this map, uses all of the given array as the key. public void put(char[] chars, V value) {
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
*/
public void put(char[] chars, /*V*/Object value) {
map.put(new Key(chars), value); map.put(new Key(chars), value);
} }
/**
* Returns the value to which the specified array slice is mapped in this map, public V get(char[] chars, int start, int length) {
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
public /*V*/Object get(char[] chars, int start, int length) {
checkBoundaries(chars, start, length); checkBoundaries(chars, start, length);
return map.get(new Key(chars, start, length)); return map.get(new Key(chars, start, length));
} }
/**
* Returns the value to which the specified array is mapped in this map, public V get(char[] chars) {
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
*/
public /*V*/Object get(char[] chars) {
return map.get(new Key(chars)); return map.get(new Key(chars));
} }
/** public V remove(char[] chars, int start, int length) {
* Removes the mapping for the given array slice if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
public /*V*/Object remove(char[] chars, int start, int length) {
checkBoundaries(chars, start, length); checkBoundaries(chars, start, length);
return map.remove(new Key(chars, start, length)); return map.remove(new Key(chars, start, length));
} }
/** public V remove(char[] chars) {
* Removes the mapping for the given array if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
public /*V*/Object remove(char[] chars) {
return map.remove(new Key(chars)); return map.remove(new Key(chars));
} }
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
public boolean containsKey(char[] chars, int start, int length) { public boolean containsKey(char[] chars, int start, int length) {
checkBoundaries(chars, start, length); checkBoundaries(chars, start, length);
return map.containsKey(new Key(chars, start, length)); return map.containsKey(new Key(chars, start, length));
} }
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
public boolean containsKey(char[] chars) { public boolean containsKey(char[] chars) {
return map.containsKey(new Key(chars)); return map.containsKey(new Key(chars));
} }
/**
* Returns true if the given value is contained in the map. public boolean containsValue(V value) {
*/
public boolean containsValue(/*V*/Object value) {
return map.containsValue(value); return map.containsValue(value);
} }
/** public Collection<V> values() {
* Use this in a foreach loop.
*/
public Collection/*<V>*/ values() {
return map.values(); return map.values();
} }
/**
* Removes all mappings from the map.
*/
public void clear() { public void clear() {
map.clear(); map.clear();
} }
/**
* Returns the number of mappings.
*/
public int size() { public int size() {
return map.size(); return map.size();
} }
/**
* Returns true if the map is empty.
*/
public boolean isEmpty() { public boolean isEmpty() {
return map.isEmpty(); return map.isEmpty();
} }
@ -256,7 +202,7 @@ public final class CharArrayMap/*<V>*/ {
/** /**
* Returns a String representation of the map. * Returns a String representation of the map.
*/ */
public String toString() { @Override public String toString() {
return map.toString(); return map.toString();
} }

View file

@ -0,0 +1,94 @@
package org.eclipse.cdt.core.parser.util;
import java.util.Collection;
public interface ICharArrayMap<V> {
/**
* Creates a new mapping in this map, uses the given array slice as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
void put(char[] chars, int start, int length, V value);
/**
* Creates a new mapping in this map, uses all of the given array as the key.
* If the map previously contained a mapping for this key, the old value is replaced.
* @throws NullPointerException if chars is null
*/
void put(char[] chars, V value);
/**
* Returns the value to which the specified array slice is mapped in this map,
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
V get(char[] chars, int start, int length);
/**
* Returns the value to which the specified array is mapped in this map,
* or null if the map contains no mapping for this key.
* @throws NullPointerException if chars is null
*/
V get(char[] chars);
/**
* Removes the mapping for the given array slice if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
V remove(char[] chars, int start, int length);
/**
* Removes the mapping for the given array if present.
* Returns the value object that corresponded to the key
* or null if the key was not in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
V remove(char[] chars);
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
boolean containsKey(char[] chars, int start, int length);
/**
* Returns true if the given key has a value associated with it in the map.
* @throws NullPointerException if chars is null
* @throws IllegalArgumentException if the boundaries specified by start and length are out of range
*/
boolean containsKey(char[] chars);
/**
* Returns true if the given value is contained in the map.
*/
boolean containsValue(V value);
/**
* Use this in a foreach loop.
*/
Collection<V> values();
/**
* Removes all mappings from the map.
*/
void clear();
/**
* Returns the number of mappings.
*/
int size();
/**
* Returns true if the map is empty.
*/
boolean isEmpty();
}