StatsSet.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model;
  16. import java.util.Map;
  17. import java.util.Map.Entry;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastMap;
  21. /**
  22. * @author mkizub <BR>
  23. * This class is used in order to have a set of couples (key,value).<BR>
  24. * Methods deployed are accessors to the set (add/get value from its key) and addition of a whole set in the current one.
  25. */
  26. public final class StatsSet
  27. {
  28. private static final Logger _log = Logger.getLogger(StatsSet.class.getName());
  29. private final Map<String, Object> _set = new FastMap<String, Object>();
  30. /**
  31. * Returns the set of values
  32. * @return HashMap
  33. */
  34. public final Map<String, Object> getSet()
  35. {
  36. return _set;
  37. }
  38. /**
  39. * Add a set of couple values in the current set
  40. * @param newSet : StatsSet pointing out the list of couples to add in the current set
  41. */
  42. public void add(StatsSet newSet)
  43. {
  44. Map<String, Object> newMap = newSet.getSet();
  45. for (Entry<String, Object> entry : newMap.entrySet())
  46. {
  47. _set.put(entry.getKey(), entry.getValue());
  48. }
  49. }
  50. /**
  51. * Return the boolean associated to the key put in parameter ("name")
  52. * @param name : String designating the key in the set
  53. * @return boolean : value associated to the key
  54. */
  55. public boolean getBool(String name)
  56. {
  57. Object val = _set.get(name);
  58. if (val == null)
  59. throw new IllegalArgumentException("Boolean value required, but not specified");
  60. if (val instanceof Boolean)
  61. return ((Boolean) val).booleanValue();
  62. try
  63. {
  64. return Boolean.parseBoolean((String) val);
  65. }
  66. catch (Exception e)
  67. {
  68. throw new IllegalArgumentException("Boolean value required, but found: " + val);
  69. }
  70. }
  71. /**
  72. * Return the boolean associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  73. * @param name : String designating the key in the set
  74. * @param deflt : boolean designating the default value if value associated with the key is null
  75. * @return boolean : value of the key
  76. */
  77. public boolean getBool(String name, boolean deflt)
  78. {
  79. Object val = _set.get(name);
  80. if (val == null)
  81. return deflt;
  82. if (val instanceof Boolean)
  83. return ((Boolean) val).booleanValue();
  84. try
  85. {
  86. return Boolean.parseBoolean((String) val);
  87. }
  88. catch (Exception e)
  89. {
  90. throw new IllegalArgumentException("Boolean value required, but found: " + val);
  91. }
  92. }
  93. /**
  94. * Returns the int associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  95. * @param name : String designating the key in the set
  96. * @param deflt : byte designating the default value if value associated with the key is null
  97. * @return byte : value associated to the key
  98. */
  99. public byte getByte(String name, byte deflt)
  100. {
  101. Object val = _set.get(name);
  102. if (val == null)
  103. return deflt;
  104. if (val instanceof Number)
  105. return ((Number) val).byteValue();
  106. try
  107. {
  108. return Byte.parseByte((String) val);
  109. }
  110. catch (Exception e)
  111. {
  112. throw new IllegalArgumentException("Byte value required, but found: " + val);
  113. }
  114. }
  115. /**
  116. * Returns the byte associated to the key put in parameter ("name").
  117. * @param name : String designating the key in the set
  118. * @return byte : value associated to the key
  119. */
  120. public byte getByte(String name)
  121. {
  122. Object val = _set.get(name);
  123. if (val == null)
  124. throw new IllegalArgumentException("Byte value required, but not specified");
  125. if (val instanceof Number)
  126. return ((Number) val).byteValue();
  127. try
  128. {
  129. return Byte.parseByte((String) val);
  130. }
  131. catch (Exception e)
  132. {
  133. throw new IllegalArgumentException("Byte value required, but found: " + val);
  134. }
  135. }
  136. /**
  137. * Returns the short associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  138. * @param name : String designating the key in the set
  139. * @param deflt : short designating the default value if value associated with the key is null
  140. * @return short : value associated to the key
  141. */
  142. public short getShort(String name, short deflt)
  143. {
  144. Object val = _set.get(name);
  145. if (val == null)
  146. return deflt;
  147. if (val instanceof Number)
  148. return ((Number) val).shortValue();
  149. try
  150. {
  151. return Short.parseShort((String) val);
  152. }
  153. catch (Exception e)
  154. {
  155. throw new IllegalArgumentException("Short value required, but found: " + val);
  156. }
  157. }
  158. /**
  159. * Returns the short associated to the key put in parameter ("name").
  160. * @param name : String designating the key in the set
  161. * @return short : value associated to the key
  162. */
  163. public short getShort(String name)
  164. {
  165. Object val = _set.get(name);
  166. if (val == null)
  167. throw new IllegalArgumentException("Short value required, but not specified");
  168. if (val instanceof Number)
  169. return ((Number) val).shortValue();
  170. try
  171. {
  172. return Short.parseShort((String) val);
  173. }
  174. catch (Exception e)
  175. {
  176. throw new IllegalArgumentException("Short value required, but found: " + val);
  177. }
  178. }
  179. /**
  180. * Returns the int associated to the key put in parameter ("name").
  181. * @param name : String designating the key in the set
  182. * @return int : value associated to the key
  183. */
  184. public int getInteger(String name)
  185. {
  186. Object val = _set.get(name);
  187. if (val == null)
  188. throw new IllegalArgumentException("Integer value required, but not specified");
  189. if (val instanceof Number)
  190. return ((Number) val).intValue();
  191. try
  192. {
  193. return Integer.parseInt((String) val);
  194. }
  195. catch (Exception e)
  196. {
  197. throw new IllegalArgumentException("Integer value required, but found: " + val);
  198. }
  199. }
  200. /**
  201. * Returns the int associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  202. * @param name : String designating the key in the set
  203. * @param deflt : int designating the default value if value associated with the key is null
  204. * @return int : value associated to the key
  205. */
  206. public int getInteger(String name, int deflt)
  207. {
  208. Object val = _set.get(name);
  209. if (val == null)
  210. return deflt;
  211. if (val instanceof Number)
  212. return ((Number) val).intValue();
  213. try
  214. {
  215. return Integer.parseInt((String) val);
  216. }
  217. catch (Exception e)
  218. {
  219. throw new IllegalArgumentException("Integer value required, but found: " + val);
  220. }
  221. }
  222. /**
  223. * Returns the int[] associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  224. * @param name : String designating the key in the set
  225. * @return int[] : value associated to the key
  226. */
  227. public int[] getIntegerArray(String name)
  228. {
  229. Object val = _set.get(name);
  230. if (val == null)
  231. throw new IllegalArgumentException("Integer value required, but not specified");
  232. if (val instanceof Number)
  233. {
  234. int[] result =
  235. {
  236. ((Number) val).intValue()
  237. };
  238. return result;
  239. }
  240. int c = 0;
  241. String[] vals = ((String) val).split(";");
  242. int[] result = new int[vals.length];
  243. for (String v : vals)
  244. {
  245. try
  246. {
  247. result[c++] = Integer.parseInt(v);
  248. }
  249. catch (Exception e)
  250. {
  251. throw new IllegalArgumentException("Integer value required, but found: " + val);
  252. }
  253. }
  254. return result;
  255. }
  256. /**
  257. * Returns the long associated to the key put in parameter ("name").
  258. * @param name : String designating the key in the set
  259. * @return long : value associated to the key
  260. */
  261. public long getLong(String name)
  262. {
  263. Object val = _set.get(name);
  264. if (val == null)
  265. throw new IllegalArgumentException("Integer value required, but not specified");
  266. if (val instanceof Number)
  267. return ((Number) val).longValue();
  268. try
  269. {
  270. return Long.parseLong((String) val);
  271. }
  272. catch (Exception e)
  273. {
  274. throw new IllegalArgumentException("Integer value required, but found: " + val);
  275. }
  276. }
  277. /**
  278. * Returns the long associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  279. * @param name : String designating the key in the set
  280. * @param deflt : long designating the default value if value associated with the key is null
  281. * @return long : value associated to the key
  282. */
  283. public long getLong(String name, int deflt)
  284. {
  285. Object val = _set.get(name);
  286. if (val == null)
  287. return deflt;
  288. if (val instanceof Number)
  289. return ((Number) val).longValue();
  290. try
  291. {
  292. return Long.parseLong((String) val);
  293. }
  294. catch (Exception e)
  295. {
  296. throw new IllegalArgumentException("Integer value required, but found: " + val);
  297. }
  298. }
  299. /**
  300. * Returns the float associated to the key put in parameter ("name").
  301. * @param name : String designating the key in the set
  302. * @return float : value associated to the key
  303. */
  304. public float getFloat(String name)
  305. {
  306. Object val = _set.get(name);
  307. if (val == null)
  308. throw new IllegalArgumentException("Float value required, but not specified");
  309. if (val instanceof Number)
  310. return ((Number) val).floatValue();
  311. try
  312. {
  313. return (float) Double.parseDouble((String) val);
  314. }
  315. catch (Exception e)
  316. {
  317. throw new IllegalArgumentException("Float value required, but found: " + val);
  318. }
  319. }
  320. /**
  321. * Returns the float associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  322. * @param name : String designating the key in the set
  323. * @param deflt : float designating the default value if value associated with the key is null
  324. * @return float : value associated to the key
  325. */
  326. public float getFloat(String name, float deflt)
  327. {
  328. Object val = _set.get(name);
  329. if (val == null)
  330. return deflt;
  331. if (val instanceof Number)
  332. return ((Number) val).floatValue();
  333. try
  334. {
  335. return (float) Double.parseDouble((String) val);
  336. }
  337. catch (Exception e)
  338. {
  339. throw new IllegalArgumentException("Float value required, but found: " + val);
  340. }
  341. }
  342. /**
  343. * Returns the double associated to the key put in parameter ("name").
  344. * @param name : String designating the key in the set
  345. * @return double : value associated to the key
  346. */
  347. public double getDouble(String name)
  348. {
  349. Object val = _set.get(name);
  350. if (val == null)
  351. throw new IllegalArgumentException("Float value required, but not specified");
  352. if (val instanceof Number)
  353. return ((Number) val).doubleValue();
  354. try
  355. {
  356. return Double.parseDouble((String) val);
  357. }
  358. catch (Exception e)
  359. {
  360. throw new IllegalArgumentException("Float value required, but found: " + val);
  361. }
  362. }
  363. /**
  364. * Returns the double associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  365. * @param name : String designating the key in the set
  366. * @param deflt : float designating the default value if value associated with the key is null
  367. * @return double : value associated to the key
  368. */
  369. public double getDouble(String name, float deflt)
  370. {
  371. Object val = _set.get(name);
  372. if (val == null)
  373. return deflt;
  374. if (val instanceof Number)
  375. return ((Number) val).doubleValue();
  376. try
  377. {
  378. return Double.parseDouble((String) val);
  379. }
  380. catch (Exception e)
  381. {
  382. throw new IllegalArgumentException("Float value required, but found: " + val);
  383. }
  384. }
  385. /**
  386. * Returns the String associated to the key put in parameter ("name").
  387. * @param name : String designating the key in the set
  388. * @return String : value associated to the key
  389. */
  390. public String getString(String name)
  391. {
  392. Object val = _set.get(name);
  393. if (val == null)
  394. throw new IllegalArgumentException("String value required, but not specified");
  395. return String.valueOf(val);
  396. }
  397. /**
  398. * Returns the String associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
  399. * @param name : String designating the key in the set
  400. * @param deflt : String designating the default value if value associated with the key is null
  401. * @return String : value associated to the key
  402. */
  403. public String getString(String name, String deflt)
  404. {
  405. Object val = _set.get(name);
  406. if (val == null)
  407. return deflt;
  408. return String.valueOf(val);
  409. }
  410. /**
  411. * Returns an enumeration of &lt;T&gt; from the set
  412. * @param <T> : Class of the enumeration returned
  413. * @param name : String designating the key in the set
  414. * @param enumClass : Class designating the class of the value associated with the key in the set
  415. * @return Enum<T>
  416. */
  417. @SuppressWarnings("unchecked")
  418. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass)
  419. {
  420. Object val = _set.get(name);
  421. if (val == null)
  422. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but not specified");
  423. if (enumClass.isInstance(val))
  424. return (T) val;
  425. try
  426. {
  427. return Enum.valueOf(enumClass, String.valueOf(val));
  428. }
  429. catch (Exception e)
  430. {
  431. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
  432. }
  433. }
  434. /**
  435. * Returns an enumeration of &lt;T&gt; from the set. If the enumeration is empty, the method returns the value of the parameter "deflt".
  436. * @param <T> : Class of the enumeration returned
  437. * @param name : String designating the key in the set
  438. * @param enumClass : Class designating the class of the value associated with the key in the set
  439. * @param deflt : <T> designating the value by default
  440. * @return Enum<T>
  441. */
  442. @SuppressWarnings("unchecked")
  443. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass, T deflt)
  444. {
  445. Object val = _set.get(name);
  446. if (val == null)
  447. return deflt;
  448. if (enumClass.isInstance(val))
  449. return (T) val;
  450. try
  451. {
  452. return Enum.valueOf(enumClass, String.valueOf(val));
  453. }
  454. catch (Exception e)
  455. {
  456. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + "required, but found: " + val);
  457. }
  458. }
  459. /**
  460. * Add the String hold in param "value" for the key "name"
  461. * @param name : String designating the key in the set
  462. * @param value : String corresponding to the value associated with the key
  463. */
  464. public void set(String name, String value)
  465. {
  466. _set.put(name, value);
  467. }
  468. /**
  469. * Add the boolean hold in param "value" for the key "name"
  470. * @param name : String designating the key in the set
  471. * @param value : boolean corresponding to the value associated with the key
  472. */
  473. public void set(String name, boolean value)
  474. {
  475. _set.put(name, value);
  476. }
  477. /**
  478. * Add the int hold in param "value" for the key "name"
  479. * @param name : String designating the key in the set
  480. * @param value : int corresponding to the value associated with the key
  481. */
  482. public void set(String name, int value)
  483. {
  484. _set.put(name, value);
  485. }
  486. /**
  487. * Safe version of "set". Expected values are within [min, max[<br>
  488. * Add the int hold in param "value" for the key "name".
  489. * @param name : String designating the key in the set
  490. * @param value : int corresponding to the value associated with the key
  491. * @param min
  492. * @param max
  493. * @param reference
  494. */
  495. public void safeSet(String name, int value, int min, int max, String reference)
  496. {
  497. assert !((min <= max && (value < min || value >= max)));
  498. if (min <= max && (value < min || value >= max))
  499. {
  500. _log.log(Level.SEVERE, "Incorrect value: " + value + "for: " + name + "Ref: " + reference);
  501. }
  502. set(name, value);
  503. }
  504. /**
  505. * Add the double hold in param "value" for the key "name"
  506. * @param name : String designating the key in the set
  507. * @param value : double corresponding to the value associated with the key
  508. */
  509. public void set(String name, double value)
  510. {
  511. _set.put(name, value);
  512. }
  513. /**
  514. * Add the long hold in param "value" for the key "name"
  515. * @param name : String designating the key in the set
  516. * @param value : double corresponding to the value associated with the key
  517. */
  518. public void set(String name, long value)
  519. {
  520. _set.put(name, value);
  521. }
  522. /**
  523. * Add the Enum hold in param "value" for the key "name"
  524. * @param name : String designating the key in the set
  525. * @param value : Enum corresponding to the value associated with the key
  526. */
  527. public void set(String name, Enum<?> value)
  528. {
  529. _set.put(name, value);
  530. }
  531. }