StatsSet.java 17 KB

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