Extracted to outer class
This commit is contained in:
parent
d9a3df8277
commit
b85037666a
2 changed files with 106 additions and 0 deletions
81
src/main/java/nl/jssl/autounit/util/Pair.java
Normal file
81
src/main/java/nl/jssl/autounit/util/Pair.java
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package nl.jssl.autounit.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Pair implements Iterable<Object> {
|
||||
public final Object element1;
|
||||
public final Object element2;
|
||||
|
||||
public Pair(Object element1) {
|
||||
this.element1 = element1;
|
||||
this.element2 = null;
|
||||
}
|
||||
|
||||
public Pair(Object element1, Object element2) {
|
||||
super();
|
||||
this.element1 = element1;
|
||||
this.element2 = element2;
|
||||
}
|
||||
|
||||
public int depth() {
|
||||
int d = 0;
|
||||
if (element2 != null) {
|
||||
if (element2 instanceof Pair) {
|
||||
d += 1 + ((Pair) element2).depth();
|
||||
} else {
|
||||
d += 1;
|
||||
}
|
||||
}
|
||||
if (element1 != null) {
|
||||
if (element1 instanceof Pair) {
|
||||
d += 1 + ((Pair) element1).depth();
|
||||
} else {
|
||||
d += 1;
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Object> iterator() {
|
||||
return asList().iterator();
|
||||
}
|
||||
|
||||
private List<Object> asList() {
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
if (element1 != null) {
|
||||
add(element1, list);
|
||||
} else {
|
||||
add("autounit:[NULL]", list);
|
||||
}
|
||||
if (element2 != null) {
|
||||
add(element2, list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private void add(Object element, List<Object> list) {
|
||||
if (element instanceof Pair) {
|
||||
Pair pair = (Pair) element;
|
||||
add(pair.element1, list);
|
||||
add(pair.element2, list);
|
||||
} else {
|
||||
list.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String string = "";
|
||||
for (Object o : this) {
|
||||
string += o.toString() + "-";
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
return asList().toArray();
|
||||
}
|
||||
}
|
||||
25
src/test/java/nl/jssl/autounit/util/PairTests.java
Normal file
25
src/test/java/nl/jssl/autounit/util/PairTests.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package nl.jssl.autounit.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PairTests {
|
||||
@Test
|
||||
public void testDepth1() {
|
||||
Pair p = new Pair("1");
|
||||
assertEquals(1, p.depth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDepth2() {
|
||||
Pair p = new Pair(new Pair("1"));
|
||||
assertEquals(2, p.depth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDepth3() {
|
||||
Pair p = new Pair(new Pair(new Pair("1")));
|
||||
assertEquals(3, p.depth());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue