Skip to content

Commit

Permalink
made collections implement java.util.Collection read-only interface
Browse files Browse the repository at this point in the history
  • Loading branch information
richhickey committed Oct 18, 2007
1 parent 0197214 commit 5c4b0b8
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 72 deletions.
2 changes: 1 addition & 1 deletion clojure.iml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
78 changes: 77 additions & 1 deletion src/jvm/clojure/lang/APersistentMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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;
}

}
82 changes: 81 additions & 1 deletion src/jvm/clojure/lang/ASeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


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

}
2 changes: 1 addition & 1 deletion src/jvm/clojure/lang/Associative.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 7 additions & 5 deletions src/jvm/clojure/lang/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ interface IParser{
}

static boolean isSpecial(Object sym){
return specials.contains(sym);
return specials.containsKey(sym);
}

static Symbol resolveSymbol(Symbol sym){
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/jvm/clojure/lang/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Object val(){
return _val;
}

public boolean contains(Object key){
public boolean containsKey(Object key){
return RT.equal(_key, key);
}

Expand Down
4 changes: 2 additions & 2 deletions src/jvm/clojure/lang/PersistentArrayMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 2 additions & 5 deletions src/jvm/clojure/lang/PersistentHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down
80 changes: 79 additions & 1 deletion src/jvm/clojure/lang/PersistentQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 5c4b0b8

Please sign in to comment.