working on bencode lists, test covering

This commit is contained in:
mykola2312 2024-10-13 10:08:10 +03:00
parent 7df116d02b
commit ee8b20097d
2 changed files with 78 additions and 0 deletions

View file

@ -0,0 +1,62 @@
package com.mykola2312.retracker.bencode;
import java.util.Iterator;
public class BList extends BValue {
private BValue last = null;
private int length = 0;
@Override()
public BType getType() {
return BType.LIST;
}
protected BValue getFirst() {
return getChild();
}
protected void setFirst(BValue first) {
setChild(first);
}
protected BValue getLast() {
return last;
}
protected void setLast(BValue last) {
this.last = last;
}
// builder
public BList append(BValue item) {
BValue first = getFirst();
if (first == null) {
setFirst(item);
setLast(item);
} else {
BValue last = getLast();
last.setNext(item);
setLast(item);
}
length++;
return this;
}
public int getLength() {
return length;
}
public BValue get(int index) throws IndexOutOfBoundsException {
Iterator<BValue> it = iterator();
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException();
}
while (--index > 0) {
it.next();
}
return it.next();
}
}

View file

@ -0,0 +1,16 @@
package com.mykola2312.retracker.bencode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
public class BListTest {
@Test
public void testEmptyList() {
BList empty = new BList();
assertEquals(0, empty.getLength());
assertNull(empty.getChild());
}
}