Use Guava's ToStringHelper throughout.

This commit is contained in:
Major-
2014-09-15 04:32:31 +01:00
parent 69c75f5e31
commit 5682e7e919
6 changed files with 96 additions and 89 deletions
+3 -1
View File
@@ -2,6 +2,8 @@ package org.apollo.game.model;
import org.apollo.game.model.def.ItemDefinition; import org.apollo.game.model.def.ItemDefinition;
import com.google.common.base.MoreObjects;
/** /**
* Represents a single item. * Represents a single item.
* *
@@ -78,7 +80,7 @@ public final class Item {
@Override @Override
public String toString() { public String toString() {
return Item.class.getName() + " [id=" + id + ", amount=" + amount + "]"; return MoreObjects.toStringHelper(this).add("id", id).add("amount", amount).toString();
} }
} }
@@ -3,6 +3,8 @@ package org.apollo.game.model.entity;
import org.apollo.game.model.Position; import org.apollo.game.model.Position;
import org.apollo.game.model.def.ObjectDefinition; import org.apollo.game.model.def.ObjectDefinition;
import com.google.common.base.MoreObjects;
/** /**
* Represents an object in the game world. * Represents an object in the game world.
* *
@@ -12,12 +14,12 @@ import org.apollo.game.model.def.ObjectDefinition;
public final class GameObject extends Entity { public final class GameObject extends Entity {
/** /**
* The config value that stores the object's id, type, and orientation. * The packed value that stores this object's id, type, and orientation.
*/ */
private final int config; private final int packed;
/** /**
* Creates a game object. * Creates the game object.
* *
* @param id The object's id. * @param id The object's id.
* @param position The position. * @param position The position.
@@ -26,7 +28,7 @@ public final class GameObject extends Entity {
*/ */
public GameObject(int id, Position position, int type, int orientation) { public GameObject(int id, Position position, int type, int orientation) {
super(position); super(position);
this.config = (id * 256) + (type * 4) + orientation; this.packed = id << 8 | type << 2 | orientation;
} }
/** /**
@@ -49,7 +51,7 @@ public final class GameObject extends Entity {
* @return The id. * @return The id.
*/ */
public int getId() { public int getId() {
return config / 256; return packed >> 8;
} }
/** /**
@@ -58,7 +60,7 @@ public final class GameObject extends Entity {
* @return The orientation. * @return The orientation.
*/ */
public int getRotation() { public int getRotation() {
return config & 0x3; return packed & 0x3;
} }
/** /**
@@ -67,13 +69,13 @@ public final class GameObject extends Entity {
* @return The type. * @return The type.
*/ */
public int getType() { public int getType() {
return (config >> 2) & 0x3F; return (packed >> 2) & 0x3F;
} }
@Override @Override
public String toString() { public String toString() {
return GameObject.class.getName() + " [id=" + getId() + ", type=" + getType() + ", rotation=" + getRotation() return MoreObjects.toStringHelper(this).add("id", getId()).add("type", getType()).add("rotation", getRotation())
+ "]"; .toString();
} }
} }
+6 -7
View File
@@ -4,6 +4,9 @@ import org.apollo.game.model.Position;
import org.apollo.game.model.def.NpcDefinition; import org.apollo.game.model.def.NpcDefinition;
import org.apollo.game.sync.block.SynchronizationBlock; import org.apollo.game.sync.block.SynchronizationBlock;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
/** /**
* An {@link Npc} is a {@link Mob} that is not being controlled by a player. * An {@link Npc} is a {@link Mob} that is not being controlled by a player.
* *
@@ -81,15 +84,13 @@ public final class Npc extends Mob {
* @param boundary The boundary. * @param boundary The boundary.
*/ */
public void setBoundary(Position[] boundary) { public void setBoundary(Position[] boundary) {
if (boundary.length != 4) { Preconditions.checkArgument(boundary.length == 4, "Boundary count must be 4.");
throw new IllegalArgumentException("Boundary count must be 4.");
}
this.boundary = boundary; this.boundary = boundary;
} }
@Override @Override
public String toString() { public String toString() {
return "[" + Npc.class.getName() + ": id=" + definition.getId() + ", name=" + definition.getName() + "]"; return MoreObjects.toStringHelper(this).add("id", definition.getId()).add("name", definition.getName()).toString();
} }
/** /**
@@ -98,9 +99,7 @@ public final class Npc extends Mob {
* @param id The id. * @param id The id.
*/ */
public void transform(int id) { public void transform(int id) {
if (id < 0 || id >= NpcDefinition.count()) { Preconditions.checkArgument(id >= 0 && id < NpcDefinition.count(), "Id to transform to is out of bounds.");
throw new IllegalArgumentException("Id to transform to is out of bounds.");
}
definition = NpcDefinition.lookup(this.id = id); definition = NpcDefinition.lookup(this.id = id);
blockSet.add(SynchronizationBlock.createTransformBlock(id)); blockSet.add(SynchronizationBlock.createTransformBlock(id));
} }
+4 -2
View File
@@ -40,6 +40,8 @@ import org.apollo.net.session.GameSession;
import org.apollo.security.PlayerCredentials; import org.apollo.security.PlayerCredentials;
import org.apollo.util.Point; import org.apollo.util.Point;
import com.google.common.base.MoreObjects;
/** /**
* A {@link Player} is a {@link Mob} that a user is controlling. * A {@link Player} is a {@link Mob} that a user is controlling.
* *
@@ -912,8 +914,8 @@ public final class Player extends Mob {
@Override @Override
public String toString() { public String toString() {
return Player.class.getName() + " [username=" + getUsername() + ", privilege=" + privilegeLevel + ", clientVersion=" return MoreObjects.toStringHelper(this).add("username", getUsername()).add("privilege", privilegeLevel)
+ clientVersion + "]"; .add("client version", clientVersion).toString();
} }
/** /**
@@ -7,6 +7,8 @@ import java.util.Queue;
import org.apollo.game.model.Direction; import org.apollo.game.model.Direction;
import org.apollo.game.model.Position; import org.apollo.game.model.Position;
import com.google.common.base.MoreObjects;
/** /**
* A queue of {@link Direction}s which a {@link Mob} will follow. * A queue of {@link Direction}s which a {@link Mob} will follow.
* *
@@ -44,7 +46,7 @@ public final class WalkingQueue {
@Override @Override
public String toString() { public String toString() {
return Point.class.getName() + " [direction=" + direction + ", position=" + position + "]"; return MoreObjects.toStringHelper(this).add("direction", direction).add("position", position).toString();
} }
} }
@@ -3,82 +3,82 @@ package org.apollo.game.model.entity.attr;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.base.Preconditions;
/** /**
* A {@link Map} wrapper used to store {@link Attribute Attributes} and their {@link AttributeDefinition definitions}. * A {@link Map} wrapper used to store {@link Attribute}s and their {@link AttributeDefinition definitions}.
* *
* @author Major * @author Major
*/ */
public final class AttributeMap { public final class AttributeMap {
/** /**
* The map of attribute names to definitions. * The map of attribute names to definitions.
*/ */
private static Map<String, AttributeDefinition<?>> definitions = new HashMap<>(50); private static Map<String, AttributeDefinition<?>> definitions = new HashMap<>(50);
/** /**
* Registers an {@link AttributeDefinition}. * Registers an {@link AttributeDefinition}.
* *
* @param name The name of the attribute. * @param name The name of the attribute.
* @param definition The definition. * @param definition The definition.
*/ */
public static void addDefinition(String name, AttributeDefinition<?> definition) { public static void addDefinition(String name, AttributeDefinition<?> definition) {
definitions.put(name, definition); definitions.put(name, definition);
} }
/** /**
* Gets the {@link AttributeDefinition} with the specified name, or {@code null} if it is not defined. * Gets the {@link AttributeDefinition} with the specified name, or {@code null} if it is not defined.
* *
* @param name The name of the attribute. * @param name The name of the attribute.
* @return The attribute definition. * @return The attribute definition.
*/ */
public static AttributeDefinition<?> getDefinition(String name) { public static AttributeDefinition<?> getDefinition(String name) {
return definitions.get(name); return definitions.get(name);
} }
/** /**
* Gets the {@link AttributeDefinition}s, as a {@link Map}. * Gets the {@link AttributeDefinition}s, as a {@link Map}.
* *
* @return The map of attribute names to definitions. * @return The map of attribute names to definitions.
*/ */
public static Map<String, AttributeDefinition<?>> getDefinitions() { public static Map<String, AttributeDefinition<?>> getDefinitions() {
return new HashMap<>(definitions); return new HashMap<>(definitions);
} }
/** /**
* The map of attribute names to attributes. * The map of attribute names to attributes.
*/ */
private Map<String, Attribute<?>> attributes = new HashMap<>(); private Map<String, Attribute<?>> attributes = new HashMap<>();
/** /**
* Gets the {@link Attribute} with the specified name. * Gets the {@link Attribute} with the specified name.
* *
* @param name The name of the attribute. * @param name The name of the attribute.
* @return The attribute. * @return The attribute.
*/ */
public Attribute<?> getAttribute(String name) { public Attribute<?> getAttribute(String name) {
return attributes.get(name); return attributes.get(name);
} }
/** /**
* Gets a shallow copy of the map of attributes. * Gets a shallow copy of the {@link Map} of {@link Attribute}s.
* *
* @return The attributes. * @return The attributes.
*/ */
public Map<String, Attribute<?>> getAttributes() { public Map<String, Attribute<?>> getAttributes() {
return new HashMap<>(attributes); return new HashMap<>(attributes);
} }
/** /**
* Sets the value of the {@link Attribute} with the specified name. * Sets the value of the {@link Attribute} with the specified name.
* *
* @param name The name of the attribute. * @param name The name of the attribute.
* @param attribute The attribute. * @param attribute The attribute.
*/ */
public void setAttribute(String name, Attribute<?> attribute) { public void setAttribute(String name, Attribute<?> attribute) {
if (getDefinition(name) == null) { Preconditions.checkNotNull(getDefinition(name), "Attributes must be defined before their value can be set.");
throw new IllegalArgumentException("Attributes must be defined before their value can be set."); attributes.put(name, attribute);
} }
attributes.put(name, attribute);
}
} }