StatsSet.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. final Object val = _set.get(name);
  187. if (val == null)
  188. {
  189. throw new IllegalArgumentException("Integer value required, but not specified: " + name + "!");
  190. }
  191. if (val instanceof Number)
  192. {
  193. return ((Number) val).intValue();
  194. }
  195. try
  196. {
  197. return Integer.parseInt((String) val);
  198. }
  199. catch (Exception e)
  200. {
  201. throw new IllegalArgumentException("Integer value required, but found: " + val + "!");
  202. }
  203. }
  204. /**
  205. * 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.
  206. * @param name : String designating the key in the set
  207. * @param deflt : int designating the default value if value associated with the key is null
  208. * @return int : value associated to the key
  209. */
  210. public int getInteger(String name, int deflt)
  211. {
  212. Object val = _set.get(name);
  213. if (val == null)
  214. return deflt;
  215. if (val instanceof Number)
  216. return ((Number) val).intValue();
  217. try
  218. {
  219. return Integer.parseInt((String) val);
  220. }
  221. catch (Exception e)
  222. {
  223. throw new IllegalArgumentException("Integer value required, but found: " + val);
  224. }
  225. }
  226. /**
  227. * 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.
  228. * @param name : String designating the key in the set
  229. * @return int[] : value associated to the key
  230. */
  231. public int[] getIntegerArray(String name)
  232. {
  233. Object val = _set.get(name);
  234. if (val == null)
  235. throw new IllegalArgumentException("Integer value required, but not specified");
  236. if (val instanceof Number)
  237. {
  238. int[] result =
  239. {
  240. ((Number) val).intValue()
  241. };
  242. return result;
  243. }
  244. int c = 0;
  245. String[] vals = ((String) val).split(";");
  246. int[] result = new int[vals.length];
  247. for (String v : vals)
  248. {
  249. try
  250. {
  251. result[c++] = Integer.parseInt(v);
  252. }
  253. catch (Exception e)
  254. {
  255. throw new IllegalArgumentException("Integer value required, but found: " + val);
  256. }
  257. }
  258. return result;
  259. }
  260. /**
  261. * Returns the long associated to the key put in parameter ("name").
  262. * @param name : String designating the key in the set
  263. * @return long : value associated to the key
  264. */
  265. public long getLong(String name)
  266. {
  267. Object val = _set.get(name);
  268. if (val == null)
  269. throw new IllegalArgumentException("Integer value required, but not specified");
  270. if (val instanceof Number)
  271. return ((Number) val).longValue();
  272. try
  273. {
  274. return Long.parseLong((String) val);
  275. }
  276. catch (Exception e)
  277. {
  278. throw new IllegalArgumentException("Integer value required, but found: " + val);
  279. }
  280. }
  281. /**
  282. * 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.
  283. * @param name : String designating the key in the set
  284. * @param deflt : long designating the default value if value associated with the key is null
  285. * @return long : value associated to the key
  286. */
  287. public long getLong(String name, int deflt)
  288. {
  289. Object val = _set.get(name);
  290. if (val == null)
  291. return deflt;
  292. if (val instanceof Number)
  293. return ((Number) val).longValue();
  294. try
  295. {
  296. return Long.parseLong((String) val);
  297. }
  298. catch (Exception e)
  299. {
  300. throw new IllegalArgumentException("Integer value required, but found: " + val);
  301. }
  302. }
  303. /**
  304. * Returns the float associated to the key put in parameter ("name").
  305. * @param name : String designating the key in the set
  306. * @return float : value associated to the key
  307. */
  308. public float getFloat(String name)
  309. {
  310. Object val = _set.get(name);
  311. if (val == null)
  312. throw new IllegalArgumentException("Float value required, but not specified");
  313. if (val instanceof Number)
  314. return ((Number) val).floatValue();
  315. try
  316. {
  317. return (float) Double.parseDouble((String) val);
  318. }
  319. catch (Exception e)
  320. {
  321. throw new IllegalArgumentException("Float value required, but found: " + val);
  322. }
  323. }
  324. /**
  325. * 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.
  326. * @param name : String designating the key in the set
  327. * @param deflt : float designating the default value if value associated with the key is null
  328. * @return float : value associated to the key
  329. */
  330. public float getFloat(String name, float deflt)
  331. {
  332. Object val = _set.get(name);
  333. if (val == null)
  334. return deflt;
  335. if (val instanceof Number)
  336. return ((Number) val).floatValue();
  337. try
  338. {
  339. return (float) Double.parseDouble((String) val);
  340. }
  341. catch (Exception e)
  342. {
  343. throw new IllegalArgumentException("Float value required, but found: " + val);
  344. }
  345. }
  346. /**
  347. * Returns the double associated to the key put in parameter ("name").
  348. * @param name : String designating the key in the set
  349. * @return double : value associated to the key
  350. */
  351. public double getDouble(String name)
  352. {
  353. Object val = _set.get(name);
  354. if (val == null)
  355. throw new IllegalArgumentException("Float value required, but not specified");
  356. if (val instanceof Number)
  357. return ((Number) val).doubleValue();
  358. try
  359. {
  360. return Double.parseDouble((String) val);
  361. }
  362. catch (Exception e)
  363. {
  364. throw new IllegalArgumentException("Float value required, but found: " + val);
  365. }
  366. }
  367. /**
  368. * 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.
  369. * @param name : String designating the key in the set
  370. * @param deflt : float designating the default value if value associated with the key is null
  371. * @return double : value associated to the key
  372. */
  373. public double getDouble(String name, double deflt)
  374. {
  375. Object val = _set.get(name);
  376. if (val == null)
  377. return deflt;
  378. if (val instanceof Number)
  379. return ((Number) val).doubleValue();
  380. try
  381. {
  382. return Double.parseDouble((String) val);
  383. }
  384. catch (Exception e)
  385. {
  386. throw new IllegalArgumentException("Float value required, but found: " + val);
  387. }
  388. }
  389. /**
  390. * Returns the String associated to the key put in parameter ("name").
  391. * @param name : String designating the key in the set
  392. * @return String : value associated to the key
  393. */
  394. public String getString(String name)
  395. {
  396. Object val = _set.get(name);
  397. if (val == null)
  398. throw new IllegalArgumentException("String value required, but not specified");
  399. return String.valueOf(val);
  400. }
  401. /**
  402. * 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.
  403. * @param name : String designating the key in the set
  404. * @param deflt : String designating the default value if value associated with the key is null
  405. * @return String : value associated to the key
  406. */
  407. public String getString(String name, String deflt)
  408. {
  409. Object val = _set.get(name);
  410. if (val == null)
  411. return deflt;
  412. return String.valueOf(val);
  413. }
  414. /**
  415. * Returns an enumeration of &lt;T&gt; from the set
  416. * @param <T> : Class of the enumeration returned
  417. * @param name : String designating the key in the set
  418. * @param enumClass : Class designating the class of the value associated with the key in the set
  419. * @return Enum<T>
  420. */
  421. @SuppressWarnings("unchecked")
  422. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass)
  423. {
  424. Object val = _set.get(name);
  425. if (val == null)
  426. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but not specified");
  427. if (enumClass.isInstance(val))
  428. return (T) val;
  429. try
  430. {
  431. return Enum.valueOf(enumClass, String.valueOf(val));
  432. }
  433. catch (Exception e)
  434. {
  435. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
  436. }
  437. }
  438. /**
  439. * Returns an enumeration of &lt;T&gt; from the set. If the enumeration is empty, the method returns the value of the parameter "deflt".
  440. * @param <T> : Class of the enumeration returned
  441. * @param name : String designating the key in the set
  442. * @param enumClass : Class designating the class of the value associated with the key in the set
  443. * @param deflt : <T> designating the value by default
  444. * @return Enum<T>
  445. */
  446. @SuppressWarnings("unchecked")
  447. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass, T deflt)
  448. {
  449. Object val = _set.get(name);
  450. if (val == null)
  451. return deflt;
  452. if (enumClass.isInstance(val))
  453. return (T) val;
  454. try
  455. {
  456. return Enum.valueOf(enumClass, String.valueOf(val));
  457. }
  458. catch (Exception e)
  459. {
  460. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + "required, but found: " + val);
  461. }
  462. }
  463. /**
  464. * Add the String hold in param "value" for the key "name"
  465. * @param name : String designating the key in the set
  466. * @param value : String corresponding to the value associated with the key
  467. */
  468. public void set(String name, String value)
  469. {
  470. _set.put(name, value);
  471. }
  472. /**
  473. * Add the boolean hold in param "value" for the key "name"
  474. * @param name : String designating the key in the set
  475. * @param value : boolean corresponding to the value associated with the key
  476. */
  477. public void set(String name, boolean value)
  478. {
  479. _set.put(name, value);
  480. }
  481. /**
  482. * Add the int hold in param "value" for the key "name"
  483. * @param name : String designating the key in the set
  484. * @param value : int corresponding to the value associated with the key
  485. */
  486. public void set(String name, int value)
  487. {
  488. _set.put(name, value);
  489. }
  490. /**
  491. * Safe version of "set". Expected values are within [min, max[<br>
  492. * Add the int hold in param "value" for the key "name".
  493. * @param name : String designating the key in the set
  494. * @param value : int corresponding to the value associated with the key
  495. * @param min
  496. * @param max
  497. * @param reference
  498. */
  499. public void safeSet(String name, int value, int min, int max, String reference)
  500. {
  501. assert !((min <= max && (value < min || value >= max)));
  502. if (min <= max && (value < min || value >= max))
  503. {
  504. _log.log(Level.SEVERE, "Incorrect value: " + value + "for: " + name + "Ref: " + reference);
  505. }
  506. set(name, value);
  507. }
  508. /**
  509. * Add the double hold in param "value" for the key "name"
  510. * @param name : String designating the key in the set
  511. * @param value : double corresponding to the value associated with the key
  512. */
  513. public void set(String name, double value)
  514. {
  515. _set.put(name, value);
  516. }
  517. /**
  518. * Add the long hold in param "value" for the key "name"
  519. * @param name : String designating the key in the set
  520. * @param value : double corresponding to the value associated with the key
  521. */
  522. public void set(String name, long value)
  523. {
  524. _set.put(name, value);
  525. }
  526. /**
  527. * Add the Enum hold in param "value" for the key "name"
  528. * @param name : String designating the key in the set
  529. * @param value : Enum corresponding to the value associated with the key
  530. */
  531. public void set(String name, Enum<?> value)
  532. {
  533. _set.put(name, value);
  534. }
  535. }