BlurObject.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. package com.hcks.cmfds.commons.lang;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6. /**
  7. *
  8. * @Author 刘厦 (liusha.information@gmail.com)
  9. * @Date 创建时间:2011-6-5,下午05:47:21
  10. * @Version 0.0.0
  11. *
  12. * 类说明:
  13. *
  14. */
  15. public class BlurObject implements Serializable, Cloneable {
  16. /**
  17. *
  18. */
  19. private static final long serialVersionUID = 4141840934670622411L;
  20. /** 对象 */
  21. public static final int OBJECT = 1;
  22. /** 模糊对象 */
  23. public static final int BLUR_OBJECT = 2;
  24. /** 整数 */
  25. public static final int INT = 3;
  26. /** 长整数 */
  27. public static final int LONG = 4;
  28. /** 串 */
  29. public static final int STRING = 5;
  30. /** 浮点 */
  31. public static final int FLOAT = 6;
  32. /** 双精度 */
  33. public static final int DOUBLE = 7;
  34. /** 布尔 */
  35. public static final int BOOLEAN = 8;
  36. /** 映射 */
  37. public static final int MAP = 9;
  38. /** 列表 */
  39. public static final int LIST = 10;
  40. /** 集合 */
  41. public static final int SET = 11;
  42. /** 当前存储对象值 */
  43. private Object attr;
  44. /** 当前存储对象类型 */
  45. private int attrType;
  46. /** 如果是随意对象,那么需要提供类对象 */
  47. private Class<? extends Object> attrClass;
  48. /**
  49. * 构造器
  50. *
  51. * @param o
  52. */
  53. public BlurObject(Object o) {
  54. attr = o;
  55. attrType = OBJECT;
  56. if (o != null) {
  57. attrClass = o.getClass();
  58. }
  59. }
  60. /**
  61. * 构造器
  62. *
  63. * @param m
  64. */
  65. @SuppressWarnings("unchecked")
  66. public BlurObject(Map m) {
  67. attr = m;
  68. attrType = MAP;
  69. attrClass = Map.class;
  70. }
  71. /**
  72. * 构造器
  73. *
  74. * @param s
  75. */
  76. @SuppressWarnings("unchecked")
  77. public BlurObject(Set s) {
  78. attr = s;
  79. attrType = SET;
  80. attrClass = Set.class;
  81. }
  82. /**
  83. * 构造器
  84. *
  85. * @param l
  86. */
  87. @SuppressWarnings("unchecked")
  88. public BlurObject(List l) {
  89. attr = l;
  90. attrType = LIST;
  91. attrClass = List.class;
  92. }
  93. /**
  94. * 构造器
  95. *
  96. * @param bo
  97. */
  98. public BlurObject(BlurObject bo) {
  99. attr = bo;
  100. attrType = BLUR_OBJECT;
  101. if (bo != null) {
  102. attrClass = BlurObject.class;
  103. }
  104. }
  105. /**
  106. * 构造器
  107. *
  108. * @param i
  109. */
  110. public BlurObject(int i) {
  111. attr = i;
  112. attrType = INT;
  113. attrClass = Integer.class;
  114. }
  115. /**
  116. * 构造器
  117. *
  118. * @param s
  119. */
  120. public BlurObject(String s) {
  121. attr = s;
  122. attrType = STRING;
  123. attrClass = String.class;
  124. }
  125. /**
  126. * 构造器
  127. *
  128. * @param f
  129. */
  130. public BlurObject(float f) {
  131. attr = f;
  132. attrType = FLOAT;
  133. attrClass = Float.class;
  134. }
  135. /**
  136. * 构造器
  137. *
  138. * @param d
  139. */
  140. public BlurObject(double d) {
  141. attr = d;
  142. attrType = DOUBLE;
  143. attrClass = Double.class;
  144. }
  145. /**
  146. * 构造器
  147. *
  148. * @param l
  149. */
  150. public BlurObject(long l) {
  151. attr = l;
  152. attrType = LONG;
  153. attrClass = Long.class;
  154. }
  155. /**
  156. * 构造器
  157. *
  158. * @param o
  159. * @param c
  160. */
  161. public BlurObject(Object o, Class<? extends Object> c) {
  162. attr = o;
  163. attrType = OBJECT;
  164. attrClass = c;
  165. }
  166. /**
  167. * 构造器
  168. *
  169. * @param b
  170. */
  171. public BlurObject(boolean b) {
  172. attr = b;
  173. attrType = BOOLEAN;
  174. attrClass = Boolean.class;
  175. }
  176. /**
  177. * 获取数据类型
  178. *
  179. * @return
  180. */
  181. public int getObjectType() {
  182. return attrType;
  183. }
  184. /**
  185. * 输出对象
  186. *
  187. * @return
  188. */
  189. public Object toObjectValue() {
  190. return attr;
  191. }
  192. /**
  193. * 输出模糊对象
  194. *
  195. * @return
  196. */
  197. public BlurObject toBlurObjectValue() {
  198. if (attrType == BLUR_OBJECT) {
  199. return (BlurObject) attr;
  200. }
  201. return new BlurObject(attr);
  202. }
  203. /**
  204. * 输出为映射
  205. *
  206. * @return
  207. */
  208. @SuppressWarnings("unchecked")
  209. public Map toMapValue() {
  210. if (attrType == MAP) {
  211. return (Map) attr;
  212. }
  213. return null;
  214. }
  215. /**
  216. * 输出为列表
  217. *
  218. * @return
  219. */
  220. @SuppressWarnings("unchecked")
  221. public List toListValue() {
  222. if (attrType == LIST) {
  223. return (List) attr;
  224. }
  225. return null;
  226. }
  227. /**
  228. * 输出为集合
  229. *
  230. * @return
  231. */
  232. @SuppressWarnings("unchecked")
  233. public Set toSetValue() {
  234. if (attrType == SET) {
  235. return (Set) attr;
  236. }
  237. return null;
  238. }
  239. /**
  240. * 输出布尔值,如果当前类型非布尔值,那么尝试转换
  241. *
  242. * @return
  243. */
  244. public boolean toBooleanValue() {
  245. if (attr == null) {
  246. return false;
  247. }
  248. if (attrType == INT) {
  249. return ((Integer) attr) > 0;
  250. }
  251. if (attrType == LONG) {
  252. return ((Long) attr) > 0;
  253. }
  254. if (attrType == FLOAT) {
  255. return ((Float) attr) > 0;
  256. }
  257. if (attrType == DOUBLE) {
  258. return ((Double) attr) > 0;
  259. }
  260. if (attrType == STRING || attrType == OBJECT) {
  261. return "true".equalsIgnoreCase(attr.toString());
  262. }
  263. if (attrType == BLUR_OBJECT) {
  264. return ((BlurObject) attr).toBooleanValue();
  265. }
  266. return false;
  267. }
  268. /**
  269. * 输出整数
  270. *
  271. * @return
  272. */
  273. public int toIntValue() {
  274. if (attr == null) {
  275. return 0;
  276. }
  277. if (attrType == INT) {
  278. return ((Integer) attr).intValue();
  279. }
  280. if (attrType == LONG) {
  281. return ((Long) attr).intValue();
  282. }
  283. if (attrType == FLOAT) {
  284. return ((Float) attr).intValue();
  285. }
  286. if (attrType == DOUBLE) {
  287. return ((Double) attr).intValue();
  288. }
  289. if (attrType == STRING || attrType == OBJECT) {
  290. int ret = 0;
  291. try {
  292. ret = new Double(Double.parseDouble(attr.toString())).intValue();
  293. } catch (NumberFormatException e) {
  294. ret = 0;
  295. }
  296. return ret;
  297. }
  298. if (attrType == BLUR_OBJECT) {
  299. return ((BlurObject) attr).toIntValue();
  300. }
  301. return 0;
  302. }
  303. /**
  304. * 输出串
  305. *
  306. * @return
  307. */
  308. public String toStringValue() {
  309. if (attr == null) {
  310. return null;
  311. }
  312. if (attrType == STRING || attrType == OBJECT) {
  313. return attr.toString();
  314. }
  315. if (attrType == BLUR_OBJECT) {
  316. return ((BlurObject) attr).toString();
  317. }
  318. if (attrType == INT) {
  319. return attr + "";
  320. }
  321. if (attrType == LONG) {
  322. return attr + "";
  323. }
  324. if (attrType == FLOAT) {
  325. return attr + "";
  326. }
  327. if (attrType == DOUBLE) {
  328. return attr + "";
  329. }
  330. if (attrType == BOOLEAN) {
  331. return attr + "";
  332. }
  333. return "";
  334. }
  335. /**
  336. * 输出浮点数
  337. *
  338. * @return
  339. */
  340. public float toFloatValue() {
  341. if (attr == null) {
  342. return 0.0f;
  343. }
  344. if (attrType == INT) {
  345. return ((Integer) attr).floatValue();
  346. }
  347. if (attrType == LONG) {
  348. return ((Long) attr).floatValue();
  349. }
  350. if (attrType == FLOAT) {
  351. return ((Float) attr).floatValue();
  352. }
  353. if (attrType == DOUBLE) {
  354. return ((Double) attr).floatValue();
  355. }
  356. if (attrType == STRING || attrType == OBJECT) {
  357. float ret = 0.0f;
  358. try {
  359. ret = new Double(Double.parseDouble(attr.toString())).floatValue();
  360. } catch (NumberFormatException e) {
  361. ret = 0.0f;
  362. }
  363. return ret;
  364. }
  365. if (attrType == BLUR_OBJECT) {
  366. return ((BlurObject) attr).toFloatValue();
  367. }
  368. return 0.0f;
  369. }
  370. /**
  371. * 输出双精度
  372. *
  373. * @return
  374. */
  375. public double toDoubleValue() {
  376. if (attr == null) {
  377. return 0.0;
  378. }
  379. if (attrType == INT) {
  380. return ((Integer) attr).doubleValue();
  381. }
  382. if (attrType == LONG) {
  383. return ((Long) attr).doubleValue();
  384. }
  385. if (attrType == FLOAT) {
  386. return ((Float) attr).doubleValue();
  387. }
  388. if (attrType == DOUBLE) {
  389. return ((Double) attr).doubleValue();
  390. }
  391. if (attrType == STRING || attrType == OBJECT) {
  392. double ret = 0.0;
  393. try {
  394. ret = new Double(Double.parseDouble(attr.toString())).doubleValue();
  395. } catch (NumberFormatException e) {
  396. ret = 0.0;
  397. }
  398. return ret;
  399. }
  400. if (attrType == BLUR_OBJECT) {
  401. return ((BlurObject) attr).toDoubleValue();
  402. }
  403. return 0.0;
  404. }
  405. /**
  406. * 输出长整形
  407. *
  408. * @return
  409. */
  410. public long toLongValue() {
  411. if (attr == null) {
  412. return 0l;
  413. }
  414. if (attrType == INT) {
  415. return ((Integer) attr).longValue();
  416. }
  417. if (attrType == LONG) {
  418. return ((Long) attr).longValue();
  419. }
  420. if (attrType == FLOAT) {
  421. return ((Float) attr).longValue();
  422. }
  423. if (attrType == DOUBLE) {
  424. return ((Double) attr).longValue();
  425. }
  426. if (attrType == STRING || attrType == OBJECT) {
  427. long ret = 0l;
  428. try {
  429. ret = new Double(Double.parseDouble(attr.toString())).longValue();
  430. } catch (NumberFormatException e) {
  431. ret = 0l;
  432. }
  433. return ret;
  434. }
  435. if (attrType == BLUR_OBJECT) {
  436. return ((BlurObject) attr).toLongValue();
  437. }
  438. return 0l;
  439. }
  440. /**
  441. * 输出指定类的对象
  442. *
  443. * @param czlzz 指定类
  444. * @return 如果对象不能转换成指定类返回null,指定类是null,返回null。
  445. */
  446. public Object toObjectValue(Class<? extends Object> clazz) {
  447. Object object = null;
  448. try {
  449. object = clazz.cast(attr);
  450. } catch (ClassCastException e) {
  451. object = null;
  452. }
  453. return object;
  454. }
  455. /**
  456. * 获得对象类
  457. *
  458. * @return
  459. */
  460. public Class<? extends Object> getObjectClass() {
  461. if (attrClass != null) {
  462. return attrClass;
  463. } else {
  464. if (attr != null) {
  465. return attr.getClass();
  466. }
  467. }
  468. return null;
  469. }
  470. /*
  471. * (non-Javadoc)
  472. *
  473. * @see java.lang.Object#hashCode()
  474. */
  475. public int hashCode() {
  476. final int prime = 31;
  477. int result = 1;
  478. result = prime * result + ((attr == null) ? 0 : attr.hashCode());
  479. result = prime * result + ((attrClass == null) ? 0 : attrClass.hashCode());
  480. result = prime * result + attrType;
  481. return result;
  482. }
  483. /*
  484. * (non-Javadoc)
  485. *
  486. * @see java.lang.Object#equals(java.lang.Object)
  487. */
  488. public boolean equals(Object obj) {
  489. if (this == obj) {
  490. return true;
  491. }
  492. if (obj == null) {
  493. return false;
  494. }
  495. if (getClass() != obj.getClass()) {
  496. return false;
  497. }
  498. BlurObject other = (BlurObject) obj;
  499. if (this.attr == null) {
  500. if (other.attr != null) {
  501. return false;
  502. }
  503. } else if (!this.attr.equals(other.attr)) {
  504. return false;
  505. }
  506. if (attrClass == null) {
  507. if (other.attrClass != null) {
  508. return false;
  509. }
  510. } else if (!attrClass.equals(other.attrClass)) {
  511. return false;
  512. }
  513. if (attrType != other.attrType) {
  514. return false;
  515. }
  516. return true;
  517. }
  518. /*
  519. * (non-Javadoc)
  520. *
  521. * @see java.lang.Object#toString()
  522. */
  523. public String toString() {
  524. if (attr != null) {
  525. return attr.toString();
  526. }
  527. return "";
  528. }
  529. }