Improve support for the toString() method in wrapped objects.

This commit is contained in:
Kristian S. Stangeland 2013-07-26 05:04:59 +02:00
parent 5584357fee
commit 34f5278605
10 changed files with 93 additions and 10 deletions

View File

@ -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 + "]";
}
}

View File

@ -179,4 +179,9 @@ public class WrappedChunkCoordinate implements Comparable<WrappedChunkCoordinate
public int hashCode() {
return handle.hashCode();
}
@Override
public String toString() {
return String.format("ChunkCoordinate [x: %s, y: %s, z: %s]", getX(), getY(), getZ());
}
}

View File

@ -41,6 +41,7 @@ import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.FieldUtils;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.wrappers.collection.ConvertedMap;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.Iterators;
@ -83,6 +84,9 @@ public class WrappedDataWatcher implements Iterable<WrappedWatchableObject> {
// Map of watchable objects
private Map<Integer, Object> watchableObjects;
// A map view of all the watchable objects
private Map<Integer, WrappedWatchableObject> 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<WrappedWatchableObject> {
}
});
}
/**
* Retrieve a view of this DataWatcher as a map.
* <p>
* 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<Integer, WrappedWatchableObject> asMap() {
// Construct corresponding map
if (mapView == null) {
mapView = new ConvertedMap<Integer, Object, WrappedWatchableObject>(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();
}
}

View File

@ -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 <VInner> - the first type.
* @param <VOuter> - the second type.
*/
abstract class AbstractConverted<VInner, VOuter> {
public abstract class AbstractConverted<VInner, VOuter> {
/**
* Convert a value from the inner map to the outer visible map.
* @param inner - the inner value.

View File

@ -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 <VInner> - type of the element in the inner invisible collection.
* @param <VOuter> - type of the elements publically accessible in the outer collection.
*/
abstract class ConvertedCollection<VInner, VOuter> extends AbstractConverted<VInner, VOuter> implements Collection<VOuter> {
public abstract class ConvertedCollection<VInner, VOuter> extends AbstractConverted<VInner, VOuter> implements Collection<VOuter> {
// Inner collection
private Collection<VInner> inner;

View File

@ -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 <VInner> - type of the items in the inner invisible list.
* @param <VOuter> - type of the items publically accessible in the outer list.
*/
abstract class ConvertedList<VInner, VOuter> extends ConvertedCollection<VInner, VOuter> implements List<VOuter> {
public abstract class ConvertedList<VInner, VOuter> extends ConvertedCollection<VInner, VOuter> implements List<VOuter> {
private List<VInner> inner;
public ConvertedList(List<VInner> inner) {

View File

@ -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 <VInner> - type of the value in the entries in the inner invisible map.
* @param <VOuter> - type of the value in the entries publically accessible in the outer map.
*/
abstract class ConvertedMap<Key, VInner, VOuter> extends AbstractConverted<VInner, VOuter> implements Map<Key, VOuter> {
public abstract class ConvertedMap<Key, VInner, VOuter> extends AbstractConverted<VInner, VOuter> implements Map<Key, VOuter> {
// Inner map
private Map<Key, VInner> inner;
@ -161,4 +163,36 @@ abstract class ConvertedMap<Key, VInner, VOuter> extends AbstractConverted<VInne
}
};
}
/**
* Returns a string representation of this map. The string representation
* consists of a list of key-value mappings in the order returned by the
* map's <tt>entrySet</tt> view's iterator, enclosed in braces
* (<tt>"{}"</tt>). Adjacent mappings are separated by the characters
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as
* the key followed by an equals sign (<tt>"="</tt>) 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<Entry<Key, VOuter>> i = entrySet().iterator();
if (!i.hasNext())
return "{}";
StringBuilder sb = new StringBuilder();
sb.append('{');
for (;;) {
Entry<Key, VOuter> 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(", ");
}
}
}

View File

@ -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 <VInner> - type of the element in the inner invisible set.
* @param <VOuter> - type of the elements publically accessible in the outer set.
*/
abstract class ConvertedSet<VInner, VOuter> extends ConvertedCollection<VInner, VOuter> implements Set<VOuter> {
public abstract class ConvertedSet<VInner, VOuter> extends ConvertedCollection<VInner, VOuter> implements Set<VOuter> {
public ConvertedSet(Collection<VInner> inner) {
super(inner);
}

View File

@ -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;
/**

View File

@ -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;