Added get, size, values, keyset, iterator, etc methods to ListTag and CompoundTag.

This commit is contained in:
Steveice10 2012-03-25 10:01:34 -07:00
parent e426b3d8e1
commit e9a259edd9
3 changed files with 36 additions and 11 deletions

View File

@ -4,7 +4,6 @@ import java.io.Closeable;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import ch.spacebase.opennbt.NBTConstants; import ch.spacebase.opennbt.NBTConstants;
@ -182,7 +181,7 @@ public final class NBTOutputStream implements Closeable {
* @throws IOException if an I/O error occurs. * @throws IOException if an I/O error occurs.
*/ */
private void writeCompoundTagPayload(CompoundTag tag) throws IOException { private void writeCompoundTagPayload(CompoundTag tag) throws IOException {
for(Tag childTag : tag.getValue().values()) { for(Tag childTag : tag.values()) {
writeTag(childTag); writeTag(childTag);
} }
os.writeByte((byte) 0); // end tag - better way? os.writeByte((byte) 0); // end tag - better way?
@ -193,16 +192,14 @@ public final class NBTOutputStream implements Closeable {
* @param tag The tag. * @param tag The tag.
* @throws IOException if an I/O error occurs. * @throws IOException if an I/O error occurs.
*/ */
@SuppressWarnings("unchecked")
private void writeListTagPayload(ListTag<?> tag) throws IOException { private void writeListTagPayload(ListTag<?> tag) throws IOException {
Class<? extends Tag> clazz = tag.getType(); Class<? extends Tag> clazz = tag.getType();
List<Tag> tags = (List<Tag>) tag.getValue(); int size = tag.size();
int size = tags.size();
os.writeByte(NBTUtils.getTypeCode(clazz)); os.writeByte(NBTUtils.getTypeCode(clazz));
os.writeInt(size); os.writeInt(size);
for(int i = 0; i < size; i++) { for(Tag t : tag.getValue()) {
writeTagPayload(tags.get(i)); this.writeTagPayload(t);
} }
} }

View File

@ -34,14 +34,13 @@ package ch.spacebase.opennbt.tag;
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Set;
import ch.spacebase.opennbt.NBTUtils; import ch.spacebase.opennbt.NBTUtils;
/** /**
* The <code>TAG_Compound</code> tag. * The <code>TAG_Compound</code> tag.
*/ */
@ -67,6 +66,22 @@ public final class CompoundTag extends Tag {
return value; return value;
} }
public Tag get(String tagName) {
return this.value.get(tagName);
}
public Set<String> keySet() {
return this.value.keySet();
}
public Collection<Tag> values() {
return this.value.values();
}
public int size() {
return this.value.size();
}
@Override @Override
public String toString() { public String toString() {
String name = getName(); String name = getName();

View File

@ -36,6 +36,7 @@ package ch.spacebase.opennbt.tag;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import ch.spacebase.opennbt.NBTUtils; import ch.spacebase.opennbt.NBTUtils;
@ -46,7 +47,7 @@ import ch.spacebase.opennbt.NBTUtils;
/** /**
* The <code>TAG_List</code> tag. * The <code>TAG_List</code> tag.
*/ */
public final class ListTag<T extends Tag> extends Tag { public final class ListTag<T extends Tag> extends Tag implements Iterable<T> {
/** /**
* The type. * The type.
@ -83,6 +84,18 @@ public final class ListTag<T extends Tag> extends Tag {
return value; return value;
} }
public T get(int index) {
return this.value.get(index);
}
public Iterator<T> iterator() {
return this.value.iterator();
}
public int size() {
return this.value.size();
}
@Override @Override
public String toString() { public String toString() {
String name = getName(); String name = getName();