Permalink
Browse files

made collections implement java.util.Collection read-only interface

  • Loading branch information...
1 parent 0197214 commit 5c4b0b8323fe091f5a6c5a0af041a41403fbf3fb @richhickey richhickey committed Oct 18, 2007
View
@@ -20,7 +20,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
- <orderEntry type="library" exported="" name="asm-3.0" level="project" />
+ <orderEntry type="library" name="asm-3.0" level="project" />
<orderEntryProperties />
</component>
<component name="VcsManagerConfiguration">
@@ -10,7 +10,9 @@
package clojure.lang;
-public abstract class APersistentMap extends AFn implements IPersistentMap{
+import java.util.Collection;
+
+public abstract class APersistentMap extends AFn implements IPersistentMap, Collection{
int _hash = -1;
@@ -138,4 +140,78 @@ public ValSeq withMeta(IPersistentMap meta){
public Object invoke(Object arg1) throws Exception{
return valAt(arg1);
}
+
+// java.util.Collection implementation
+
+public Object[] toArray(){
+ return RT.seqToArray(seq());
+}
+
+public boolean add(Object o){
+ throw new UnsupportedOperationException();
+}
+
+public boolean remove(Object o){
+ throw new UnsupportedOperationException();
+}
+
+public boolean addAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public void clear(){
+ throw new UnsupportedOperationException();
+}
+
+public boolean retainAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public boolean removeAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public boolean containsAll(Collection c){
+ for(Object o : c)
+ {
+ if(contains(o))
+ return true;
+ }
+ return false;
+}
+
+public Object[] toArray(Object[] a){
+ if(a.length >= count())
+ {
+ ISeq s = seq();
+ for(int i = 0; s != null; ++i, s = s.rest())
+ {
+ a[i] = s.first();
+ }
+ if(a.length >= count())
+ a[count()] = null;
+ return a;
+ }
+ else
+ return toArray();
+}
+
+public int size(){
+ return count();
+}
+
+public boolean isEmpty(){
+ return count() == 0;
+}
+
+public boolean contains(Object o){
+ if(o instanceof IMapEntry)
+ {
+ IMapEntry e = (IMapEntry) o;
+ IMapEntry v = entryAt(e.key());
+ return (v != null && RT.equal(v.val(), e.val()));
+ }
+ return false;
+}
+
}
@@ -10,7 +10,10 @@
package clojure.lang;
-public abstract class ASeq extends Obj implements ISeq{
+import java.util.Collection;
+import java.util.Iterator;
+
+public abstract class ASeq extends Obj implements ISeq, Collection{
transient int _hash = -1;
@@ -71,4 +74,81 @@ public ISeq cons(Object o){
return new Cons(o, this);
}
+// java.util.Collection implementation
+
+public Object[] toArray(){
+ return RT.seqToArray(seq());
+}
+
+public boolean add(Object o){
+ throw new UnsupportedOperationException();
+}
+
+public boolean remove(Object o){
+ throw new UnsupportedOperationException();
+}
+
+public boolean addAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public void clear(){
+ throw new UnsupportedOperationException();
+}
+
+public boolean retainAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public boolean removeAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public boolean containsAll(Collection c){
+ for(Object o : c)
+ {
+ if(contains(o))
+ return true;
+ }
+ return false;
+}
+
+public Object[] toArray(Object[] a){
+ if(a.length >= count())
+ {
+ ISeq s = seq();
+ for(int i = 0; s != null; ++i, s = s.rest())
+ {
+ a[i] = s.first();
+ }
+ if(a.length >= count())
+ a[count()] = null;
+ return a;
+ }
+ else
+ return toArray();
+}
+
+public int size(){
+ return count();
+}
+
+public boolean isEmpty(){
+ return count() == 0;
+}
+
+public boolean contains(Object o){
+ for(ISeq s = seq(); s != null; s = s.rest())
+ {
+ if(RT.equal(s.first(), o))
+ return true;
+ }
+ return false;
+}
+
+
+public Iterator iterator(){
+ return new SeqIterator(this);
+}
+
}
@@ -10,7 +10,7 @@
* You must not remove this notice, or any other, from this software.
*/
public interface Associative extends IPersistentCollection{
-boolean contains(Object key);
+boolean containsKey(Object key);
IMapEntry entryAt(Object key);
@@ -190,7 +190,7 @@ public boolean hasJavaClass(){
}
static boolean isSpecial(Object sym){
- return specials.contains(sym);
+ return specials.containsKey(sym);
}
static Symbol resolveSymbol(Symbol sym){
@@ -753,8 +753,10 @@ public InstanceMethodExpr(int line, Expr target, String methodName, IPersistentV
{
List methods = Reflector.getMethods(target.getJavaClass(), args.count(), methodName, false);
if(methods.isEmpty())
- throw new IllegalArgumentException("No matching method found");
- method = (java.lang.reflect.Method) ((methods.size() == 1) ? methods.get(0) : null);
+ method = null;
+ //throw new IllegalArgumentException("No matching method found");
+ else
+ method = (java.lang.reflect.Method) ((methods.size() == 1) ? methods.get(0) : null);
}
else
method = null;
@@ -2008,7 +2010,7 @@ public Class getJavaClass() throws Exception{
}
private void emitLocal(GeneratorAdapter gen, LocalBinding lb){
- if(closes.contains(lb))
+ if(closes.containsKey(lb))
{
gen.loadThis();
gen.getField(fntype, lb.name, OBJECT_TYPE);
@@ -2509,7 +2511,7 @@ private static Expr analyzeSeq(C context, ISeq form, String name) throws Excepti
Integer line = (Integer) LINE.get();
try
{
- if(RT.meta(form) != null && RT.meta(form).contains(LispReader.LINE_KEY))
+ if(RT.meta(form) != null && RT.meta(form).containsKey(LispReader.LINE_KEY))
line = (Integer) RT.meta(form).valAt(LispReader.LINE_KEY);
Var.pushThreadBindings(
RT.map(LINE, line));
@@ -36,7 +36,7 @@ public Object val(){
return _val;
}
-public boolean contains(Object key){
+public boolean containsKey(Object key){
return RT.equal(_key, key);
}
@@ -26,7 +26,7 @@
public class PersistentArrayMap extends APersistentMap{
final Object[] array;
-static final int HASHTABLE_THRESHOLD = 16;
+static final int HASHTABLE_THRESHOLD = 8;
public static PersistentArrayMap EMPTY = new PersistentArrayMap();
@@ -65,7 +65,7 @@ public int count(){
return array.length / 2;
}
-public boolean contains(Object key){
+public boolean containsKey(Object key){
return indexOf(key) >= 0;
}
@@ -14,9 +14,6 @@
import java.util.*;
//this stuff is just for the test main()
-import java.util.regex.Pattern;
-import java.io.File;
-import java.io.FileNotFoundException;
/*
A persistent rendition of Phil Bagwell's Hash Array Mapped Trie
@@ -95,7 +92,7 @@ public PersistentHashMap(IPersistentMap meta, int count, INode root){
this.root = root;
}
-public boolean contains(Object key){
+public boolean containsKey(Object key){
return entryAt(key) != null;
}
@@ -119,7 +116,7 @@ public Object valAt(Object key){
}
public IPersistentMap assocEx(Object key, Object val) throws Exception{
- if(contains(key))
+ if(containsKey(key))
throw new Exception("Key already present");
return assoc(key, val);
}
@@ -12,6 +12,8 @@
import java.util.Queue;
import java.util.LinkedList;
+import java.util.Collection;
+import java.util.Iterator;
//import java.util.concurrent.ConcurrentLinkedQueue;
/**
@@ -21,7 +23,7 @@
* so no reversing or suspensions required for persistent use
*/
-public class PersistentQueue extends Obj implements IPersistentList{
+public class PersistentQueue extends Obj implements IPersistentList, Collection{
final public static PersistentQueue EMPTY = new PersistentQueue(null, null, null);
@@ -144,6 +146,82 @@ public Seq withMeta(IPersistentMap meta){
}
}
+// java.util.Collection implementation
+
+public Object[] toArray(){
+ return RT.seqToArray(seq());
+}
+
+public boolean add(Object o){
+ throw new UnsupportedOperationException();
+}
+
+public boolean remove(Object o){
+ throw new UnsupportedOperationException();
+}
+
+public boolean addAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public void clear(){
+ throw new UnsupportedOperationException();
+}
+
+public boolean retainAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public boolean removeAll(Collection c){
+ throw new UnsupportedOperationException();
+}
+
+public boolean containsAll(Collection c){
+ for(Object o : c)
+ {
+ if(contains(o))
+ return true;
+ }
+ return false;
+}
+
+public Object[] toArray(Object[] a){
+ if(a.length >= count())
+ {
+ ISeq s = seq();
+ for(int i = 0; s != null; ++i, s = s.rest())
+ {
+ a[i] = s.first();
+ }
+ if(a.length >= count())
+ a[count()] = null;
+ return a;
+ }
+ else
+ return toArray();
+}
+
+public int size(){
+ return count();
+}
+
+public boolean isEmpty(){
+ return count() == 0;
+}
+
+public boolean contains(Object o){
+ for(ISeq s = seq(); s != null; s = s.rest())
+ {
+ if(RT.equal(s.first(), o))
+ return true;
+ }
+ return false;
+}
+
+public Iterator iterator(){
+ return new SeqIterator(seq());
+}
+
/*
public static void main(String[] args){
if(args.length != 1)
Oops, something went wrong.

0 comments on commit 5c4b0b8

Please sign in to comment.