implement iterator for BValue, add exception class
This commit is contained in:
parent
76f642f8c8
commit
da9c812187
4 changed files with 58 additions and 1 deletions
|
|
@ -1,6 +1,15 @@
|
||||||
package com.mykola2312.retracker.bencode;
|
package com.mykola2312.retracker.bencode;
|
||||||
|
|
||||||
abstract public class BValue {
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/* Base class for every type of bencode value, as tree nodes,
|
||||||
|
* simple types like integer or string will have next pointing
|
||||||
|
* to next sibling and have no child, where list's child points
|
||||||
|
* to linked values - items of list. In dict situation, child points
|
||||||
|
* to linked list of keys, where each key has child - value
|
||||||
|
*/
|
||||||
|
|
||||||
|
abstract public class BValue implements Iterable<BValue> {
|
||||||
private BType type;
|
private BType type;
|
||||||
private BValue next = null;
|
private BValue next = null;
|
||||||
private BValue child = null;
|
private BValue child = null;
|
||||||
|
|
@ -20,4 +29,8 @@ abstract public class BValue {
|
||||||
public BValue getChild() {
|
public BValue getChild() {
|
||||||
return child;
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterator<BValue> iterator() {
|
||||||
|
return new BValueIterator(child);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.mykola2312.retracker.bencode;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class BValueIterator implements Iterator<BValue> {
|
||||||
|
private BValue node;
|
||||||
|
public BValueIterator(BValue head) {
|
||||||
|
this.node = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return node != null && node.getNext() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BValue next() {
|
||||||
|
node = node.getNext();
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.mykola2312.retracker.bencode.error;
|
||||||
|
|
||||||
|
import com.mykola2312.retracker.bencode.BValue;
|
||||||
|
|
||||||
|
public class BError extends Exception {
|
||||||
|
private static final long serialVersionUID = 6950892783320917930L;
|
||||||
|
|
||||||
|
public BValue node;
|
||||||
|
|
||||||
|
public BError(BValue node, String message) {
|
||||||
|
super(message);
|
||||||
|
this.node = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.mykola2312.retracker.bencode.error;
|
||||||
|
|
||||||
|
import com.mykola2312.retracker.bencode.BValue;
|
||||||
|
|
||||||
|
public class BErrorNoChildren extends BError {
|
||||||
|
private static final long serialVersionUID = -4503679050993811843L;
|
||||||
|
|
||||||
|
public BErrorNoChildren(BValue node) {
|
||||||
|
super(node, "Node has no children");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue