/** * Sanitise a string containing a common representation of a number to make * it parsable. Strips thousand-separating commas and trims later members * of a comma-separated list. For example the string "(9.5, 10.6, 33.2)" * will be sanitised to "9.5". * * @param obj Object to sanitise * @return Sanitised number-format string to parse */ private static String sanitiseNumber(Object obj) { String string = obj.toString().trim(); if (string.length() < 1) { return "0"; } Matcher candidate = Coerce.listPattern.matcher(string); if (Coerce.listBracketsMatch(candidate)) { string = candidate.group(2).trim(); } int decimal = string.indexOf('.'); int comma = string.indexOf(',', decimal); if (decimal > -1 && comma > -1) { return Coerce.sanitiseNumber(string.substring(0, comma)); } if (string.indexOf('-', 1) != -1) { return "0"; } return string.replace(",", "").split(" ")[0]; }
/** * Coerce the supplied object to a list. Accepts lists and all types of 1D * arrays. Also (naively) supports lists in Strings in a format like * <code>{1,2,3,I,am,a,list}</code> * * @param obj Object to coerce * @return Some kind of List filled with unimaginable horrors */ public static List<?> toList(@Nullable Object obj) { if (obj == null) { return Collections.<Object>emptyList(); } if (obj instanceof List) { return (List<?>)obj; } Class<?> clazz = obj.getClass(); if (clazz.isArray()) { if (clazz.getComponentType().isPrimitive()) { return Coerce.primitiveArrayToList(obj); } return Arrays.asList((Object[])obj); } return Coerce.parseStringToList(obj.toString()); }
List<T> filteredList = Lists.newArrayList(); for (Object o : Coerce.toList(obj)) { if (ofClass.isAssignableFrom(o.getClass())) { filteredList.add((T)o); } else if (ofClass.equals(String.class)) { filteredList.add((T)Coerce.toString(o)); } else if (ofClass.equals(Integer.TYPE) || ofClass.equals(Integer.class)) { filteredList.add((T)(Integer)Coerce.toInteger(o)); } else if (ofClass.equals(Float.TYPE) || ofClass.equals(Float.class)) { filteredList.add((T)new Float(Coerce.toDouble(o))); } else if (ofClass.equals(Double.TYPE) || ofClass.equals(Double.class)) { filteredList.add((T)(Double)Coerce.toDouble(o)); } else if (ofClass.equals(Boolean.TYPE) || ofClass.equals(Boolean.class)) { filteredList.add((T)(Boolean)Coerce.toBoolean(o));
/** * Coerce the supplied object to a long number, parse it if necessary. * * @param obj Object to coerce * @return Object as a long, <code>0</code> if the object is null or cannot * be parsed */ public static long toLong(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).longValue(); } try { return Long.parseLong(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
if (listBracketsMatch(vecMatch)) { return new Vector2i(Integer.parseInt(vecMatch.group(1)), Integer.parseInt(vecMatch.group(2))); List<?> list = Coerce.toList(obj); if (list.size() == 2) { return new Vector2i(Coerce.toInteger(list.get(0)), Coerce.toInteger(list.get(1)));
/** * Create a new integer property with the specified value. * * @param value value to match */ public IntProperty(int value) { super(Coerce.toInteger(value)); }
/** * Create a new SlotSide property for matching the specified value with the * specified operator. * * @param value the value to match * @param operator the operator to use when comparing with other properties */ public SlotSide(Object value, Operator operator) { super(Coerce.<Direction>toEnum(value, Direction.class, Direction.NONE), operator); }
/** * Create a new integer property with the specified value and logical * operator. * * @param value value to match * @param operator logical operator to use when comparing to other * properties */ public BooleanProperty(Object value, Operator operator) { super(Coerce.toBoolean(value), operator); }
/** * Create a new integer property with the specified value. * * @param value value to match */ public DoubleProperty(double value) { super(Coerce.toDouble(value)); }
/** * Create a new AcceptsItems property with the supplied value and operator. * * @param value Item types to accept * @param operator Logical operator to apply when comparing with other * properties */ public AcceptsItems(Object value, Operator operator) { super(Coerce.toListOf(value, ItemType.class), operator); }
private static List<?> parseStringToList(String string) { Matcher candidate = Coerce.listPattern.matcher(string); if (!Coerce.listBracketsMatch(candidate)) { return Collections.<Object>emptyList(); } List<String> list = Lists.newArrayList(); for (String part : candidate.group(2).split(",")) { if (part != null) { list.add(part); } } return list; }
/** * Coerce the supplied object to a string. * * @param obj Object to coerce * @return Object as a string, empty string if the object is null */ public static String toString(@Nullable Object obj) { if (obj == null) { return ""; } if (obj.getClass().isArray()) { return Coerce.toList(obj).toString(); } return obj.toString(); }
/** * Coerce the supplied object to a short number, parse it if necessary. * * @param obj Object to coerce * @return Object as a short, <code>0</code> if the object is null or cannot * be parsed */ public static short toShort(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).shortValue(); } try { return Short.parseShort(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
if (listBracketsMatch(vecMatch)) { return new Vector2i(Integer.parseInt(vecMatch.group(1)), Integer.parseInt(vecMatch.group(2))); List<?> list = Coerce.toList(obj); if (list.size() == 2) { return new Vector2i(Coerce.toInteger(list.get(0)), Coerce.toInteger(list.get(1)));
/** * Create a new integer property with the specified value and logical * operator. * * @param value value to match * @param operator logical operator to use when comparing to other * properties */ public IntProperty(Object value, Operator operator) { super(Coerce.toInteger(value), operator); }
/** * Coerce the specified object to an enum of the supplied type, returns the * first enum constant in the enum if parsing fails. * * @param obj Object to coerce * @param enumClass Enum class to coerce to * @param <E> enum type * @return Coerced enum value */ public static <E extends Enum<E>> E toEnum(@Nullable Object obj, Class<E> enumClass) { return Coerce.toEnum(obj, enumClass, enumClass.getEnumConstants()[0]); }
/** * Create a new integer property with the specified value. * * @param value value to match */ public BooleanProperty(boolean value) { super(Coerce.toBoolean(value)); }
/** * Create a new integer property with the specified value and logical * operator. * * @param value value to match * @param operator logical operator to use when comparing to other * properties */ public DoubleProperty(Object value, Operator operator) { super(Coerce.toDouble(value), operator); }
/** * Returns true if <em>other</em> is also an {@link AcceptsItems} property * and <b>any</b> item appearing in the other property's collection appears * in this property's collection. In formal terms, the method returns true * if the size of the intersection between the two item type collections is * greater than zero. */ @Override public boolean equals(Object obj) { if (!(obj instanceof InventoryProperty)) { return false; } InventoryProperty<?, ?> other = (InventoryProperty<?, ?>) obj; if (!other.getKey().equals(this.getKey())) { return false; } List<ItemType> otherTypes = Coerce.toListOf(other.getValue(), ItemType.class); for (ItemType t : this.value) { if (otherTypes.contains(t)) { return true; } } return false; }
private static List<?> parseStringToList(String string) { Matcher candidate = Coerce.listPattern.matcher(string); if (!Coerce.listBracketsMatch(candidate)) { return Collections.<Object>emptyList(); } List<String> list = Lists.newArrayList(); for (String part : candidate.group(2).split(",")) { if (part != null) { list.add(part); } } return list; }