Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial capacity to List and Compound tags #60

Merged
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 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) {
urielsalis marked this conversation as resolved.
Show resolved Hide resolved
super(new HashMap<>(initialCapacity));
}

@Override
public byte getID() {
return ID;
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<>(this.size());
// assure type safety for clone
copy.typeClass = typeClass;
for (T t : getValue()) {
Expand Down