Permalink
Browse files

Minor refactoring for better clarity

  • Loading branch information...
djspiewak committed Apr 3, 2008
1 parent f6cfda7 commit f5aa3471eb37b383a124dd934ae117e1c4432207
@@ -20,13 +20,13 @@ public DictionaryType() {
@Override
protected final void writeValue(OutputStream os) throws IOException {
final SortedSet<Key<?>> keys = new TreeSet<Key<?>>();
populate(keys);
final SortedSet<EntryType<?>> entries = new TreeSet<EntryType<?>>();
populate(entries);
for (Key<?> key : keys) {
key.write(os);
for (EntryType<?> entry : entries) {
entry.write(os);
}
}
protected abstract void populate(SortedSet<Key<?>> keys);
protected abstract void populate(SortedSet<EntryType<?>> entries);
}
@@ -13,22 +13,22 @@
* @author Daniel Spiewak
*/
@Prefix('d')
public class DictionaryValue extends CompositeValue<DictionaryValue, EntryPair> {
private EntryPair previous;
public class DictionaryValue extends CompositeValue<DictionaryValue, EntryValue> {
private EntryValue previous;
public DictionaryValue(Parser p, InputStream is) {
super(p, is);
previous = null;
}
public EntryPair next() {
public EntryValue next() {
try {
if (previous != null) {
previous.resolve();
}
return previous = new EntryPair(this);
return previous = new EntryValue(this);
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -12,11 +12,11 @@
/**
* @author Daniel Spiewak
*/
public final class Key<T extends StringType & Comparable<T>> implements Type, Comparable<Key<T>> {
public final class EntryType<T extends StringType & Comparable<T>> implements Type, Comparable<EntryType<T>> {
private final T key;
private final Type value;
public Key(T key, Type value) {
public EntryType(T key, Type value) {
this.key = key;
this.value = value;
}
@@ -26,7 +26,7 @@ public void write(OutputStream os) throws IOException {
value.write(os);
}
public int compareTo(Key<T> o) {
public int compareTo(EntryType<T> o) {
return o.key.compareTo(key);
}
}
@@ -11,19 +11,19 @@
/**
* @author Daniel Spiewak
*/
public class EntryPair implements Value<EntryPair> {
public class EntryValue implements Value<EntryValue> {
private final DictionaryValue parent;
private StringValue key;
private Value<?> value;
private boolean resolved = false;
EntryPair(DictionaryValue parent) {
EntryValue(DictionaryValue parent) {
this.parent = parent;
}
public EntryPair resolve() throws IOException {
public EntryValue resolve() throws IOException {
if (resolved) {
throw new IOException("Value already resolved");
}
View
@@ -8,7 +8,7 @@
import com.googlecode.jbencode.Parser;
import com.googlecode.jbencode.Value;
import com.googlecode.jbencode.composite.DictionaryValue;
import com.googlecode.jbencode.composite.EntryPair;
import com.googlecode.jbencode.composite.EntryValue;
import com.googlecode.jbencode.composite.ListValue;
import com.googlecode.jbencode.primitive.IntegerValue;
import com.googlecode.jbencode.primitive.StringValue;
@@ -36,7 +36,7 @@ public void testParse() throws IOException {
DictionaryValue dict = (DictionaryValue) value;
System.out.println('{');
for (EntryPair pair : dict) {
for (EntryValue pair : dict) {
System.out.print(" \"" + new String(pair.getKey().resolve()) + "\" -> ");
if (pair.getValue() instanceof IntegerValue) {

0 comments on commit f5aa347

Please sign in to comment.