Created
October 31, 2008 15:26
-
-
Save dscataglini/21327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junivi.lang; | |
public interface Block { | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junivi.lang; | |
public interface BooleanBlock extends Block{ | |
public boolean yield(Object[] elements); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junivi.lang; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Iterator; | |
public class EnumerableArray extends ArrayList implements IEnumerable, RubyArray{ | |
public EnumerableArray(){ | |
super(); | |
} | |
public EnumerableArray(int initialCapacity){ | |
super(initialCapacity); | |
} | |
public EnumerableArray(Collection c){ | |
super(c); | |
} | |
public Object find(Object def, Block matcher){ | |
Object returned = matchCollectionAgainstExpectations(matcher, true); | |
return (returned == null) ? def : returned; | |
} | |
public Object find(Block matcher){ | |
return find(null, matcher); | |
} | |
public Object detect(Object def, Block matcher) { | |
return find(def, matcher); | |
} | |
public Object detect(Block matcher) { | |
return find(matcher); | |
} | |
public boolean all(Block matcher) { | |
if(size() == 0) return false; | |
return (matchCollectionAgainstExpectations(matcher, false) == null) ? true : false; | |
} | |
public boolean any(Block matcher) { | |
if(size() == 0) return false; | |
return (matchCollectionAgainstExpectations(matcher, true) == null) ? false : true; | |
} | |
private Object matchCollectionAgainstExpectations(Block matcher, boolean expected) { | |
Iterator iter = iterator(); | |
while(iter.hasNext()){ | |
Object current = iter.next(); | |
if(((BooleanBlock) matcher).yield(new Object[]{current}) == expected) | |
return current; | |
} | |
return null; | |
} | |
public boolean include(Object o) { | |
return contains(o); | |
} | |
public boolean member(Object o) { | |
return contains(o); | |
} | |
public IEnumerable sort() { | |
Object[] a = this.toArray(); | |
Arrays.sort(a); | |
return new EnumerableArray(Arrays.asList(a)); | |
} | |
public Object first(){ | |
return get(0); | |
} | |
public IEnumerable entries() { | |
return this; | |
} | |
public IEnumerable to_a() { | |
return this; | |
} | |
public IEnumerable reject(Block matcher) { | |
return getAllMatches(matcher, false); | |
} | |
public IEnumerable find_all(Block matcher) { | |
return getAllMatches(matcher, true); | |
} | |
public IEnumerable select(Block matcher) { | |
return find_all(matcher); | |
} | |
private IEnumerable getAllMatches(Block matcher, boolean expected) { | |
EnumerableArray arr = new EnumerableArray(); | |
Iterator iter = iterator(); | |
while(iter.hasNext()){ | |
Object current = iter.next(); | |
if(((BooleanBlock) matcher).yield(new Object[]{current}) == expected) | |
arr.add(current); | |
} | |
return arr; | |
} | |
private IEnumerable getAllMatches(ObjectBlock matcher) { | |
EnumerableArray arr = new EnumerableArray(); | |
Iterator iter = iterator(); | |
while(iter.hasNext()) | |
arr.add(matcher.yield(new Object[]{iter.next()})); | |
return arr; | |
} | |
public IEnumerable collect(Block matcher) { | |
return getAllMatches((ObjectBlock) matcher); | |
} | |
public IEnumerable map(Block matcher) { | |
return collect(matcher); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junivi.lang; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import junit.framework.TestCase; | |
public class EnumerableArrayTest extends TestCase { | |
private EnumerableArray emptyArray = new EnumerableArray(); | |
private EnumerableArray stringArray = new EnumerableArray(Arrays.asList(new String[]{"3","2","1"})); | |
private EnumerableArray mixedArray = new EnumerableArray(Arrays.asList(new Object[]{"1", new HashSet(), new MyObject(), new ArrayList()})); | |
public void setUp(){ | |
} | |
public void testEnumerableArray() { | |
EnumerableArray arr = new EnumerableArray(); | |
assertNotNull(arr); | |
} | |
public void testEnumerableArrayInt() { | |
EnumerableArray arr = new EnumerableArray(5); | |
assertNotNull(arr); | |
} | |
public void testEnumerableArrayCollection() { | |
EnumerableArray arr = new EnumerableArray(Arrays.asList(new String[]{"1","2","3"})); | |
assertNotNull(arr); | |
assertEquals(3, arr.size()); | |
} | |
public void testFindDetectObjectEnumerableMatchFound() { | |
String ret = (String) stringArray.detect("test", new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].equals("1"); | |
} | |
}); | |
assertEquals("1", ret); | |
} | |
public void testFindDetectObjectEnumerableNotMatchFound() { | |
String ret = (String) stringArray.detect("test", new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].equals("4"); | |
} | |
}); | |
assertEquals("test", ret); | |
} | |
public void testFindDetectEnumerableMatchNotFound() { | |
String ret = (String) stringArray.detect(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].equals("4"); | |
} | |
}); | |
assertEquals(null, ret); | |
} | |
public void testAllOnEmptyArray() { | |
boolean ret = emptyArray.all(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].getClass().equals(String.class); | |
} | |
}); | |
assertFalse(ret); | |
} | |
public void testAllOnStringArray() { | |
boolean ret = stringArray.all(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].getClass().equals(String.class); | |
} | |
}); | |
assertTrue(ret); | |
} | |
public void testAllOnStringArrayFalse() { | |
boolean ret = mixedArray.all(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].getClass().equals(String.class); | |
} | |
}); | |
assertFalse(ret); | |
} | |
public void testAnyFalse() { | |
boolean ret = mixedArray.any(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].getClass().isInstance(ArrayList.class); | |
} | |
}); | |
assertFalse(ret); | |
} | |
public void testAnyOnEmptyShouldReturnFalse() { | |
boolean ret = mixedArray.any(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].getClass().isInstance(ArrayList.class); | |
} | |
}); | |
assertFalse(ret); | |
} | |
public void testInclude() { | |
assertTrue(mixedArray.include("1")); | |
assertTrue(mixedArray.include(new ArrayList())); | |
assertFalse(mixedArray.include(new HashMap())); | |
} | |
public void testMember() { | |
assertTrue(mixedArray.member("1")); | |
assertTrue(mixedArray.member(new ArrayList())); | |
assertFalse(mixedArray.member(new HashMap())); | |
} | |
public void testSort() { | |
EnumerableArray arr = (EnumerableArray) stringArray.sort(); | |
assertEquals("1", arr.first()); | |
} | |
public void testFirst(){ | |
assertEquals("3", stringArray.first()); | |
} | |
public void testReject(){ | |
EnumerableArray arr = (EnumerableArray) stringArray.reject(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].equals("1"); | |
} | |
}); | |
assertEquals(2, arr.size()); | |
} | |
public void testFind_All(){ | |
EnumerableArray arr = (EnumerableArray) mixedArray.select(new BooleanBlock(){ | |
public boolean yield(Object[] elements) { | |
return elements[0].getClass().equals(String.class); | |
} | |
}); | |
assertEquals(1, arr.size()); | |
} | |
public void testMapAndCollect(){ | |
EnumerableArray arr = (EnumerableArray) stringArray.map(new ObjectBlock(){ | |
public Object yield(Object[] elements) { | |
return ((String) elements[0]).concat("-2"); | |
} | |
}); | |
assertEquals("3-2", arr.first()); | |
} | |
class MyObject{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junivi.lang; | |
public interface IEnumerable { | |
public Object find(Object def, Block matcher); | |
public Object detect(Object def, Block matcher); | |
public Object find(Block matcher); | |
public Object detect(Block matcher); | |
public IEnumerable collect(Block matcher); | |
public IEnumerable map(Block matcher); | |
public IEnumerable to_a(); | |
public IEnumerable entries(); | |
public IEnumerable find_all(Block matcher); | |
public IEnumerable select(Block matcher); | |
public IEnumerable reject(Block matcher); | |
public IEnumerable sort(); | |
public boolean all(Block matcher); | |
public boolean any(Block matcher); | |
public boolean include(Object o); | |
public boolean member(Object o); | |
} | |
// TODO: missing the following methods so far | |
//each_cons each_slice each_with_index enum_cons | |
//enum_slice enum_with_index grep inject inject | |
//map max min partition sort sort_by | |
//to_set zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.junivi.lang; | |
public interface ObjectBlock extends Block{ | |
public Object yield(Object[] elements); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment