From 34f52786052c7c629d14061307f1f2620b8c9f28 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Fri, 26 Jul 2013 05:04:59 +0200 Subject: [PATCH] Improve support for the toString() method in wrapped objects. --- .../protocol/wrappers/ChunkPosition.java | 5 +++ .../wrappers/WrappedChunkCoordinate.java | 5 +++ .../protocol/wrappers/WrappedDataWatcher.java | 37 ++++++++++++++++++ .../AbstractConverted.java | 4 +- .../ConvertedCollection.java | 4 +- .../{nbt => collection}/ConvertedList.java | 4 +- .../{nbt => collection}/ConvertedMap.java | 38 ++++++++++++++++++- .../{nbt => collection}/ConvertedSet.java | 4 +- .../wrappers/nbt/WrappedCompound.java | 1 + .../protocol/wrappers/nbt/WrappedList.java | 1 + 10 files changed, 93 insertions(+), 10 deletions(-) rename ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/{nbt => collection}/AbstractConverted.java (94%) rename ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/{nbt => collection}/ConvertedCollection.java (94%) rename ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/{nbt => collection}/ConvertedList.java (95%) rename ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/{nbt => collection}/ConvertedMap.java (73%) rename ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/{nbt => collection}/ConvertedSet.java (87%) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/ChunkPosition.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/ChunkPosition.java index d5326a85..e0af5eaf 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/ChunkPosition.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/ChunkPosition.java @@ -221,4 +221,9 @@ public class ChunkPosition { public int hashCode() { return Objects.hashCode(x, y, z); } + + @Override + public String toString() { + return "ChunkPosition [x=" + x + ", y=" + y + ", z=" + z + "]"; + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java index e673eec4..3680261f 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedChunkCoordinate.java @@ -179,4 +179,9 @@ public class WrappedChunkCoordinate implements Comparable { // Map of watchable objects private Map watchableObjects; + // A map view of all the watchable objects + private Map mapView; + /** * Initialize a new data watcher. * @throws FieldAccessException If we're unable to wrap a DataWatcher. @@ -611,4 +615,37 @@ public class WrappedDataWatcher implements Iterable { } }); } + + /** + * Retrieve a view of this DataWatcher as a map. + *

+ * Any changes to the map will be reflected in this DataWatcher, and vice versa. + * @return A view of the data watcher as a map. + */ + public Map asMap() { + // Construct corresponding map + if (mapView == null) { + mapView = new ConvertedMap(getWatchableObjectMap()) { + @Override + protected Object toInner(WrappedWatchableObject outer) { + if (outer == null) + return null; + return outer.getHandle(); + } + + @Override + protected WrappedWatchableObject toOuter(Object inner) { + if (inner == null) + return null; + return new WrappedWatchableObject(inner); + } + }; + } + return mapView; + } + + @Override + public String toString() { + return asMap().toString(); + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/AbstractConverted.java similarity index 94% rename from ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/AbstractConverted.java index 554d4c6b..7491a5a7 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/AbstractConverted.java @@ -15,7 +15,7 @@ * 02111-1307 USA */ -package com.comphenix.protocol.wrappers.nbt; +package com.comphenix.protocol.wrappers.collection; import javax.annotation.Nullable; @@ -29,7 +29,7 @@ import com.google.common.base.Function; * @param - the first type. * @param - the second type. */ -abstract class AbstractConverted { +public abstract class AbstractConverted { /** * Convert a value from the inner map to the outer visible map. * @param inner - the inner value. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedCollection.java similarity index 94% rename from ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedCollection.java index dea203e6..0f226bc1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedCollection.java @@ -15,7 +15,7 @@ * 02111-1307 USA */ -package com.comphenix.protocol.wrappers.nbt; +package com.comphenix.protocol.wrappers.collection; import java.lang.reflect.Array; import java.util.Collection; @@ -33,7 +33,7 @@ import com.google.common.collect.Lists; * @param - type of the element in the inner invisible collection. * @param - type of the elements publically accessible in the outer collection. */ -abstract class ConvertedCollection extends AbstractConverted implements Collection { +public abstract class ConvertedCollection extends AbstractConverted implements Collection { // Inner collection private Collection inner; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedList.java similarity index 95% rename from ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedList.java index 3562a7c5..4232015e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedList.java @@ -15,7 +15,7 @@ * 02111-1307 USA */ -package com.comphenix.protocol.wrappers.nbt; +package com.comphenix.protocol.wrappers.collection; import java.util.Collection; import java.util.List; @@ -29,7 +29,7 @@ import java.util.ListIterator; * @param - type of the items in the inner invisible list. * @param - type of the items publically accessible in the outer list. */ -abstract class ConvertedList extends ConvertedCollection implements List { +public abstract class ConvertedList extends ConvertedCollection implements List { private List inner; public ConvertedList(List inner) { diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedMap.java similarity index 73% rename from ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedMap.java index b662d0da..9a452306 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedMap.java @@ -15,12 +15,14 @@ * 02111-1307 USA */ -package com.comphenix.protocol.wrappers.nbt; +package com.comphenix.protocol.wrappers.collection; import java.util.Collection; +import java.util.Iterator; import java.util.Map; import java.util.Set; + /** * Represents a map that wraps another map by transforming the entries going in and out. * @@ -29,7 +31,7 @@ import java.util.Set; * @param - type of the value in the entries in the inner invisible map. * @param - type of the value in the entries publically accessible in the outer map. */ -abstract class ConvertedMap extends AbstractConverted implements Map { +public abstract class ConvertedMap extends AbstractConverted implements Map { // Inner map private Map inner; @@ -161,4 +163,36 @@ abstract class ConvertedMap extends AbstractConvertedentrySet view's iterator, enclosed in braces + * ("{}"). Adjacent mappings are separated by the characters + * ", " (comma and space). Each key-value mapping is rendered as + * the key followed by an equals sign ("=") followed by the + * associated value. Keys and values are converted to strings as by + * {@link String#valueOf(Object)}. + * + * @return a string representation of this map + */ + public String toString() { + Iterator> i = entrySet().iterator(); + if (!i.hasNext()) + return "{}"; + + StringBuilder sb = new StringBuilder(); + sb.append('{'); + for (;;) { + Entry e = i.next(); + Key key = e.getKey(); + VOuter value = e.getValue(); + sb.append(key == this ? "(this Map)" : key); + sb.append('='); + sb.append(value == this ? "(this Map)" : value); + if (! i.hasNext()) + return sb.append('}').toString(); + sb.append(", "); + } + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedSet.java similarity index 87% rename from ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedSet.java index 5aa0d203..e3c233b1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/collection/ConvertedSet.java @@ -15,7 +15,7 @@ * 02111-1307 USA */ -package com.comphenix.protocol.wrappers.nbt; +package com.comphenix.protocol.wrappers.collection; import java.util.Collection; import java.util.Set; @@ -28,7 +28,7 @@ import java.util.Set; * @param - type of the element in the inner invisible set. * @param - type of the elements publically accessible in the outer set. */ -abstract class ConvertedSet extends ConvertedCollection implements Set { +public abstract class ConvertedSet extends ConvertedCollection implements Set { public ConvertedSet(Collection inner) { super(inner); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java index ae277973..fa1660aa 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java @@ -23,6 +23,7 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; +import com.comphenix.protocol.wrappers.collection.ConvertedMap; import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; /** diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java index 7e281b04..ad3e0edb 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java @@ -24,6 +24,7 @@ import java.util.List; import javax.annotation.Nullable; +import com.comphenix.protocol.wrappers.collection.ConvertedList; import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; import com.google.common.base.Function; import com.google.common.base.Joiner;