From 90d60b34590075a143097c89c02094b8209778b5 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 21 Jul 2007 16:36:51 -0600 Subject: [PATCH] more classpath classes --- .../java/lang/IllegalStateException.java | 19 +++ .../java/lang/InheritableThreadLocal.java | 9 ++ classpath/java/lang/Iterable.java | 7 + classpath/java/lang/Thread.java | 15 +- classpath/java/util/Collection.java | 11 ++ classpath/java/util/HashMap.java | 134 +++++++++++++++--- classpath/java/util/HashSet.java | 55 +++++++ classpath/java/util/Iterator.java | 9 ++ classpath/java/util/Map.java | 4 + .../java/util/NoSuchElementException.java | 11 ++ classpath/java/util/Set.java | 3 + classpath/java/util/WeakHashMap.java | 8 ++ src/builtin.cpp | 2 +- 13 files changed, 269 insertions(+), 18 deletions(-) create mode 100644 classpath/java/lang/IllegalStateException.java create mode 100644 classpath/java/lang/InheritableThreadLocal.java create mode 100644 classpath/java/lang/Iterable.java create mode 100644 classpath/java/util/Collection.java create mode 100644 classpath/java/util/HashSet.java create mode 100644 classpath/java/util/Iterator.java create mode 100644 classpath/java/util/NoSuchElementException.java create mode 100644 classpath/java/util/Set.java diff --git a/classpath/java/lang/IllegalStateException.java b/classpath/java/lang/IllegalStateException.java new file mode 100644 index 0000000000..c111a55398 --- /dev/null +++ b/classpath/java/lang/IllegalStateException.java @@ -0,0 +1,19 @@ +package java.lang; + +public class IllegalStateException extends RuntimeException { + public IllegalStateException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalStateException(String message) { + this(message, null); + } + + public IllegalStateException(Throwable cause) { + this(null, cause); + } + + public IllegalStateException() { + this(null, null); + } +} diff --git a/classpath/java/lang/InheritableThreadLocal.java b/classpath/java/lang/InheritableThreadLocal.java new file mode 100644 index 0000000000..826f749cd3 --- /dev/null +++ b/classpath/java/lang/InheritableThreadLocal.java @@ -0,0 +1,9 @@ +package java.lang; + +import java.util.Map; + +public class InheritableThreadLocal extends ThreadLocal { + protected T childValue(T parentValue) { + return parentValue; + } +} diff --git a/classpath/java/lang/Iterable.java b/classpath/java/lang/Iterable.java new file mode 100644 index 0000000000..26cc2e112e --- /dev/null +++ b/classpath/java/lang/Iterable.java @@ -0,0 +1,7 @@ +package java.lang; + +import java.util.Iterator; + +public interface Iterable { + public Iterator iterator(); +} diff --git a/classpath/java/lang/Thread.java b/classpath/java/lang/Thread.java index 20a95cad01..5bd884f04e 100644 --- a/classpath/java/lang/Thread.java +++ b/classpath/java/lang/Thread.java @@ -12,7 +12,20 @@ public class Thread implements Runnable { this.task = task; } - public synchronized native void start(); + public synchronized void start() { + Map map = currentThread().locals; + if (map != null) { + for (Map.Entry e: map.entrySet()) { + if (e.getKey() instanceof InheritableThreadLocal) { + locals().put(e.getKey(), e.getValue()); + } + } + } + + doStart(); + } + + private native void doStart(); public void run() { if (task != null) { diff --git a/classpath/java/util/Collection.java b/classpath/java/util/Collection.java new file mode 100644 index 0000000000..1a79f11c62 --- /dev/null +++ b/classpath/java/util/Collection.java @@ -0,0 +1,11 @@ +package java.util; + +public interface Collection extends Iterable { + public int size(); + + public boolean add(T entry); + + public boolean remove(T entry); + + public void clear(); +} diff --git a/classpath/java/util/HashMap.java b/classpath/java/util/HashMap.java index 5eff151671..abe6a3b96e 100644 --- a/classpath/java/util/HashMap.java +++ b/classpath/java/util/HashMap.java @@ -118,28 +118,27 @@ public class HashMap implements Map { } } + private Cell putCell(K key, V value) { + Cell c = find(key); + if (c == null) { + insert(factory.make(key, value, null)); + } else { + V old = c.getValue(); + c.setValue(value); + } + return c; + } + public V get(K key) { Cell c = find(key); return (c == null ? null : c.getValue()); } - public V put(K key, V value) { - Cell c = find(key); - if (c == null) { - insert(factory.make(key, value, null)); - return null; - } else { - V old = c.getValue(); - c.setValue(value); - return old; - } - } - - public V remove(K key) { - V old = null; + public Cell removeCell(K key) { + Cell old = null; if (key == null) { if (nullCell != null) { - old = nullCell.getValue(); + old = nullCell; nullCell = null; -- size; } @@ -149,7 +148,7 @@ public class HashMap implements Map { Cell p = null; for (Cell c = array[index]; c != null; c = c.next()) { if (key.equals(c.getKey())) { - old = c.getValue(); + old = c; if (p == null) { array[index] = c.next(); } else { @@ -166,6 +165,28 @@ public class HashMap implements Map { return old; } + public V put(K key, V value) { + Cell c = putCell(key, value); + return (c == null ? null : c.getValue()); + } + + public V remove(K key) { + Cell c = removeCell(key); + return (c == null ? null : c.getValue()); + } + + public void clear() { + array = null; + } + + public Set> entrySet() { + return new MySet(); + } + + Iterator> iterator() { + return new MyIterator(); + } + interface Cell extends Entry { public HashMap.Cell next(); @@ -217,4 +238,85 @@ public class HashMap implements Map { return new MyCell(key, value, next); } } + + private class MySet implements Set> { + public int size() { + return HashMap.this.size(); + } + + public boolean add(Entry e) { + return putCell(e.getKey(), e.getValue()) != null; + } + + public boolean remove(Entry e) { + return removeCell(e.getKey()) != null; + } + + public void clear() { + HashMap.this.clear(); + } + + public Iterator> iterator() { + return new MyIterator(); + } + } + + private class MyIterator implements Iterator> { + private int currentIndex = -1; + private int nextIndex = -1; + private Cell previousCell; + private Cell currentCell; + private Cell nextCell; + + public MyIterator() { + hasNext(); + } + + public Entry next() { + if (hasNext()) { + if (currentCell != null && currentCell.next() != null) { + previousCell = currentCell; + } else { + previousCell = null; + } + + currentCell = nextCell; + currentIndex = nextIndex; + + nextCell = nextCell.next(); + + return currentCell; + } else { + throw new NoSuchElementException(); + } + } + + public boolean hasNext() { + if (array != null) { + while (nextCell == null && ++ nextIndex < array.length) { + if (array[nextIndex] != null) { + nextCell = array[nextIndex]; + return true; + } + } + } + return false; + } + + public void remove() { + if (currentCell != null) { + if (previousCell == null) { + array[currentIndex] = currentCell.next(); + } else { + previousCell.setNext(currentCell.next()); + if (previousCell.next() == null) { + previousCell = null; + } + } + currentCell = null; + } else { + throw new IllegalStateException(); + } + } + } } diff --git a/classpath/java/util/HashSet.java b/classpath/java/util/HashSet.java new file mode 100644 index 0000000000..c685417db6 --- /dev/null +++ b/classpath/java/util/HashSet.java @@ -0,0 +1,55 @@ +package java.util; + +public class HashSet implements Set { + private static final Object Value = new Object(); + + private final HashMap map; + + public HashSet(int capacity) { + map = new HashMap(capacity); + } + + public HashSet() { + this(0); + } + + public int size() { + return map.size(); + } + + public boolean add(T element) { + return map.put(element, Value) != Value; + } + + public boolean remove(T element) { + return map.remove(element) != Value; + } + + public void clear() { + map.clear(); + } + + public Iterator iterator() { + return new MyIterator(map.iterator()); + } + + private static class MyIterator implements Iterator { + private final Iterator> it; + + public MyIterator(Iterator> it) { + this.it = it; + } + + public T next() { + return it.next().getKey(); + } + + public boolean hasNext() { + return it.hasNext(); + } + + public void remove() { + it.remove(); + } + } +} diff --git a/classpath/java/util/Iterator.java b/classpath/java/util/Iterator.java new file mode 100644 index 0000000000..c0e611d5af --- /dev/null +++ b/classpath/java/util/Iterator.java @@ -0,0 +1,9 @@ +package java.util; + +public interface Iterator { + public T next(); + + public boolean hasNext(); + + public void remove(); +} diff --git a/classpath/java/util/Map.java b/classpath/java/util/Map.java index 2be7083a45..03c632a307 100644 --- a/classpath/java/util/Map.java +++ b/classpath/java/util/Map.java @@ -9,6 +9,10 @@ public interface Map { public V remove(K key); + public void clear(); + + public Set> entrySet(); + public interface Entry { public K getKey(); diff --git a/classpath/java/util/NoSuchElementException.java b/classpath/java/util/NoSuchElementException.java new file mode 100644 index 0000000000..cf7e19d7f4 --- /dev/null +++ b/classpath/java/util/NoSuchElementException.java @@ -0,0 +1,11 @@ +package java.util; + +public class NoSuchElementException extends RuntimeException { + public NoSuchElementException(String message) { + super(message); + } + + public NoSuchElementException() { + super(); + } +} diff --git a/classpath/java/util/Set.java b/classpath/java/util/Set.java new file mode 100644 index 0000000000..d01a5ec704 --- /dev/null +++ b/classpath/java/util/Set.java @@ -0,0 +1,3 @@ +package java.util; + +public interface Set extends Collection { } diff --git a/classpath/java/util/WeakHashMap.java b/classpath/java/util/WeakHashMap.java index 716ab340f8..08050b1aaa 100644 --- a/classpath/java/util/WeakHashMap.java +++ b/classpath/java/util/WeakHashMap.java @@ -45,6 +45,14 @@ public class WeakHashMap implements Map { return map.remove(key); } + public void clear() { + map.clear(); + } + + public Set> entrySet() { + return map.entrySet(); + } + private static class MyCell extends WeakReference implements HashMap.Cell diff --git a/src/builtin.cpp b/src/builtin.cpp index 89096d9ab9..1bfeb5b596 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -251,7 +251,7 @@ populate(Thread* t, object map) { "Java_java_lang_Runtiime_exit", reinterpret_cast(exit) }, - { "Java_java_lang_Thread_start", + { "Java_java_lang_Thread_doStart", reinterpret_cast(start) }, { "Java_java_lang_Thread_currentThread", reinterpret_cast(currentThread) },