Skip to content

Commit

Permalink
Make ArrayTag and NumberTag implementations implement their own Compa…
Browse files Browse the repository at this point in the history
…rable

Make createEmptyValue static
Adjust JavaDoc
Fix ObjectTag and EndTag null value
Throw IllegalArgumentException when negative depth is used
Remove no-args constructor in Tag
  • Loading branch information
Querz committed Jan 22, 2019
1 parent ea1cf0d commit 26ab484
Show file tree
Hide file tree
Showing 32 changed files with 117 additions and 136 deletions.
12 changes: 2 additions & 10 deletions src/main/java/net/querz/nbt/ArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* For implementations see {@link ByteArrayTag}, {@link IntArrayTag}, {@link LongArrayTag}.
* @param <T> The array type.
* */
public abstract class ArrayTag<T> extends Tag<T> implements Comparable<ArrayTag<T>> {
public abstract class ArrayTag<T> extends Tag<T> {

public ArrayTag(T value) {
super(value);
if (!value.getClass().isArray()) {
throw new UnsupportedOperationException("type of array tag must be an array");
}
setValue(value);
}

public int length() {
Expand All @@ -35,14 +35,6 @@ public String valueToString(int depth) {
return arrayToString("", "");
}

@Override
public int compareTo(ArrayTag<T> other) {
if (this.getClass() != other.getClass()) {
throw new IllegalArgumentException("array types are incompatible");
}
return Integer.compare(Array.getLength(getValue()), Array.getLength(other.getValue()));
}

protected String arrayToString(String prefix, String suffix) {
StringBuilder sb = new StringBuilder("[").append(prefix).append("".equals(prefix) ? "" : ";");
for (int i = 0; i < length(); i++) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/querz/nbt/ByteArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

public class ByteArrayTag extends ArrayTag<byte[]> {
public class ByteArrayTag extends ArrayTag<byte[]> implements Comparable<ByteArrayTag> {

public static final byte[] ZERO_VALUE = new byte[0];

Expand Down Expand Up @@ -45,6 +46,11 @@ public int hashCode() {
return Arrays.hashCode(getValue());
}

@Override
public int compareTo(ByteArrayTag other) {
return Integer.compare(length(), other.length());
}

@Override
public ByteArrayTag clone() {
return new ByteArrayTag(Arrays.copyOf(getValue(), length()));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class ByteTag extends NumberTag<Byte> {
public class ByteTag extends NumberTag<Byte> implements Comparable<ByteTag> {

public static final byte ZERO_VALUE = 0;

Expand Down Expand Up @@ -48,6 +48,11 @@ public boolean equals(Object other) {
return super.equals(other) && asByte() == ((ByteTag) other).asByte();
}

@Override
public int compareTo(ByteTag other) {
return getValue().compareTo(other.getValue());
}

@Override
public ByteTag clone() {
return new ByteTag(getValue());
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/net/querz/nbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;

public class CompoundTag extends Tag<Map<String, Tag<?>>> implements Iterable<Map.Entry<String, Tag<?>>>, Comparable<CompoundTag> {

public CompoundTag() {
setValue(createEmptyValue());
super(createEmptyValue());
}

private Map<String, Tag<?>> createEmptyValue() {
private static Map<String, Tag<?>> createEmptyValue() {
return new HashMap<>(8);
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/DoubleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class DoubleTag extends NumberTag<Double> {
public class DoubleTag extends NumberTag<Double> implements Comparable<DoubleTag> {

public static final double ZERO_VALUE = 0.0D;

Expand Down Expand Up @@ -40,6 +40,11 @@ public boolean equals(Object other) {
return super.equals(other) && getValue().equals(((DoubleTag) other).getValue());
}

@Override
public int compareTo(DoubleTag other) {
return getValue().compareTo(other.getValue());
}

@Override
public DoubleTag clone() {
return new DoubleTag(getValue());
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/querz/nbt/EndTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ public final class EndTag extends Tag<Void> {

static final EndTag INSTANCE = new EndTag();

private EndTag() {}
private EndTag() {
super(null);
}

@Override
protected Void checkValue(Void value) {
return value;
}

@Override
public void serializeValue(DataOutputStream dos, int depth) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/FloatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class FloatTag extends NumberTag<Float> {
public class FloatTag extends NumberTag<Float> implements Comparable<FloatTag> {

public static final float ZERO_VALUE = 0.0F;

Expand Down Expand Up @@ -40,6 +40,11 @@ public boolean equals(Object other) {
return super.equals(other) && getValue().equals(((FloatTag) other).getValue());
}

@Override
public int compareTo(FloatTag other) {
return getValue().compareTo(other.getValue());
}

@Override
public FloatTag clone() {
return new FloatTag(getValue());
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/IntArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.IOException;
import java.util.Arrays;

public class IntArrayTag extends ArrayTag<int[]> {
public class IntArrayTag extends ArrayTag<int[]> implements Comparable<IntArrayTag> {

public static final int[] ZERO_VALUE = new int[0];

Expand Down Expand Up @@ -49,6 +49,11 @@ public int hashCode() {
return Arrays.hashCode(getValue());
}

@Override
public int compareTo(IntArrayTag other) {
return Integer.compare(length(), other.length());
}

@Override
public IntArrayTag clone() {
return new IntArrayTag(Arrays.copyOf(getValue(), length()));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/IntTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class IntTag extends NumberTag<Integer> {
public class IntTag extends NumberTag<Integer> implements Comparable<IntTag> {

public static final int ZERO_VALUE = 0;

Expand Down Expand Up @@ -40,6 +40,11 @@ public boolean equals(Object other) {
return super.equals(other) && asInt() == ((IntTag) other).asInt();
}

@Override
public int compareTo(IntTag other) {
return getValue().compareTo(other.getValue());
}

@Override
public IntTag clone() {
return new IntTag(getValue());
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/querz/nbt/ListTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ListTag<T extends Tag<?>> extends Tag<List<T>> implements Iterable<
private Class<?> typeClass = null;

private ListTag() {
setValue(createEmptyValue());
super(createEmptyValue(3));
}

/**
Expand All @@ -40,12 +40,13 @@ protected static ListTag<?> createUnchecked() {
}

/**
* <p>Creates an empty typed value for this ListTag</p>
* <p>Creates an empty mutable list to be used as empty value of ListTags.</p>
*
* @return A typed instance of {@link java.util.List} with an initial capacity of 3
* @param <T> Type of the list elements
* @return An instance of {@link java.util.List} with an initial capacity of 3
* */
private List<T> createEmptyValue() {
return new ArrayList<>(3);
private static <T> List<T> createEmptyValue(int initialCapacity) {
return new ArrayList<>(initialCapacity);
}

/**
Expand All @@ -54,7 +55,7 @@ private List<T> createEmptyValue() {
* @throws NullPointerException When {@code typeClass} is {@code null}
*/
public ListTag(Class<? super T> typeClass) throws IllegalArgumentException, NullPointerException {
super(new ArrayList<>(3));
super(createEmptyValue(3));
if (typeClass == EndTag.class) {
throw new IllegalArgumentException("cannot create ListTag with EndTag elements");
}
Expand Down Expand Up @@ -263,7 +264,7 @@ public void deserializeValue(DataInputStream dis, int depth) throws IOException
}
int size = dis.readInt();
size = size < 0 ? 0 : size;
setValue(new ArrayList<>(size));
setValue(createEmptyValue(size));
if (size != 0) {
for (int i = 0; i < size; i++) {
Tag<?> tag = TagFactory.fromID(typeID);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/LongArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.IOException;
import java.util.Arrays;

public class LongArrayTag extends ArrayTag<long[]> {
public class LongArrayTag extends ArrayTag<long[]> implements Comparable<LongArrayTag> {

public static final long[] ZERO_VALUE = new long[0];

Expand Down Expand Up @@ -49,6 +49,11 @@ public int hashCode() {
return Arrays.hashCode(getValue());
}

@Override
public int compareTo(LongArrayTag other) {
return Integer.compare(length(), other.length());
}

@Override
public LongArrayTag clone() {
return new LongArrayTag(Arrays.copyOf(getValue(), length()));
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/LongTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class LongTag extends NumberTag<Long> {
public class LongTag extends NumberTag<Long> implements Comparable<LongTag> {

public static final long ZERO_VALUE = 0L;

Expand Down Expand Up @@ -40,6 +40,11 @@ public boolean equals(Object other) {
return super.equals(other) && asLong() == ((LongTag) other).asLong();
}

@Override
public int compareTo(LongTag other) {
return getValue().compareTo(other.getValue());
}

@Override
public LongTag clone() {
return new LongTag(getValue());
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/net/querz/nbt/NumberTag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.querz.nbt;

public abstract class NumberTag<T extends Number & Comparable<T>> extends Tag<T> implements Comparable<NumberTag<T>> {
public abstract class NumberTag<T extends Number & Comparable<T>> extends Tag<T> {

public NumberTag(T value) {
super(value);
Expand Down Expand Up @@ -34,12 +34,4 @@ public double asDouble() {
public String valueToString(int depth) {
return getValue().toString();
}

@Override
public int compareTo(NumberTag<T> other) {
if (this.getClass() != other.getClass()) {
throw new IllegalArgumentException("number types are incompatible");
}
return getValue().compareTo(other.getValue());
}
}
7 changes: 6 additions & 1 deletion src/main/java/net/querz/nbt/ShortTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.DataOutputStream;
import java.io.IOException;

public class ShortTag extends NumberTag<Short> {
public class ShortTag extends NumberTag<Short> implements Comparable<ShortTag> {

public static final short ZERO_VALUE = 0;

Expand Down Expand Up @@ -40,6 +40,11 @@ public boolean equals(Object other) {
return super.equals(other) && asShort() == ((ShortTag) other).asShort();
}

@Override
public int compareTo(ShortTag other) {
return getValue().compareTo(other.getValue());
}

@Override
public ShortTag clone() {
return new ShortTag(getValue());
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/net/querz/nbt/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public abstract class Tag<T> implements Cloneable {

private T value;

/**
* Initializes this Tag with a {@code null} value.
*/
protected Tag() {}

/**
* Initializes this Tag with some value. If the value is {@code null}, it will
* throw a {@code NullPointerException}
Expand All @@ -64,9 +59,20 @@ protected T getValue() {
/**
* Sets the value for this Tag directly.
* @param value The value to be set.
* @throws NullPointerException If the value is null
* */
protected void setValue(T value) {
this.value = Objects.requireNonNull(value);
this.value = checkValue(value);
}

/**
* Checks if the value {@code value} is {@code null}.
* @param value The value to check
* @throws NullPointerException If {@code value} was {@code null}
* @return The parameter {@code value}
* */
protected T checkValue(T value) {
return Objects.requireNonNull(value);
}

/**
Expand Down Expand Up @@ -227,7 +233,9 @@ public int hashCode() {
* @exception MaxDepthReachedException If {@code depth <= 0}.
* */
protected int decrementDepth(int depth) {
if (depth <= 0) {
if (depth < 0) {
throw new IllegalArgumentException("negative depth is not allowed");
} else if (depth == 0) {
throw new MaxDepthReachedException("reached maximum depth of NBT structure");
}
return --depth;
Expand Down
Loading

0 comments on commit 26ab484

Please sign in to comment.