StatsSet.java 19 KB

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