Merge branch 'master' of dice:git/vm

This commit is contained in:
Joel Dice 2007-09-26 13:45:58 -06:00
commit 749ae86d49
30 changed files with 400 additions and 9 deletions

View File

@ -0,0 +1,25 @@
package java.io;
public class ByteArrayInputStream extends InputStream {
private final byte[] array;
private int position;
private final int length;
public ByteArrayInputStream(byte[] array, int offset, int length) {
this.array = array;
position = offset;
this.length = length;
}
public int read() {
if (position < length) {
return array[position++] & 0xff;
} else {
return -1;
}
}
public int available() {
return length - position;
}
}

View File

@ -0,0 +1,42 @@
package java.lang;
import java.lang.reflect.Method;
public abstract class Enum<E extends Enum<E>> {
private final String name;
private final int ordinal;
public Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
public int compareTo(E other) {
return ordinal - other.ordinal;
}
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
if (name != null) {
try {
Method method = enumType.getMethod("values");
Enum values[] = (Enum[])(method.invoke(null));
for (Enum value : values) {
if (name.equals(value.name)) {
return (T) value;
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
return null;
}
public int ordinal() {
return ordinal;
}
public String toString() {
return name;
}
}

View File

@ -0,0 +1,11 @@
package java.lang;
public class IncompatibleClassChangeError extends LinkageError {
public IncompatibleClassChangeError(String message) {
super(message);
}
public IncompatibleClassChangeError() {
super();
}
}

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Integer extends Number {
public final class Integer extends Number implements Comparable<Integer> {
public static final Class TYPE = Class.forCanonicalName("I");
public static final int MIN_VALUE = 0x80000000;
@ -28,6 +28,10 @@ public final class Integer extends Number {
return value;
}
public int compareTo(Integer other) {
return value - other.value;
}
public String toString() {
return toString(value);
}

View File

@ -2,6 +2,7 @@ package java.lang;
public final class Long extends Number {
public static final Class TYPE = Class.forCanonicalName("J");
public static final Long MAX_VALUE = 9223372036854775807l;
private final long value;
@ -13,6 +14,10 @@ public final class Long extends Number {
this.value = parseLong(s);
}
public static Long valueOf(String value) {
return new Long(value);
}
public static Long valueOf(long value) {
return new Long(value);
}

View File

@ -62,6 +62,11 @@ public final class Math {
return (int) Math.floor(v + 0.5);
}
public static double random() {
// TODO
throw new UnsupportedOperationException();
}
public static native double floor(double v);
public static native double ceil(double v);

View File

@ -0,0 +1,11 @@
package java.lang;
public class NoSuchFieldError extends IncompatibleClassChangeError {
public NoSuchFieldError(String message) {
super(message);
}
public NoSuchFieldError() {
super();
}
}

View File

@ -2,7 +2,7 @@ package java.lang;
public class Object {
protected Object clone() throws CloneNotSupportedException {
if (this instanceof Cloneable) {
if ((this instanceof Cloneable) || getClass().isArray()) {
return clone(this);
} else {
throw new CloneNotSupportedException(getClass().getName());

View File

@ -1,5 +1,6 @@
package java.lang;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
@ -31,7 +32,7 @@ public class Runtime {
}
}
public Process exec(String command) {
public Process exec(String command) throws IOException {
int[] process = new int[4];
exec(command, process);
return new MyProcess(process[0], process[1], process[2], process[3]);

View File

@ -2,6 +2,7 @@ package java.lang;
public final class Short extends Number {
public static final Class TYPE = Class.forCanonicalName("S");
public static final short MAX_VALUE = 32767;
private final short value;

View File

@ -0,0 +1,4 @@
package java.lang.annotation;
public interface Annotation {
}

View File

@ -13,7 +13,14 @@ public class ArrayList<T> implements List<T> {
public ArrayList() {
this(0);
}
public ArrayList(Collection<T> source) {
this(source.size());
for (T o : source) {
add(o);
}
}
private void grow() {
if (array == null || size >= array.length) {
resize(array == null ? MinimumCapacity : array.length * 2);
@ -60,6 +67,13 @@ public class ArrayList<T> implements List<T> {
return false;
}
public void add(int index, T element) {
size = Math.max(size+1, index+1);
grow();
System.arraycopy(array, index, array, index+1, size-index);
array[index] = element;
}
public boolean add(T element) {
++ size;
grow();
@ -93,6 +107,15 @@ public class ArrayList<T> implements List<T> {
}
}
public T set(int index, T element) {
if (index >= size) {
resize(index+1);
}
Object oldValue = array[index];
array[index] = element;
return (T) oldValue;
}
public T remove(int index) {
T v = get(index);
@ -118,6 +141,27 @@ public class ArrayList<T> implements List<T> {
return false;
}
public boolean isEmpty() {
return size() == 0;
}
public <S> S[] toArray(S[] a) {
Object[] retVal = null;
if (a.length >= size) {
retVal = a;
} else {
retVal = new Object[size];
}
for (int i = 0; i < size; ++i) {
retVal[i] = array[i];
}
if (a.length > size) {
a[size] = null;
}
return (S[])retVal;
}
public void clear() {
array = null;
size = 0;
@ -126,4 +170,8 @@ public class ArrayList<T> implements List<T> {
public Iterator<T> iterator() {
return new Collections.ArrayListIterator(this);
}
public ListIterator<T> listIterator(int index) {
return new Collections.ArrayListIterator(this, index);
}
}

View File

@ -25,6 +25,10 @@ public class Arrays {
throw new UnsupportedOperationException();
}
public void add(int index, T element) {
throw new UnsupportedOperationException();
}
public boolean contains(T element) {
for (int i = 0; i < array.length; ++i) {
if (equal(element, array[i])) {
@ -38,6 +42,14 @@ public class Arrays {
return array[index];
}
public <S> S[] toArray(S[] a) {
return (S[])array;
}
public boolean isEmpty() {
return size() == 0;
}
public T remove(int index) {
throw new UnsupportedOperationException();
}

View File

@ -34,6 +34,10 @@ public abstract class Calendar {
fields[field] = value;
}
public void setTime(Date date) {
// TODO
}
public abstract void roll(int field, boolean up);
public void roll(int field, int amount) {

View File

@ -33,8 +33,8 @@ public class Collections {
}
static class SynchronizedCollection<T> implements Collection<T> {
private final Object lock;
private final Collection<T> collection;
protected final Object lock;
protected final Collection<T> collection;
public SynchronizedCollection(Object lock, Collection<T> collection) {
this.lock = lock;
@ -73,6 +73,11 @@ public class Collections {
public SynchronizedSet(Object lock, Set<T> set) {
super(lock, set);
}
public void addAll(Collection<T> c) {
synchronized (lock) { ((Set<T>)collection).addAll(c); }
}
}
static class SynchronizedIterator<T> implements Iterator<T> {
@ -97,13 +102,31 @@ public class Collections {
}
}
static class ArrayListIterator<T> implements Iterator<T> {
static class ArrayListIterator<T> implements ListIterator<T> {
private final List<T> list;
private boolean canRemove = false;
private int index = -1;
private int index;
public ArrayListIterator(List<T> list) {
this(list, 0);
}
public ArrayListIterator(List<T> list, int index) {
this.list = list;
this.index = index - 1;
}
public boolean hasPrevious() {
return index >= 0;
}
public T previous() {
if (hasPrevious()) {
canRemove = true;
return list.get(index--);
} else {
throw new NoSuchElementException();
}
}
public T next() {

View File

@ -0,0 +1,5 @@
package java.util;
public interface Comparator<T> {
public int compare(T o1, T o2);
}

View File

@ -0,0 +1,17 @@
package java.util;
public class Date {
public final long when;
public Date() {
when = System.currentTimeMillis();
}
public Date(long when) {
this.when = when;
}
public long getTime() {
return when;
}
}

View File

@ -44,6 +44,10 @@ public class HashMap<K, V> implements Map<K, V> {
return r;
}
public boolean isEmpty() {
return size() == 0;
}
public int size() {
return size;
}
@ -179,6 +183,12 @@ public class HashMap<K, V> implements Map<K, V> {
return (c == null ? null : c.getValue());
}
public void putAll(Map<? extends K,? extends V> elts) {
for (Map.Entry<? extends K, ? extends V> entry : elts.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
public V remove(K key) {
Cell<K, V> c = removeCell(key);
return (c == null ? null : c.getValue());
@ -276,6 +286,10 @@ public class HashMap<K, V> implements Map<K, V> {
return HashMap.this.size();
}
public void addAll(Collection<Entry<K, V>> c) {
throw new UnsupportedOperationException();
}
public boolean contains(Entry<K, V> e) {
return containsKey(e.getKey());
}
@ -306,6 +320,10 @@ public class HashMap<K, V> implements Map<K, V> {
return containsKey(key);
}
public void addAll(Collection<K> c) {
throw new UnsupportedOperationException();
}
public boolean add(K key) {
return putCell(key, null) != null;
}

View File

@ -15,6 +15,10 @@ public class Hashtable<K, V> implements Map<K, V> {
return map.toString();
}
public synchronized boolean isEmpty() {
return map.isEmpty();
}
public synchronized int size() {
return map.size();
}
@ -35,6 +39,10 @@ public class Hashtable<K, V> implements Map<K, V> {
return map.put(key, value);
}
public synchronized void putAll(Map<? extends K,? extends V> elts) {
map.putAll(elts);
}
public synchronized V remove(K key) {
return map.remove(key);
}

View File

@ -11,6 +11,10 @@ public class IdentityHashMap<K, V> implements Map<K, V> {
this(0);
}
public boolean isEmpty() {
return map.isEmpty();
}
public int size() {
return map.size();
}
@ -31,6 +35,10 @@ public class IdentityHashMap<K, V> implements Map<K, V> {
return map.put(key, value);
}
public void putAll(Map<? extends K,? extends V> elts) {
map.putAll(elts);
}
public V remove(K key) {
return map.remove(key);
}

View File

@ -73,6 +73,23 @@ public class LinkedList<T> implements List<T> {
}
}
public <S> S[] toArray(S[] a) {
Object[] retVal = null;
if (a.length >= size) {
retVal = a;
} else {
retVal = new Object[size];
}
int i=0;
for (Object o : this) {
retVal[i++] = o;
}
if (a.length > size) {
a[size] = null;
}
return (S[])retVal;
}
public int size() {
return size;
}
@ -90,6 +107,16 @@ public class LinkedList<T> implements List<T> {
return true;
}
public void add(int index, T element) {
if (index == 0) {
addFirst(element);
} else {
Cell<T> cell = find(index);
Cell<T> newCell = new Cell<T>(element, cell.prev, cell);
cell.prev.next = newCell;
}
}
public void addFirst(T element) {
addFirst(new Cell(element, null, null));
}
@ -124,6 +151,10 @@ public class LinkedList<T> implements List<T> {
return c.value;
}
public boolean isEmpty() {
return size() == 0;
}
public T removeFirst() {
if (front != null) {
T v = front.value;

View File

@ -4,4 +4,12 @@ public interface List<T> extends Collection<T> {
public T get(int index);
public T remove(int index);
public boolean add(T element);
public void add(int index, T element);
public boolean isEmpty();
public <S> S[] toArray(S[] a);
}

View File

@ -0,0 +1,6 @@
package java.util;
public interface ListIterator<E> extends Iterator<E> {
public boolean hasPrevious();
public E previous();
}

View File

@ -1,6 +1,8 @@
package java.util;
public interface Map<K, V> {
public boolean isEmpty();
public int size();
public boolean containsKey(K key);
@ -11,6 +13,8 @@ public interface Map<K, V> {
public V put(K key, V value);
public void putAll(Map<? extends K,? extends V> elts);
public V remove(K key);
public void clear();

View File

@ -1,6 +1,7 @@
package java.util;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class Properties extends Hashtable {
@ -8,6 +9,18 @@ public class Properties extends Hashtable {
new Parser().parse(in, this);
}
public void store(OutputStream out, String comment) throws IOException {
// TODO
}
public String getProperty(String key) {
return (String)get(key);
}
public void setProperty(String key, String value) {
put(key, value);
}
private static class Parser {
private StringBuilder key = null;
private StringBuilder value = null;

View File

@ -1,3 +1,5 @@
package java.util;
public interface Set<T> extends Collection<T> { }
public interface Set<T> extends Collection<T> {
public void addAll(Collection<T> c);
}

View File

@ -11,6 +11,13 @@ public class Vector<T> implements List<T> {
this(0);
}
public Vector(List<T> list) {
this(list.size());
for (T o : list) {
add(o);
}
}
public synchronized int size() {
return list.size();
}
@ -19,6 +26,10 @@ public class Vector<T> implements List<T> {
return list.contains(element);
}
public synchronized void add(int index, T element) {
list.add(index, element);
}
public synchronized boolean add(T element) {
return list.add(element);
}
@ -39,6 +50,14 @@ public class Vector<T> implements List<T> {
return list.remove(index);
}
public synchronized boolean isEmpty() {
return list.isEmpty();
}
public synchronized <S> S[] toArray(S[] a) {
return list.toArray(a);
}
public T removeElementAt(int index) {
return remove(index);
}

View File

@ -26,6 +26,10 @@ public class WeakHashMap<K, V> implements Map<K, V> {
}
}
public boolean isEmpty() {
return map.isEmpty();
}
public int size() {
return map.size();
}
@ -50,6 +54,10 @@ public class WeakHashMap<K, V> implements Map<K, V> {
return map.put(key, value);
}
public void putAll(Map<? extends K,? extends V> elts) {
map.putAll(elts);
}
public V remove(K key) {
poll();
return map.remove(key);

View File

@ -54,6 +54,15 @@ public class Inflater {
this.length = length;
}
public void reset() {
// TODO
throw new UnsupportedOperationException();
}
public int inflate(byte[] output) throws DataFormatException {
return inflate(output, 0, output.length);
}
public int inflate(byte[] output, int offset, int length)
throws DataFormatException
{

39
test/Enums.java Normal file
View File

@ -0,0 +1,39 @@
public class Enums {
private enum Suit { CLUBS, HEARTS, SPADES, DIAMONDS };
private enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING };
private enum Person { Joe(4), Mike(5) ;
private final int age;
private Person(int age) {
this.age = age;
}
public int getAge() {
return age;
}
};
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
private static boolean checkFaceCard(Rank r) {
switch (r) {
case ACE:
case JACK:
case QUEEN:
case KING:
return true;
}
return false;
}
public static void main(String[] args) {
expect(Suit.CLUBS.ordinal() == 0);
expect(Suit.valueOf("DIAMONDS") == Suit.DIAMONDS);
System.out.println(Suit.SPADES);
expect(Suit.values()[1] == Suit.HEARTS);
expect(!checkFaceCard(Rank.FIVE));
expect(checkFaceCard(Rank.KING));
expect(Person.Mike.getAge() == 5);
}
}