Skip to content

Commit

Permalink
Merge 0fc12da into f05f37f
Browse files Browse the repository at this point in the history
  • Loading branch information
urielsalis authored Apr 16, 2021
2 parents f05f37f + 0fc12da commit 7a4cdc1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
13 changes: 9 additions & 4 deletions src/main/java/net/querz/nbt/tag/CompoundTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.querz.nbt.tag;

import net.querz.io.MaxDepthIO;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -10,14 +8,21 @@
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>, MaxDepthIO {
import net.querz.io.MaxDepthIO;

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

public static final byte ID = 10;

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

public CompoundTag(int initialCapacity) {
super(new HashMap<>(initialCapacity));
}

@Override
public byte getID() {
return ID;
Expand Down Expand Up @@ -277,7 +282,7 @@ public int compareTo(CompoundTag o) {

@Override
public CompoundTag clone() {
CompoundTag copy = new CompoundTag();
CompoundTag copy = new CompoundTag(((int) Math.ceil(getValue().size() * 1.75f)) + 1);
for (Map.Entry<String, Tag<?>> e : getValue().entrySet()) {
copy.put(e.getKey(), e.getValue().clone());
}
Expand Down
61 changes: 43 additions & 18 deletions src/main/java/net/querz/nbt/tag/ListTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.querz.nbt.tag;

import net.querz.io.MaxDepthIO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
Expand All @@ -10,60 +8,85 @@
import java.util.Objects;
import java.util.function.Consumer;

import net.querz.io.MaxDepthIO;

/**
* ListTag represents a typed List in the nbt structure.
* An empty {@link ListTag} will be of type {@link EndTag} (unknown type).
* The type of an empty untyped {@link ListTag} can be set by using any of the {@code add()}
* methods or any of the {@code as...List()} methods.
* */
*/
public class ListTag<T extends Tag<?>> extends Tag<List<T>> implements Iterable<T>, Comparable<ListTag<T>>, MaxDepthIO {

public static final byte ID = 9;

private Class<?> typeClass = null;

private ListTag() {
super(createEmptyValue(3));
private ListTag(int initialCapacity) {
super(createEmptyValue(initialCapacity));
}

@Override
public byte getID() {
return ID;
}

/**
* <p>Creates a non-type-safe ListTag. Its element type will be set after the first
* <p>Creates a non-type-safe ListTag. Its element type will be set after the first
* element was added.</p>
*
* <p>This is an internal helper method for cases where the element type is not known
*
* <p>This is an internal helper method for cases where the element type is not known
* at construction time. Use {@link #ListTag(Class)} when the type is known.</p>
*
*
* @return A new non-type-safe ListTag
*/
public static ListTag<?> createUnchecked(Class<?> typeClass) {
ListTag<?> list = new ListTag<>();
return createUnchecked(typeClass, 3);
}

/**
* <p>Creates a non-type-safe ListTag. Its element type will be set after the first
* element was added.</p>
*
* <p>This is an internal helper method for cases where the element type is not known
* at construction time. Use {@link #ListTag(Class)} when the type is known.</p>
*
* @return A new non-type-safe ListTag
*/
public static ListTag<?> createUnchecked(Class<?> typeClass, int initialCapacity) {
ListTag<?> list = new ListTag<>(initialCapacity);
list.typeClass = typeClass;
return list;
}

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

/**
* @param typeClass The exact class of the elements
* @throws IllegalArgumentException When {@code typeClass} is {@link EndTag}{@code .class}
* @throws NullPointerException When {@code typeClass} is {@code null}
* @throws NullPointerException When {@code typeClass} is {@code null}
*/
public ListTag(Class<? super T> typeClass) throws IllegalArgumentException, NullPointerException {
super(createEmptyValue(3));
this(typeClass, 3);
}

/**
* @param typeClass The exact class of the elements
* @param initialCapacity Initial capacity of list
* @throws IllegalArgumentException When {@code typeClass} is {@link EndTag}{@code .class}
* @throws NullPointerException When {@code typeClass} is {@code null}
*/
public ListTag(Class<? super T> typeClass, int initialCapacity) throws IllegalArgumentException, NullPointerException {
super(createEmptyValue(initialCapacity));
if (typeClass == EndTag.class) {
throw new IllegalArgumentException("cannot create ListTag with EndTag elements");
}
Expand Down Expand Up @@ -114,8 +137,9 @@ public T set(int index, T t) {

/**
* Adds a Tag to this ListTag after the last index.
*
* @param t The element to be added.
* */
*/
public void add(T t) {
add(size(), t);
}
Expand Down Expand Up @@ -271,7 +295,8 @@ public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!super.equals(other) || size() != ((ListTag<?>) other).size() || getTypeClass() != ((ListTag<?>) other).getTypeClass()) {
if (!super.equals(other) || size() != ((ListTag<?>) other).size() || getTypeClass() != ((ListTag<?>) other)
.getTypeClass()) {
return false;
}
for (int i = 0; i < size(); i++) {
Expand All @@ -295,7 +320,7 @@ public int compareTo(ListTag<T> o) {
@SuppressWarnings("unchecked")
@Override
public ListTag<T> clone() {
ListTag<T> copy = new ListTag<>();
ListTag<T> copy = new ListTag<>(((int) Math.ceil(this.size() * 1.75)) + 1);
// assure type safety for clone
copy.typeClass = typeClass;
for (T t : getValue()) {
Expand Down

0 comments on commit 7a4cdc1

Please sign in to comment.