Skip to content

Commit

Permalink
Provide a remove method in NbtCompound. Discourage getValue().
Browse files Browse the repository at this point in the history
Added a missing remove method in NbtCompound. In addition, the
getValue() method in NbtCompount has been depreciated. It is far better
to use the put and get methods in NbtCompound instead.
  • Loading branch information
aadnk committed Feb 17, 2013
1 parent 8c0a671 commit e919056
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public interface NbtBase<TType> {
* Is either a primitive {@link java.lang.Number wrapper}, {@link java.lang.String String},
* {@link java.util.List List} or a {@link java.util.Map Map}.
* <p>
* Users are encouraged to cast an NBT compound to {@link NbtCompound} and use its put and get-methods
* instead of accessing its content from getValue().
* <p>
* All operations that modify collections directly, such as {@link java.util.List#add(Object) List.add(Object)} or
* {@link java.util.Map#clear() Map.clear()}, are considered optional. This also include members in {@link java.util.Iterator Iterator} and
* {@link java.util.ListIterator ListIterator}. Operations that are not implemented throw a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* @author Kristian
*/
public interface NbtCompound extends NbtBase<Map<String, NbtBase<?>>>, Iterable<NbtBase<?>> {
@Override
@Deprecated()
public Map<String, NbtBase<?>> getValue();

/**
* Determine if an entry with the given key exists or not.
* @param key - the key to lookup.
Expand Down Expand Up @@ -304,6 +308,13 @@ public interface NbtCompound extends NbtBase<Map<String, NbtBase<?>>>, Iterable<
*/
public abstract <T> NbtCompound put(String key, Collection<? extends NbtBase<T>> list);

/**
* Remove the NBT element that is associated with the given key.
* @param key - the key of the element to remove.
* @return The removed element, or NULL if no such element was found.
*/
public abstract <T> NbtBase<?> remove(String key);

/**
* Retrieve an iterator view of the NBT tags stored in this compound.
* @return The tags stored in this compound.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public NbtBase<Map<String, NbtBase<?>>> deepClone() {
*/
@Override
public <T> NbtCompound put(NbtBase<T> entry) {
if (entry == null)
throw new IllegalArgumentException("Entry cannot be NULL.");

getValue().put(entry.getName(), entry);
return this;
}
Expand Down Expand Up @@ -565,6 +568,9 @@ public <T> NbtCompound put(NbtList<T> list) {

@Override
public NbtCompound put(String key, NbtBase<?> entry) {
if (entry == null)
throw new IllegalArgumentException("Entry cannot be NULL.");

// Don't modify the original NBT
NbtBase<?> clone = entry.deepClone();

Expand All @@ -583,6 +589,11 @@ public <T> NbtCompound put(String key, Collection<? extends NbtBase<T>> list) {
return put(WrappedList.fromList(key, list));
}

@Override
public <T> NbtBase<?> remove(String key) {
return getValue().remove(key);
}

@Override
public void write(DataOutput destination) {
NbtBinarySerializer.DEFAULT.serialize(container, destination);
Expand Down

0 comments on commit e919056

Please sign in to comment.