Made StoneSet iterable
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@31 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
36da8c8a17
commit
f9b396875a
2 changed files with 46 additions and 3 deletions
|
@ -1,19 +1,24 @@
|
||||||
package jrummikub.model;
|
package jrummikub.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||||
|
|
||||||
import jrummikub.util.Pair;
|
import jrummikub.util.Pair;
|
||||||
|
|
||||||
/** Class managing {@link Stone}s joined together to form sets */
|
/** Class managing {@link Stone}s joined together to form sets */
|
||||||
public class StoneSet {
|
public class StoneSet implements Iterable<Stone> {
|
||||||
private List<Stone> stones;
|
private List<Stone> stones;
|
||||||
|
|
||||||
public StoneSet(Stone stone) {
|
public StoneSet(Stone stone) {
|
||||||
|
stones = Collections.singletonList(stone);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StoneSet(List<Stone> stones) {
|
public StoneSet(List<Stone> stones) {
|
||||||
|
this.stones = new ArrayList<Stone>(stones);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test for rule conflict within the StoneSet */
|
/** Test for rule conflict within the StoneSet */
|
||||||
|
@ -60,4 +65,28 @@ public class StoneSet {
|
||||||
return stones.get(i);
|
return stones.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Stone> iterator() {
|
||||||
|
final Iterator<Stone> it = stones.iterator();
|
||||||
|
|
||||||
|
return new Iterator<Stone>(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return it.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stone next() {
|
||||||
|
return it.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
// removing stones is impossible
|
||||||
|
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,4 +63,18 @@ public class StoneSetTest {
|
||||||
assertSame(testSet.get(2), joinedSet.get(2));
|
assertSame(testSet.get(2), joinedSet.get(2));
|
||||||
assertSame(secondSet.get(0), joinedSet.get(3));
|
assertSame(secondSet.get(0), joinedSet.get(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//iterator
|
||||||
|
@Test
|
||||||
|
public void testIterator() {
|
||||||
|
StoneSet testSet = createTestSet();
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for(Stone stone : testSet) {
|
||||||
|
assertSame(stone,testSet.get(i));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(i, testSet.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue