StatsSet.java 19 KB

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