more classpath classes

This commit is contained in:
Joel Dice 2007-07-21 16:36:51 -06:00
parent fd770fd884
commit 90d60b3459
13 changed files with 269 additions and 18 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,9 @@
package java.lang;
import java.util.Map;
public class InheritableThreadLocal<T> extends ThreadLocal<T> {
protected T childValue(T parentValue) {
return parentValue;
}
}

View File

@ -0,0 +1,7 @@
package java.lang;
import java.util.Iterator;
public interface Iterable<T> {
public Iterator<T> iterator();
}

View File

@ -12,7 +12,20 @@ public class Thread implements Runnable {
this.task = task;
}
public synchronized native void start();
public synchronized void start() {
Map<ThreadLocal, Object> map = currentThread().locals;
if (map != null) {
for (Map.Entry<ThreadLocal, Object> 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) {

View File

@ -0,0 +1,11 @@
package java.util;
public interface Collection<T> extends Iterable<T> {
public int size();
public boolean add(T entry);
public boolean remove(T entry);
public void clear();
}

View File

@ -118,28 +118,27 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
private Cell<K, V> putCell(K key, V value) {
Cell<K, V> 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<K, V> c = find(key);
return (c == null ? null : c.getValue());
}
public V put(K key, V value) {
Cell<K, V> 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<K, V> removeCell(K key) {
Cell<K, V> old = null;
if (key == null) {
if (nullCell != null) {
old = nullCell.getValue();
old = nullCell;
nullCell = null;
-- size;
}
@ -149,7 +148,7 @@ public class HashMap<K, V> implements Map<K, V> {
Cell<K, V> p = null;
for (Cell<K, V> 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<K, V> implements Map<K, V> {
return old;
}
public V put(K key, V value) {
Cell<K, V> c = putCell(key, value);
return (c == null ? null : c.getValue());
}
public V remove(K key) {
Cell<K, V> c = removeCell(key);
return (c == null ? null : c.getValue());
}
public void clear() {
array = null;
}
public Set<Entry<K, V>> entrySet() {
return new MySet();
}
Iterator<Entry<K, V>> iterator() {
return new MyIterator();
}
interface Cell<K, V> extends Entry<K, V> {
public HashMap.Cell<K, V> next();
@ -217,4 +238,85 @@ public class HashMap<K, V> implements Map<K, V> {
return new MyCell(key, value, next);
}
}
private class MySet implements Set<Entry<K, V>> {
public int size() {
return HashMap.this.size();
}
public boolean add(Entry<K, V> e) {
return putCell(e.getKey(), e.getValue()) != null;
}
public boolean remove(Entry<K, V> e) {
return removeCell(e.getKey()) != null;
}
public void clear() {
HashMap.this.clear();
}
public Iterator<Entry<K, V>> iterator() {
return new MyIterator();
}
}
private class MyIterator implements Iterator<Entry<K, V>> {
private int currentIndex = -1;
private int nextIndex = -1;
private Cell<K, V> previousCell;
private Cell<K, V> currentCell;
private Cell<K, V> nextCell;
public MyIterator() {
hasNext();
}
public Entry<K, V> 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();
}
}
}
}

View File

@ -0,0 +1,55 @@
package java.util;
public class HashSet<T> implements Set<T> {
private static final Object Value = new Object();
private final HashMap<T, Object> 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<T> iterator() {
return new MyIterator(map.iterator());
}
private static class MyIterator<T> implements Iterator<T> {
private final Iterator<Map.Entry<T, Object>> it;
public MyIterator(Iterator<Map.Entry<T, Object>> it) {
this.it = it;
}
public T next() {
return it.next().getKey();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
}

View File

@ -0,0 +1,9 @@
package java.util;
public interface Iterator<T> {
public T next();
public boolean hasNext();
public void remove();
}

View File

@ -9,6 +9,10 @@ public interface Map<K, V> {
public V remove(K key);
public void clear();
public Set<Entry<K, V>> entrySet();
public interface Entry<K, V> {
public K getKey();

View File

@ -0,0 +1,11 @@
package java.util;
public class NoSuchElementException extends RuntimeException {
public NoSuchElementException(String message) {
super(message);
}
public NoSuchElementException() {
super();
}
}

View File

@ -0,0 +1,3 @@
package java.util;
public interface Set<T> extends Collection<T> { }

View File

@ -45,6 +45,14 @@ public class WeakHashMap<K, V> implements Map<K, V> {
return map.remove(key);
}
public void clear() {
map.clear();
}
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
private static class MyCell<K, V>
extends WeakReference<K>
implements HashMap.Cell<K, V>

View File

@ -251,7 +251,7 @@ populate(Thread* t, object map)
{ "Java_java_lang_Runtiime_exit",
reinterpret_cast<void*>(exit) },
{ "Java_java_lang_Thread_start",
{ "Java_java_lang_Thread_doStart",
reinterpret_cast<void*>(start) },
{ "Java_java_lang_Thread_currentThread",
reinterpret_cast<void*>(currentThread) },