StatsSet.java 19 KB

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