123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- package com.hcks.cmfds.commons.lang;
- import java.io.Serializable;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- /**
- *
- * @Author 刘厦 (liusha.information@gmail.com)
- * @Date 创建时间:2011-6-5,下午05:47:21
- * @Version 0.0.0
- *
- * 类说明:
- *
- */
- public class BlurObject implements Serializable, Cloneable {
-
- /**
- *
- */
- private static final long serialVersionUID = 4141840934670622411L;
- /** 对象 */
- public static final int OBJECT = 1;
- /** 模糊对象 */
- public static final int BLUR_OBJECT = 2;
- /** 整数 */
- public static final int INT = 3;
- /** 长整数 */
- public static final int LONG = 4;
- /** 串 */
- public static final int STRING = 5;
- /** 浮点 */
- public static final int FLOAT = 6;
- /** 双精度 */
- public static final int DOUBLE = 7;
- /** 布尔 */
- public static final int BOOLEAN = 8;
- /** 映射 */
- public static final int MAP = 9;
- /** 列表 */
- public static final int LIST = 10;
- /** 集合 */
- public static final int SET = 11;
- /** 当前存储对象值 */
- private Object attr;
- /** 当前存储对象类型 */
- private int attrType;
- /** 如果是随意对象,那么需要提供类对象 */
- private Class<? extends Object> attrClass;
- /**
- * 构造器
- *
- * @param o
- */
- public BlurObject(Object o) {
- attr = o;
- attrType = OBJECT;
- if (o != null) {
- attrClass = o.getClass();
- }
- }
- /**
- * 构造器
- *
- * @param m
- */
- @SuppressWarnings("unchecked")
- public BlurObject(Map m) {
- attr = m;
- attrType = MAP;
- attrClass = Map.class;
- }
- /**
- * 构造器
- *
- * @param s
- */
- @SuppressWarnings("unchecked")
- public BlurObject(Set s) {
- attr = s;
- attrType = SET;
- attrClass = Set.class;
- }
- /**
- * 构造器
- *
- * @param l
- */
- @SuppressWarnings("unchecked")
- public BlurObject(List l) {
- attr = l;
- attrType = LIST;
- attrClass = List.class;
- }
- /**
- * 构造器
- *
- * @param bo
- */
- public BlurObject(BlurObject bo) {
- attr = bo;
- attrType = BLUR_OBJECT;
- if (bo != null) {
- attrClass = BlurObject.class;
- }
- }
- /**
- * 构造器
- *
- * @param i
- */
- public BlurObject(int i) {
- attr = i;
- attrType = INT;
- attrClass = Integer.class;
- }
- /**
- * 构造器
- *
- * @param s
- */
- public BlurObject(String s) {
- attr = s;
- attrType = STRING;
- attrClass = String.class;
- }
- /**
- * 构造器
- *
- * @param f
- */
- public BlurObject(float f) {
- attr = f;
- attrType = FLOAT;
- attrClass = Float.class;
- }
- /**
- * 构造器
- *
- * @param d
- */
- public BlurObject(double d) {
- attr = d;
- attrType = DOUBLE;
- attrClass = Double.class;
- }
- /**
- * 构造器
- *
- * @param l
- */
- public BlurObject(long l) {
- attr = l;
- attrType = LONG;
- attrClass = Long.class;
- }
- /**
- * 构造器
- *
- * @param o
- * @param c
- */
- public BlurObject(Object o, Class<? extends Object> c) {
- attr = o;
- attrType = OBJECT;
- attrClass = c;
- }
- /**
- * 构造器
- *
- * @param b
- */
- public BlurObject(boolean b) {
- attr = b;
- attrType = BOOLEAN;
- attrClass = Boolean.class;
- }
- /**
- * 获取数据类型
- *
- * @return
- */
- public int getObjectType() {
- return attrType;
- }
- /**
- * 输出对象
- *
- * @return
- */
- public Object toObjectValue() {
- return attr;
- }
- /**
- * 输出模糊对象
- *
- * @return
- */
- public BlurObject toBlurObjectValue() {
- if (attrType == BLUR_OBJECT) {
- return (BlurObject) attr;
- }
- return new BlurObject(attr);
- }
- /**
- * 输出为映射
- *
- * @return
- */
- @SuppressWarnings("unchecked")
- public Map toMapValue() {
- if (attrType == MAP) {
- return (Map) attr;
- }
- return null;
- }
- /**
- * 输出为列表
- *
- * @return
- */
- @SuppressWarnings("unchecked")
- public List toListValue() {
- if (attrType == LIST) {
- return (List) attr;
- }
- return null;
- }
- /**
- * 输出为集合
- *
- * @return
- */
- @SuppressWarnings("unchecked")
- public Set toSetValue() {
- if (attrType == SET) {
- return (Set) attr;
- }
- return null;
- }
- /**
- * 输出布尔值,如果当前类型非布尔值,那么尝试转换
- *
- * @return
- */
- public boolean toBooleanValue() {
- if (attr == null) {
- return false;
- }
- if (attrType == INT) {
- return ((Integer) attr) > 0;
- }
- if (attrType == LONG) {
- return ((Long) attr) > 0;
- }
- if (attrType == FLOAT) {
- return ((Float) attr) > 0;
- }
- if (attrType == DOUBLE) {
- return ((Double) attr) > 0;
- }
- if (attrType == STRING || attrType == OBJECT) {
- return "true".equalsIgnoreCase(attr.toString());
- }
- if (attrType == BLUR_OBJECT) {
- return ((BlurObject) attr).toBooleanValue();
- }
- return false;
- }
- /**
- * 输出整数
- *
- * @return
- */
- public int toIntValue() {
- if (attr == null) {
- return 0;
- }
- if (attrType == INT) {
- return ((Integer) attr).intValue();
- }
- if (attrType == LONG) {
- return ((Long) attr).intValue();
- }
- if (attrType == FLOAT) {
- return ((Float) attr).intValue();
- }
- if (attrType == DOUBLE) {
- return ((Double) attr).intValue();
- }
- if (attrType == STRING || attrType == OBJECT) {
- int ret = 0;
- try {
- ret = new Double(Double.parseDouble(attr.toString())).intValue();
- } catch (NumberFormatException e) {
- ret = 0;
- }
- return ret;
- }
- if (attrType == BLUR_OBJECT) {
- return ((BlurObject) attr).toIntValue();
- }
- return 0;
- }
- /**
- * 输出串
- *
- * @return
- */
- public String toStringValue() {
- if (attr == null) {
- return null;
- }
- if (attrType == STRING || attrType == OBJECT) {
- return attr.toString();
- }
- if (attrType == BLUR_OBJECT) {
- return ((BlurObject) attr).toString();
- }
- if (attrType == INT) {
- return attr + "";
- }
- if (attrType == LONG) {
- return attr + "";
- }
- if (attrType == FLOAT) {
- return attr + "";
- }
- if (attrType == DOUBLE) {
- return attr + "";
- }
- if (attrType == BOOLEAN) {
- return attr + "";
- }
- return "";
- }
- /**
- * 输出浮点数
- *
- * @return
- */
- public float toFloatValue() {
- if (attr == null) {
- return 0.0f;
- }
- if (attrType == INT) {
- return ((Integer) attr).floatValue();
- }
- if (attrType == LONG) {
- return ((Long) attr).floatValue();
- }
- if (attrType == FLOAT) {
- return ((Float) attr).floatValue();
- }
- if (attrType == DOUBLE) {
- return ((Double) attr).floatValue();
- }
- if (attrType == STRING || attrType == OBJECT) {
- float ret = 0.0f;
- try {
- ret = new Double(Double.parseDouble(attr.toString())).floatValue();
- } catch (NumberFormatException e) {
- ret = 0.0f;
- }
- return ret;
- }
- if (attrType == BLUR_OBJECT) {
- return ((BlurObject) attr).toFloatValue();
- }
- return 0.0f;
- }
- /**
- * 输出双精度
- *
- * @return
- */
- public double toDoubleValue() {
- if (attr == null) {
- return 0.0;
- }
- if (attrType == INT) {
- return ((Integer) attr).doubleValue();
- }
- if (attrType == LONG) {
- return ((Long) attr).doubleValue();
- }
- if (attrType == FLOAT) {
- return ((Float) attr).doubleValue();
- }
- if (attrType == DOUBLE) {
- return ((Double) attr).doubleValue();
- }
- if (attrType == STRING || attrType == OBJECT) {
- double ret = 0.0;
- try {
- ret = new Double(Double.parseDouble(attr.toString())).doubleValue();
- } catch (NumberFormatException e) {
- ret = 0.0;
- }
- return ret;
- }
- if (attrType == BLUR_OBJECT) {
- return ((BlurObject) attr).toDoubleValue();
- }
- return 0.0;
- }
- /**
- * 输出长整形
- *
- * @return
- */
- public long toLongValue() {
- if (attr == null) {
- return 0l;
- }
- if (attrType == INT) {
- return ((Integer) attr).longValue();
- }
- if (attrType == LONG) {
- return ((Long) attr).longValue();
- }
- if (attrType == FLOAT) {
- return ((Float) attr).longValue();
- }
- if (attrType == DOUBLE) {
- return ((Double) attr).longValue();
- }
- if (attrType == STRING || attrType == OBJECT) {
- long ret = 0l;
- try {
- ret = new Double(Double.parseDouble(attr.toString())).longValue();
- } catch (NumberFormatException e) {
- ret = 0l;
- }
- return ret;
- }
- if (attrType == BLUR_OBJECT) {
- return ((BlurObject) attr).toLongValue();
- }
- return 0l;
- }
- /**
- * 输出指定类的对象
- *
- * @param czlzz 指定类
- * @return 如果对象不能转换成指定类返回null,指定类是null,返回null。
- */
- public Object toObjectValue(Class<? extends Object> clazz) {
- Object object = null;
- try {
- object = clazz.cast(attr);
- } catch (ClassCastException e) {
- object = null;
- }
- return object;
- }
- /**
- * 获得对象类
- *
- * @return
- */
- public Class<? extends Object> getObjectClass() {
- if (attrClass != null) {
- return attrClass;
- } else {
- if (attr != null) {
- return attr.getClass();
- }
- }
- return null;
- }
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#hashCode()
- */
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((attr == null) ? 0 : attr.hashCode());
- result = prime * result + ((attrClass == null) ? 0 : attrClass.hashCode());
- result = prime * result + attrType;
- return result;
- }
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- BlurObject other = (BlurObject) obj;
- if (this.attr == null) {
- if (other.attr != null) {
- return false;
- }
- } else if (!this.attr.equals(other.attr)) {
- return false;
- }
- if (attrClass == null) {
- if (other.attrClass != null) {
- return false;
- }
- } else if (!attrClass.equals(other.attrClass)) {
- return false;
- }
- if (attrType != other.attrType) {
- return false;
- }
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#toString()
- */
- public String toString() {
- if (attr != null) {
- return attr.toString();
- }
- return "";
- }
- }
|