Commits

Michael Ludwig committed 9c468e1

Implement nicer toString() method for Entity and Component

  • Participants
  • Parent commits 0e380df

Comments (0)

Files changed (3)

File entreri-core/src/main/java/com/lhkbob/entreri/impl/AbstractComponent.java

 package com.lhkbob.entreri.impl;
 
 import com.lhkbob.entreri.*;
+import com.lhkbob.entreri.property.Property;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 
 /**
  * AbstractComponent is the base class used for all generated proxy implementations of
 
     @Override
     public String toString() {
-        // FIXME improve toString() to include values for properties
         if (index == 0) {
-            return "Component(" + getClass().getSimpleName() + ")";
+            return "dead " + getType().getSimpleName();
         } else {
-            int entityId = owner.getEntitySystem()
-                                .getEntityByIndex(owner.getEntityIndex(index)).getId();
-            return "Component(" + getClass().getSimpleName() + ", entity=" + entityId +
-                   ")";
+            StringBuilder sb = new StringBuilder(getType().getSimpleName());
+            sb.append("(").append(getEntity().getId());
+
+            for (int i = 0; i < owner.getDeclaredPropertyCount(); i++) {
+                sb.append(", ").append(owner.getDeclaredPropertyName(i)).append("=")
+                  .append(inspectProperty(owner.getProperty(i)));
+            }
+
+            sb.append(")");
+            return sb.toString();
+        }
+    }
+
+    private String inspectProperty(Property p) {
+        try {
+            Method get = p.getClass().getMethod("get", int.class);
+            Object v = get.invoke(p, getIndex());
+
+            if (v != null) {
+                // strip out newlines
+                return v.toString().replaceAll("[\n\r]", "");
+            } else {
+                return "null";
+            }
+        } catch (NoSuchMethodException e) {
+            // should never happen
+            return "missing get() method";
+        } catch (InvocationTargetException | IllegalAccessException e) {
+            return "unable to inspect";
         }
     }
 }

File entreri-core/src/main/java/com/lhkbob/entreri/impl/ComponentRepository.java

         return declaredProperties.get(propertyIndex).getProperty();
     }
 
+    /**
+     * @param propertyIndex The index of the property
+     *
+     * @return The logical name of the property
+     */
+    public String getDeclaredPropertyName(int propertyIndex) {
+        return declaredProperties.get(propertyIndex).key;
+    }
+
+    /**
+     * @return The number of declared properties
+     */
+    public int getDeclaredPropertyCount() {
+        return declaredProperties.size();
+    }
+
     /*
      * As expandEntityIndex() but expands all related component data and arrays
      * to hold the number of components.
             extends PropertyStore<P>
 
     {
-        final Object key;
+        final String key;
         final P property;
 
-        public DeclaredPropertyStore(PropertyFactory<P> creator, Object key) {
+        public DeclaredPropertyStore(PropertyFactory<P> creator, String key) {
             super(creator);
             this.key = key;
             property = creator.create();

File entreri-core/src/main/java/com/lhkbob/entreri/impl/EntityImpl.java

 
         Iterator<Component> it = iterator();
         while (it.hasNext()) {
-            sb.append(", ");
-            sb.append(it.next().getClass().getSimpleName());
+            sb.append(",\n\t").append(it.next());
         }
 
         sb.append(")");