From e9a259edd9737d14bb74126a6381d3b9109b9003 Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Sun, 25 Mar 2012 10:01:34 -0700 Subject: [PATCH] Added get, size, values, keyset, iterator, etc methods to ListTag and CompoundTag. --- .../opennbt/stream/NBTOutputStream.java | 11 ++++------ .../ch/spacebase/opennbt/tag/CompoundTag.java | 21 ++++++++++++++++--- .../ch/spacebase/opennbt/tag/ListTag.java | 15 ++++++++++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java b/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java index cbc734a..3109dff 100644 --- a/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java +++ b/src/main/java/ch/spacebase/opennbt/stream/NBTOutputStream.java @@ -4,7 +4,6 @@ import java.io.Closeable; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.util.List; import java.util.zip.GZIPOutputStream; import ch.spacebase.opennbt.NBTConstants; @@ -182,7 +181,7 @@ public final class NBTOutputStream implements Closeable { * @throws IOException if an I/O error occurs. */ private void writeCompoundTagPayload(CompoundTag tag) throws IOException { - for(Tag childTag : tag.getValue().values()) { + for(Tag childTag : tag.values()) { writeTag(childTag); } os.writeByte((byte) 0); // end tag - better way? @@ -193,16 +192,14 @@ public final class NBTOutputStream implements Closeable { * @param tag The tag. * @throws IOException if an I/O error occurs. */ - @SuppressWarnings("unchecked") private void writeListTagPayload(ListTag tag) throws IOException { Class clazz = tag.getType(); - List tags = (List) tag.getValue(); - int size = tags.size(); + int size = tag.size(); os.writeByte(NBTUtils.getTypeCode(clazz)); os.writeInt(size); - for(int i = 0; i < size; i++) { - writeTagPayload(tags.get(i)); + for(Tag t : tag.getValue()) { + this.writeTagPayload(t); } } diff --git a/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java b/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java index fe9ce85..b51d656 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/CompoundTag.java @@ -34,14 +34,13 @@ package ch.spacebase.opennbt.tag; * POSSIBILITY OF SUCH DAMAGE. */ +import java.util.Collection; import java.util.Collections; import java.util.Map; +import java.util.Set; import ch.spacebase.opennbt.NBTUtils; - - - /** * The TAG_Compound tag. */ @@ -67,6 +66,22 @@ public final class CompoundTag extends Tag { return value; } + public Tag get(String tagName) { + return this.value.get(tagName); + } + + public Set keySet() { + return this.value.keySet(); + } + + public Collection values() { + return this.value.values(); + } + + public int size() { + return this.value.size(); + } + @Override public String toString() { String name = getName(); diff --git a/src/main/java/ch/spacebase/opennbt/tag/ListTag.java b/src/main/java/ch/spacebase/opennbt/tag/ListTag.java index d444647..484f716 100644 --- a/src/main/java/ch/spacebase/opennbt/tag/ListTag.java +++ b/src/main/java/ch/spacebase/opennbt/tag/ListTag.java @@ -36,6 +36,7 @@ package ch.spacebase.opennbt.tag; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import ch.spacebase.opennbt.NBTUtils; @@ -46,7 +47,7 @@ import ch.spacebase.opennbt.NBTUtils; /** * The TAG_List tag. */ -public final class ListTag extends Tag { +public final class ListTag extends Tag implements Iterable { /** * The type. @@ -83,6 +84,18 @@ public final class ListTag extends Tag { return value; } + public T get(int index) { + return this.value.get(index); + } + + public Iterator iterator() { + return this.value.iterator(); + } + + public int size() { + return this.value.size(); + } + @Override public String toString() { String name = getName();