@Override public void draw() { for (int i = 0; i < length; i++) { Gizmo g = members.get(i); if (g != null && g.exists && g.visible) { g.draw(); } } }
@Override public void update() { for (int i = 0; i < length; i++) { Gizmo g = members.get(i); if (g != null && g.exists && g.active) { g.update(); } } }
public void clear() { for (int i = 0; i < length; i++) { Gizmo g = members.get(i); if (g != null) { g.parent = null; } } members.clear(); length = 0; }
public Gizmo getFirstAvailable(Class<? extends Gizmo> c) { for (int i = 0; i < length; i++) { Gizmo g = members.get(i); if (g != null && !g.exists && ((c == null) || g.getClass() == c)) { return g; } } return null; }
public Gizmo replace(Gizmo oldOne, Gizmo newOne) { int index = members.indexOf(oldOne); if (index != -1) { members.set(index, newOne); newOne.parent = this; oldOne.parent = null; return newOne; } else { return null; } }
@Override public void kill() { // A killed group keeps all its members, // but they get killed too for (int i = 0; i < length; i++) { Gizmo g = members.get(i); if (g != null && g.exists) { g.kill(); } } super.kill(); }
@Override public void destroy() { for (int i = 0; i < length; i++) { Gizmo g = members.get(i); if (g != null) { g.destroy(); } } members.clear(); members = null; length = 0; }
// Real removal public Gizmo remove(Gizmo g) { if (members.remove(g)) { length--; g.parent = null; return g; } else { return null; } }
// Fast removal - replacing with null public Gizmo erase(Gizmo g) { int index = members.indexOf(g); if (index != -1) { members.set(index, null); g.parent = null; return g; } else { return null; } }
public Gizmo addToBack(Gizmo g) { if (g.parent == this) { sendToBack(g); return g; } if (g.parent != null) { g.parent.remove(g); } if (members.get(0) == null) { members.set(0, g); g.parent = this; return g; } members.add(0, g); g.parent = this; length++; return g; }
public Gizmo add(Gizmo g) { if (g.parent == this) { return g; } if (g.parent != null) { g.parent.remove(g); } // Trying to find an empty space for a new member for (int i = 0; i < length; i++) { if (members.get(i) == null) { members.set(i, g); g.parent = this; return g; } } members.add(g); g.parent = this; length++; return g; }
@Test public void testEnumConvertor() { JSON json = new JSON(); Locale l = new Locale("en", "US"); json.addConvertor( Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), false, l)); json.addConvertor(Enum.class, new JSONEnumConvertor(false)); json.addConvertor(Object.class, new JSONObjectConvertor()); Woggle w0 = new Woggle(); Gizmo g0 = new Gizmo(); w0.name = "woggle0"; w0.nested = g0; w0.number = 100; w0.other = Color.Blue; g0.name = "woggle1"; g0.nested = null; g0.number = -101; g0.tested = true; g0.other = Color.Green; HashMap map = new HashMap(); map.put("date", new Date(1)); map.put("w0", w0); map.put("g0", g0); StringBuffer buf = new StringBuffer(); json.append((Appendable) buf, map); String js = buf.toString(); assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertTrue(js.indexOf("\"tested\":true") >= 0); assertTrue(js.indexOf("\"Green\"") >= 0); assertTrue(js.indexOf("\"Blue\"") < 0); json.addConvertor( Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); json.addConvertor(Enum.class, new JSONEnumConvertor(false)); w0.nested = null; buf = new StringBuffer(); json.append((Appendable) buf, map); js = buf.toString(); assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); Map map2 = (HashMap) json.parse(new JSON.StringSource(js)); assertTrue(map2.get("date") instanceof Date); assertTrue(map2.get("w0") instanceof Woggle); assertEquals(null, ((Woggle) map2.get("w0")).getOther()); assertEquals(Color.Green.toString(), ((Map) map2.get("g0")).get("other")); json.addConvertor( Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); json.addConvertor(Enum.class, new JSONEnumConvertor(true)); buf = new StringBuffer(); json.append((Appendable) buf, map); js = buf.toString(); map2 = (HashMap) json.parse(new JSON.StringSource(js)); assertTrue(map2.get("date") instanceof Date); assertTrue(map2.get("w0") instanceof Woggle); assertEquals(null, ((Woggle) map2.get("w0")).getOther()); Object o = ((Map) map2.get("g0")).get("other"); assertEquals(Color.Green, o); }
/* ------------------------------------------------------------ */ @Test public void testConvertor() { // test case#1 - force timezone to GMT JSON json = new JSON(); json.addConvertor( Date.class, new JSONDateConvertor("MM/dd/yyyy HH:mm:ss zzz", TimeZone.getTimeZone("GMT"), false)); json.addConvertor(Object.class, new JSONObjectConvertor()); Woggle w0 = new Woggle(); Gizmo g0 = new Gizmo(); w0.name = "woggle0"; w0.nested = g0; w0.number = 100; g0.name = "woggle1"; g0.nested = null; g0.number = -101; g0.tested = true; HashMap map = new HashMap(); Date dummyDate = new Date(1); map.put("date", dummyDate); map.put("w0", w0); StringBuffer buf = new StringBuffer(); json.append(buf, map); String js = buf.toString(); assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertTrue(js.indexOf("\"tested\":true") >= 0); // test case#3 TimeZone tzone = TimeZone.getTimeZone("JST"); String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT); String format = "EEE MMMMM dd HH:mm:ss zzz yyyy"; Locale l = new Locale("ja", "JP"); if (l != null) { json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l)); buf = new StringBuffer(); json.append(buf, map); js = buf.toString(); // assertTrue(js.indexOf("\"date\":\"\u6728 1\u6708 01 09:00:00 JST 1970\"")>=0); assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertTrue(js.indexOf("\"tested\":true") >= 0); } // test case#4 json.addConvertor(Date.class, new JSONDateConvertor(true)); w0.nested = null; buf = new StringBuffer(); json.append(buf, map); js = buf.toString(); assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); map = (HashMap) json.parse(new JSON.StringSource(js)); assertTrue(map.get("date") instanceof Date); assertTrue(map.get("w0") instanceof Woggle); }
void without(Gizmo g) { g = new Gizmo(); // OK -- g not final g.spin(); }