You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
335 lines
6.6 KiB
335 lines
6.6 KiB
11 years ago
|
package mightypork.util.objects;
|
||
11 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
import mightypork.util.logging.Log;
|
||
|
import mightypork.util.math.Calc;
|
||
|
import mightypork.util.math.Range;
|
||
|
import mightypork.util.math.constraints.vect.Vect;
|
||
|
import mightypork.util.math.constraints.vect.VectConst;
|
||
11 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Utility for converting Object to data types; Can also convert strings to data
|
||
|
* types.
|
||
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
11 years ago
|
public class Convert {
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Get INTEGER
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return integer
|
||
|
*/
|
||
11 years ago
|
public static int toInteger(Object o, Integer def)
|
||
11 years ago
|
{
|
||
|
try {
|
||
|
if (o == null) return def;
|
||
|
if (o instanceof Number) return ((Number) o).intValue();
|
||
11 years ago
|
if (o instanceof String) return (int) Math.round(Double.parseDouble((String) o));
|
||
11 years ago
|
if (o instanceof Range) return ((Range) o).randInt();
|
||
|
if (o instanceof Boolean) return ((Boolean) o) ? 1 : 0;
|
||
11 years ago
|
} catch (final NumberFormatException e) {}
|
||
11 years ago
|
return def;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get DOUBLE
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return double
|
||
|
*/
|
||
11 years ago
|
public static double toDouble(Object o, Double def)
|
||
11 years ago
|
{
|
||
|
try {
|
||
|
if (o == null) return def;
|
||
|
if (o instanceof Number) return ((Number) o).doubleValue();
|
||
11 years ago
|
if (o instanceof String) return Double.parseDouble((String) o);
|
||
11 years ago
|
if (o instanceof Range) return ((Range) o).randDouble();
|
||
|
if (o instanceof Boolean) return ((Boolean) o) ? 1 : 0;
|
||
11 years ago
|
} catch (final NumberFormatException e) {}
|
||
11 years ago
|
return def;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get FLOAT
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return float
|
||
|
*/
|
||
11 years ago
|
public static double toFloat(Object o, Float def)
|
||
11 years ago
|
{
|
||
|
try {
|
||
|
if (o == null) return def;
|
||
|
if (o instanceof Number) return ((Number) o).floatValue();
|
||
11 years ago
|
} catch (final NumberFormatException e) {}
|
||
11 years ago
|
return def;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get BOOLEAN
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return boolean
|
||
|
*/
|
||
11 years ago
|
public static boolean toBoolean(Object o, Boolean def)
|
||
11 years ago
|
{
|
||
|
if (o == null) return def;
|
||
11 years ago
|
if (o instanceof Boolean) return ((Boolean) o).booleanValue();
|
||
|
if (o instanceof Number) return ((Number) o).intValue() != 0;
|
||
11 years ago
|
|
||
11 years ago
|
if (o instanceof String) {
|
||
11 years ago
|
final String s = ((String) o).trim().toLowerCase();
|
||
11 years ago
|
if (s.equals("0")) return false;
|
||
|
if (s.equals("1")) return true;
|
||
|
try {
|
||
11 years ago
|
final double n = Double.parseDouble(s);
|
||
11 years ago
|
return n != 0;
|
||
11 years ago
|
} catch (final NumberFormatException e) {}
|
||
11 years ago
|
|
||
11 years ago
|
if (s.equals("true")) return true;
|
||
|
if (s.equals("yes")) return true;
|
||
|
if (s.equals("y")) return true;
|
||
11 years ago
|
if (s.equals("a")) return true;
|
||
11 years ago
|
if (s.equals("enabled")) return true;
|
||
11 years ago
|
|
||
11 years ago
|
if (s.equals("false")) return false;
|
||
|
if (s.equals("no")) return false;
|
||
|
if (s.equals("n")) return false;
|
||
|
if (s.equals("disabled")) return true;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return def;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get STRING
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return String
|
||
|
*/
|
||
11 years ago
|
public static String toString(Object o, String def)
|
||
11 years ago
|
{
|
||
|
if (o == null) return def;
|
||
|
if (o instanceof String) return ((String) o);
|
||
11 years ago
|
|
||
11 years ago
|
if (o instanceof Float) return Calc.toString((float) o);
|
||
|
|
||
|
if (o instanceof Double) return Calc.toString((double) o);
|
||
11 years ago
|
|
||
11 years ago
|
if (o instanceof Vect) {
|
||
|
final Vect c = (Vect) o;
|
||
11 years ago
|
return String.format("(%f|%f|%f)", c.x(), c.y(), c.z());
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
if (o instanceof Range) {
|
||
|
final Range c = (Range) o;
|
||
11 years ago
|
return String.format("{%f|%f}", c.getMin(), c.getMax());
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
if (o instanceof Class<?>) {
|
||
11 years ago
|
return Log.str(o);
|
||
|
}
|
||
|
|
||
11 years ago
|
return o.toString();
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* Get a vector
|
||
11 years ago
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return vector
|
||
11 years ago
|
*/
|
||
11 years ago
|
public static VectConst toVect(Object o, Vect def)
|
||
11 years ago
|
{
|
||
|
try {
|
||
11 years ago
|
if (o == null) return def.freeze();
|
||
|
if (o instanceof Vect) return ((Vect) o).freeze();
|
||
11 years ago
|
if (o instanceof String) {
|
||
11 years ago
|
String s = ((String) o).trim();
|
||
|
|
||
|
// drop whitespace
|
||
|
s = s.replaceAll("\\s", "");
|
||
11 years ago
|
|
||
11 years ago
|
// drop brackets
|
||
11 years ago
|
s = s.replaceAll("[\\(\\[\\{\\)\\]\\}]", "");
|
||
11 years ago
|
|
||
|
// norm separators
|
||
|
s = s.replaceAll("[:;]", "|");
|
||
|
|
||
|
// norm floating point
|
||
|
s = s.replaceAll("[,]", ".");
|
||
|
|
||
|
final String[] parts = s.split("[|]");
|
||
11 years ago
|
|
||
|
if (parts.length >= 2) {
|
||
|
|
||
|
final double x = Double.parseDouble(parts[0].trim());
|
||
|
final double y = Double.parseDouble(parts[1].trim());
|
||
|
|
||
|
if (parts.length == 2) {
|
||
11 years ago
|
return Vect.make(x, y);
|
||
11 years ago
|
}
|
||
|
|
||
|
final double z = Double.parseDouble(parts[2].trim());
|
||
|
|
||
11 years ago
|
return Vect.make(x, y, z);
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
} catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) {
|
||
11 years ago
|
// ignore
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return def.freeze();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get RANGE
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return AiCoord
|
||
|
*/
|
||
11 years ago
|
public static Range toRange(Object o, Range def)
|
||
11 years ago
|
{
|
||
|
try {
|
||
|
if (o == null) return def;
|
||
11 years ago
|
if (o instanceof Range) return (Range) o;
|
||
11 years ago
|
if (o instanceof Number) return new Range(((Number) o).doubleValue(), ((Number) o).doubleValue());
|
||
|
if (o instanceof String) {
|
||
|
String s = ((String) o).trim();
|
||
11 years ago
|
|
||
11 years ago
|
// drop whitespace
|
||
|
s = s.replaceAll("\\s", "");
|
||
|
|
||
|
// drop brackets
|
||
11 years ago
|
s = s.replaceAll("[\\(\\[\\{\\)\\]\\}]", "");
|
||
11 years ago
|
|
||
11 years ago
|
// norm separators
|
||
|
s = s.replaceAll("[:;]", "|").replace("..", "|");
|
||
|
|
||
|
// norm floating point
|
||
|
s = s.replaceAll("[,]", ".");
|
||
|
|
||
|
// dash to pipe, if not a minus sign
|
||
|
s = s.replaceAll("([0-9])\\s?[\\-]", "$1|");
|
||
|
|
||
|
final String[] parts = s.split("[|]");
|
||
|
|
||
|
if (parts.length >= 1) {
|
||
|
|
||
|
final double low = Double.parseDouble(parts[0].trim());
|
||
|
|
||
|
if (parts.length == 2) {
|
||
|
final double high = Double.parseDouble(parts[1].trim());
|
||
|
return Range.make(low, high);
|
||
|
}
|
||
|
|
||
|
return Range.make(low, low);
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
} catch (final NumberFormatException e) {
|
||
|
// ignore
|
||
|
}
|
||
11 years ago
|
return def;
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get INTEGER
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return integer
|
||
|
*/
|
||
11 years ago
|
public static int toInteger(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toInteger(o, 0);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get DOUBLE
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return double
|
||
|
*/
|
||
11 years ago
|
public static double toDouble(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toDouble(o, 0d);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get FLOAT
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return float
|
||
|
*/
|
||
11 years ago
|
public static double toFloat(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toFloat(o, 0f);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get BOOLEAN
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return boolean
|
||
|
*/
|
||
11 years ago
|
public static boolean toBoolean(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toBoolean(o, false);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get STRING
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return String
|
||
|
*/
|
||
11 years ago
|
public static String toString(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toString(o, "");
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* Get a vector of two or three coordinates
|
||
11 years ago
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return Coord
|
||
11 years ago
|
*/
|
||
11 years ago
|
public static VectConst toVect(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toVect(o, Vect.ZERO);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get RANGE
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return AiCoord
|
||
|
*/
|
||
11 years ago
|
public static Range toRange(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toRange(o, new Range());
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
}
|