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.
295 lines
5.9 KiB
295 lines
5.9 KiB
11 years ago
|
package mightypork.utils.objects;
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
import mightypork.utils.logging.Log;
|
||
|
import mightypork.utils.math.Range;
|
||
|
import mightypork.utils.math.coord.Coord;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 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 String) return (int) Math.round(Double.parseDouble((String) o));
|
||
|
if (o instanceof Number) return ((Number) o).intValue();
|
||
|
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 String) return Double.parseDouble((String) o);
|
||
|
if (o instanceof Number) return ((Number) o).doubleValue();
|
||
|
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
|
|
||
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;
|
||
|
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
|
if (o instanceof Boolean) return ((Boolean) o).booleanValue();
|
||
|
if (o instanceof Number) return ((Number) o).intValue() != 0;
|
||
|
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
|
|
||
|
if(o instanceof Boolean) {
|
||
|
return (Boolean) o ? "True" : "False";
|
||
|
}
|
||
|
|
||
|
if(o instanceof Coord) {
|
||
|
Coord c = (Coord) o;
|
||
|
return String.format("[%f:%f:%f]", c.x, c.y, c.z);
|
||
|
}
|
||
|
|
||
|
if(o instanceof Range) {
|
||
|
Range c = (Range) o;
|
||
|
return String.format("%f:%f", c.getMin(), c.getMax());
|
||
|
}
|
||
|
|
||
|
if(o instanceof Class<?>) {
|
||
|
return Log.str(o);
|
||
|
}
|
||
|
|
||
11 years ago
|
return o.toString();
|
||
|
}
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Get AI_COORD<br>
|
||
|
* Converts special constants to magic coordinate instances.
|
||
|
*
|
||
11 years ago
|
* @param o object
|
||
|
* @param def default value
|
||
11 years ago
|
* @return AiCoord
|
||
|
*/
|
||
11 years ago
|
public static Coord toCoord(Object o, Coord def)
|
||
11 years ago
|
{
|
||
|
try {
|
||
|
if (o == null) return def;
|
||
|
if (o instanceof String) {
|
||
|
String s = ((String) o).trim().toUpperCase();
|
||
11 years ago
|
|
||
11 years ago
|
// colon to semicolon
|
||
|
s = s.replace(':', ';');
|
||
|
// remove brackets if any
|
||
|
s = s.replaceAll("[\\(\\[\\{\\)\\]\\}]", "");
|
||
11 years ago
|
final String[] parts = s.split("[;]");
|
||
11 years ago
|
return new Coord(Double.parseDouble(parts[0].trim()), Double.parseDouble(parts[1].trim()));
|
||
|
}
|
||
|
if (o instanceof Coord) return new Coord((Coord) o);
|
||
11 years ago
|
} catch (final NumberFormatException e) {
|
||
11 years ago
|
// ignore
|
||
|
}
|
||
|
return def;
|
||
|
}
|
||
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;
|
||
|
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
|
// colon to semicolon
|
||
|
s = s.replace(',', ':');
|
||
|
// comma to dot.
|
||
|
s = s.replace(';', ':');
|
||
|
// dash
|
||
|
s = s.replaceAll("([0-9])\\s?[\\-]", "$1:");
|
||
|
// remove brackets if any
|
||
|
s = s.replaceAll("[\\(\\[\\{\\)\\]\\}]", "");
|
||
11 years ago
|
final String[] parts = s.split("[:]");
|
||
11 years ago
|
if (parts.length == 2) return new Range(Double.parseDouble(parts[0].trim()), Double.parseDouble(parts[1].trim()));
|
||
|
return new Range(Double.parseDouble(parts[0].trim()), Double.parseDouble(parts[0].trim()));
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
|
if (o instanceof Range) return (Range) o;
|
||
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 Coord
|
||
11 years ago
|
*
|
||
11 years ago
|
* @param o object
|
||
11 years ago
|
* @return Coord
|
||
11 years ago
|
*/
|
||
11 years ago
|
public static Coord toCoord(Object o)
|
||
11 years ago
|
{
|
||
11 years ago
|
return toCoord(o, Coord.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
|
}
|