/** * Makes a EnumOrderedSet (OS) of the enum type T given at least one T element followed by an array or vararg of any * number of additional T elements. Duplicate items in elements will have all but one item discarded, using the * later item in elements. The order given here will be kept in the result, like in OrderedSet, and * you can use {@link EnumOrderedSet#getAt(int)} to get a T value at a given index, like you would with a List. * @param initial the first item to insert into the EnumOrderedSet; if initial is null, the method returns null * @param elements an array or vararg of T; allowed to be empty * @param <T> an enum type * @return a newly-allocated OrderedSet containing all of the non-duplicate items in elements, in order */ @SuppressWarnings("unchecked") public static <T extends Enum<?>> EnumOrderedSet<T> makeEOS(T initial, T... elements) { if(initial == null) return null; EnumOrderedSet<T> eos = new EnumOrderedSet<>(initial); eos.add(initial); if(elements != null) eos.addAll(elements); return eos; }
@Override public void write(Json json, EnumOrderedSet object, Class knownType) { if(object == null) { json.writeValue(null); return; } json.writeObjectStart(); if(!object.isEmpty()) { json.writeValue("c", object.first().getClass().getName()); json.writeValue("i", object.first(), null); int sz = object.size(); Object[] r = new Object[sz - 1]; for (int i = 1, p = 0; i < sz; i++) { r[p++] = object.getAt(i); } json.writeValue("r", r, Object[].class, Object.class); } else json.writeValue("c", "default"); json.writeObjectEnd(); }
/** * Makes an empty EnumOrderedSet (EOS); needs item type to be specified in order to work. For an empty * EnumOrderedSet with Radius items, you could use {@code Maker.<Radius>makeEOS()}. Using the new keyword is * probably just as easy in this case; this method is provided for completeness relative to makeEOS() with 1 or more * parameters. * @param <T> the type of Enum keys in the returned EnumOrderedSet; cannot be inferred and must be specified * @return an empty EnumOrderedSet with the given item type */ public static <T extends Enum<T>> EnumOrderedSet<T> makeEOS() { return new EnumOrderedSet<>(); }
@Override @SuppressWarnings("unchecked") public EnumOrderedSet read(Json json, JsonValue jsonData, Class type) { if(jsonData == null || jsonData.isNull()) return null; String c = json.readValue("c", String.class, "", jsonData); if("default".equals(c)) return new EnumOrderedSet(); try { Class<? extends Enum<?>> cl = ClassReflection.forName(c); if(!ClassReflection.isEnum(cl)) return null; Enum<?> i = json.readValue("i", cl, jsonData); Object[] r = json.readValue("r", Object[].class, jsonData); return Maker.makeEOS(i, (Enum<?>[]) r); } catch (ReflectionException e) { return null; } //return new OrderedMap(json.readValue(OrderedSet.class, jsonData.get("k")), // json.readValue(ArrayList.class, jsonData.get("v")), jsonData.getFloat("f")); } });