Archiviert
13
0

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

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-07-26 05:04:59 +02:00
Ursprung 5584357fee
Commit 34f5278605
10 geänderte Dateien mit 93 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -221,4 +221,9 @@ public class ChunkPosition {
public int hashCode() { public int hashCode() {
return Objects.hashCode(x, y, z); return Objects.hashCode(x, y, z);
} }
@Override
public String toString() {
return "ChunkPosition [x=" + x + ", y=" + y + ", z=" + z + "]";
}
} }

Datei anzeigen

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

Datei anzeigen

@ -41,6 +41,7 @@ import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FieldUtils;
import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.wrappers.collection.ConvertedMap;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
@ -83,6 +84,9 @@ public class WrappedDataWatcher implements Iterable<WrappedWatchableObject> {
// Map of watchable objects // Map of watchable objects
private Map<Integer, Object> watchableObjects; private Map<Integer, Object> watchableObjects;
// A map view of all the watchable objects
private Map<Integer, WrappedWatchableObject> mapView;
/** /**
* Initialize a new data watcher. * Initialize a new data watcher.
* @throws FieldAccessException If we're unable to wrap a DataWatcher. * @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();
}
} }

Datei anzeigen

@ -15,7 +15,7 @@
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.wrappers.nbt; package com.comphenix.protocol.wrappers.collection;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -29,7 +29,7 @@ import com.google.common.base.Function;
* @param <VInner> - the first type. * @param <VInner> - the first type.
* @param <VOuter> - the second 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. * Convert a value from the inner map to the outer visible map.
* @param inner - the inner value. * @param inner - the inner value.

Datei anzeigen

@ -15,7 +15,7 @@
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.wrappers.nbt; package com.comphenix.protocol.wrappers.collection;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Collection; 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 <VInner> - type of the element in the inner invisible collection.
* @param <VOuter> - type of the elements publically accessible in the outer 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 // Inner collection
private Collection<VInner> inner; private Collection<VInner> inner;

Datei anzeigen

@ -15,7 +15,7 @@
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.wrappers.nbt; package com.comphenix.protocol.wrappers.collection;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -29,7 +29,7 @@ import java.util.ListIterator;
* @param <VInner> - type of the items in the inner invisible list. * @param <VInner> - type of the items in the inner invisible list.
* @param <VOuter> - type of the items publically accessible in the outer 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; private List<VInner> inner;
public ConvertedList(List<VInner> inner) { public ConvertedList(List<VInner> inner) {

Datei anzeigen

@ -15,12 +15,14 @@
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.wrappers.nbt; package com.comphenix.protocol.wrappers.collection;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* Represents a map that wraps another map by transforming the entries going in and out. * 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 <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. * @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 // Inner map
private Map<Key, VInner> inner; 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(", ");
}
}
} }

Datei anzeigen

@ -15,7 +15,7 @@
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.wrappers.nbt; package com.comphenix.protocol.wrappers.collection;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
@ -28,7 +28,7 @@ import java.util.Set;
* @param <VInner> - type of the element in the inner invisible set. * @param <VInner> - type of the element in the inner invisible set.
* @param <VOuter> - type of the elements publically accessible in the outer 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) { public ConvertedSet(Collection<VInner> inner) {
super(inner); super(inner);
} }

Datei anzeigen

@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.comphenix.protocol.wrappers.collection.ConvertedMap;
import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer;
/** /**

Datei anzeigen

@ -24,6 +24,7 @@ import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.comphenix.protocol.wrappers.collection.ConvertedList;
import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;