StatsSet.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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
  23. * <BR>
  24. * This class is used in order to have a set of couples (key,value).<BR>
  25. * Methods deployed are accessors to the set (add/get value from its key) and addition of a whole set in the current one.
  26. */
  27. public final class StatsSet {
  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. return Boolean.parseBoolean((String)val);
  64. } catch (Exception e) {
  65. throw new IllegalArgumentException("Boolean value required, but found: "+val);
  66. }
  67. }
  68. /**
  69. * 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
  70. * deflt.
  71. * @param name : String designating the key in the set
  72. * @param deflt : boolean designating the default value if value associated with the key is null
  73. * @return boolean : value of the key
  74. */
  75. public boolean getBool (String name, boolean deflt)
  76. {
  77. Object val = _set.get(name);
  78. if (val == null)
  79. return deflt;
  80. if (val instanceof Boolean)
  81. return ((Boolean)val).booleanValue();
  82. try {
  83. return Boolean.parseBoolean((String)val);
  84. } catch (Exception e) {
  85. throw new IllegalArgumentException("Boolean value required, but found: "+val);
  86. }
  87. }
  88. /**
  89. * 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
  90. * deflt.
  91. * @param name : String designating the key in the set
  92. * @param deflt : byte designating the default value if value associated with the key is null
  93. * @return byte : value associated to the key
  94. */
  95. public byte getByte(String name, byte deflt)
  96. {
  97. Object val = _set.get(name);
  98. if (val == null)
  99. return deflt;
  100. if (val instanceof Number)
  101. return ((Number)val).byteValue();
  102. try {
  103. return Byte.parseByte((String)val);
  104. } catch (Exception e) {
  105. throw new IllegalArgumentException("Byte value required, but found: "+val);
  106. }
  107. }
  108. /**
  109. * Returns the byte associated to the key put in parameter ("name").
  110. * @param name : String designating the key in the set
  111. * @return byte : value associated to the key
  112. */
  113. public byte getByte(String name)
  114. {
  115. Object val = _set.get(name);
  116. if (val == null)
  117. throw new IllegalArgumentException("Byte value required, but not specified");
  118. if (val instanceof Number)
  119. return ((Number)val).byteValue();
  120. try {
  121. return Byte.parseByte((String)val);
  122. } catch (Exception e) {
  123. throw new IllegalArgumentException("Byte value required, but found: "+val);
  124. }
  125. }
  126. /**
  127. * 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
  128. * deflt.
  129. * @param name : String designating the key in the set
  130. * @param deflt : short designating the default value if value associated with the key is null
  131. * @return short : value associated to the key
  132. */
  133. public short getShort(String name, short deflt)
  134. {
  135. Object val = _set.get(name);
  136. if (val == null)
  137. return deflt;
  138. if (val instanceof Number)
  139. return ((Number)val).shortValue();
  140. try {
  141. return Short.parseShort((String)val);
  142. } catch (Exception e) {
  143. throw new IllegalArgumentException("Short value required, but found: "+val);
  144. }
  145. }
  146. /**
  147. * Returns the short associated to the key put in parameter ("name").
  148. * @param name : String designating the key in the set
  149. * @return short : value associated to the key
  150. */
  151. public short getShort(String name)
  152. {
  153. Object val = _set.get(name);
  154. if (val == null)
  155. throw new IllegalArgumentException("Short value required, but not specified");
  156. if (val instanceof Number)
  157. return ((Number)val).shortValue();
  158. try {
  159. return Short.parseShort((String)val);
  160. } catch (Exception e) {
  161. throw new IllegalArgumentException("Short value required, but found: "+val);
  162. }
  163. }
  164. /**
  165. * Returns the int associated to the key put in parameter ("name").
  166. * @param name : String designating the key in the set
  167. * @return int : value associated to the key
  168. */
  169. public int getInteger(String name)
  170. {
  171. Object val = _set.get(name);
  172. if (val == null)
  173. throw new IllegalArgumentException("Integer value required, but not specified");
  174. if (val instanceof Number)
  175. return ((Number)val).intValue();
  176. try {
  177. return Integer.parseInt((String)val);
  178. } catch (Exception e) {
  179. throw new IllegalArgumentException("Integer value required, but found: "+val);
  180. }
  181. }
  182. /**
  183. * 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
  184. * deflt.
  185. * @param name : String designating the key in the set
  186. * @param deflt : int designating the default value if value associated with the key is null
  187. * @return int : value associated to the key
  188. */
  189. public int getInteger(String name, int deflt)
  190. {
  191. Object val = _set.get(name);
  192. if (val == null)
  193. return deflt;
  194. if (val instanceof Number)
  195. return ((Number)val).intValue();
  196. try {
  197. return Integer.parseInt((String)val);
  198. } catch (Exception e) {
  199. throw new IllegalArgumentException("Integer value required, but found: "+val);
  200. }
  201. }
  202. /**
  203. * 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
  204. * deflt.
  205. * @param name : String designating the key in the set
  206. * @return int[] : value associated to the key
  207. */
  208. public int[] getIntegerArray(String name)
  209. {
  210. Object val = _set.get(name);
  211. if (val == null)
  212. throw new IllegalArgumentException("Integer value required, but not specified");
  213. if (val instanceof Number){
  214. int[] result = {((Number)val).intValue()};
  215. return result;
  216. }
  217. int c = 0;
  218. String[] vals = ((String)val).split(";");
  219. int[] result = new int[vals.length];
  220. for(String v: vals)
  221. {
  222. try {
  223. result[c++] = Integer.parseInt(v);
  224. } catch (Exception e) {
  225. throw new IllegalArgumentException("Integer value required, but found: "+val);
  226. }
  227. }
  228. return result;
  229. }
  230. /**
  231. * Returns the long associated to the key put in parameter ("name").
  232. * @param name : String designating the key in the set
  233. * @return long : value associated to the key
  234. */
  235. public long getLong(String name)
  236. {
  237. Object val = _set.get(name);
  238. if (val == null)
  239. throw new IllegalArgumentException("Integer value required, but not specified");
  240. if (val instanceof Number)
  241. return ((Number)val).longValue();
  242. try {
  243. return Long.parseLong((String)val);
  244. } catch (Exception e) {
  245. throw new IllegalArgumentException("Integer value required, but found: "+val);
  246. }
  247. }
  248. /**
  249. * 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
  250. * deflt.
  251. * @param name : String designating the key in the set
  252. * @param deflt : long designating the default value if value associated with the key is null
  253. * @return long : value associated to the key
  254. */
  255. public long getLong(String name, int deflt)
  256. {
  257. Object val = _set.get(name);
  258. if (val == null)
  259. return deflt;
  260. if (val instanceof Number)
  261. return ((Number)val).longValue();
  262. try {
  263. return Long.parseLong((String)val);
  264. } catch (Exception e) {
  265. throw new IllegalArgumentException("Integer value required, but found: "+val);
  266. }
  267. }
  268. /**
  269. * Returns the float associated to the key put in parameter ("name").
  270. * @param name : String designating the key in the set
  271. * @return float : value associated to the key
  272. */
  273. public float getFloat(String name)
  274. {
  275. Object val = _set.get(name);
  276. if (val == null)
  277. throw new IllegalArgumentException("Float value required, but not specified");
  278. if (val instanceof Number)
  279. return ((Number)val).floatValue();
  280. try {
  281. return (float)Double.parseDouble((String)val);
  282. } catch (Exception e) {
  283. throw new IllegalArgumentException("Float value required, but found: "+val);
  284. }
  285. }
  286. /**
  287. * 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
  288. * deflt.
  289. * @param name : String designating the key in the set
  290. * @param deflt : float designating the default value if value associated with the key is null
  291. * @return float : value associated to the key
  292. */
  293. public float getFloat(String name, float deflt)
  294. {
  295. Object val = _set.get(name);
  296. if (val == null)
  297. return deflt;
  298. if (val instanceof Number)
  299. return ((Number)val).floatValue();
  300. try {
  301. return (float)Double.parseDouble((String)val);
  302. } catch (Exception e) {
  303. throw new IllegalArgumentException("Float value required, but found: "+val);
  304. }
  305. }
  306. /**
  307. * Returns the double associated to the key put in parameter ("name").
  308. * @param name : String designating the key in the set
  309. * @return double : value associated to the key
  310. */
  311. public double getDouble(String name)
  312. {
  313. Object val = _set.get(name);
  314. if (val == null)
  315. throw new IllegalArgumentException("Float value required, but not specified");
  316. if (val instanceof Number)
  317. return ((Number)val).doubleValue();
  318. try {
  319. return Double.parseDouble((String)val);
  320. } catch (Exception e) {
  321. throw new IllegalArgumentException("Float value required, but found: "+val);
  322. }
  323. }
  324. /**
  325. * 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
  326. * 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 double : value associated to the key
  330. */
  331. public double getDouble(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).doubleValue();
  338. try {
  339. return Double.parseDouble((String)val);
  340. } catch (Exception e) {
  341. throw new IllegalArgumentException("Float value required, but found: "+val);
  342. }
  343. }
  344. /**
  345. * Returns the String associated to the key put in parameter ("name").
  346. * @param name : String designating the key in the set
  347. * @return String : value associated to the key
  348. */
  349. public String getString(String name)
  350. {
  351. Object val = _set.get(name);
  352. if (val == null)
  353. throw new IllegalArgumentException("String value required, but not specified");
  354. return String.valueOf(val);
  355. }
  356. /**
  357. * 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
  358. * deflt.
  359. * @param name : String designating the key in the set
  360. * @param deflt : String designating the default value if value associated with the key is null
  361. * @return String : value associated to the key
  362. */
  363. public String getString(String name, String deflt)
  364. {
  365. Object val = _set.get(name);
  366. if (val == null)
  367. return deflt;
  368. return String.valueOf(val);
  369. }
  370. /**
  371. * Returns an enumeration of &lt;T&gt; from the set
  372. * @param <T> : Class of the enumeration returned
  373. * @param name : String designating the key in the set
  374. * @param enumClass : Class designating the class of the value associated with the key in the set
  375. * @return Enum<T>
  376. */
  377. @SuppressWarnings("unchecked")
  378. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass)
  379. {
  380. Object val = _set.get(name);
  381. if (val == null)
  382. throw new IllegalArgumentException("Enum value of type "+enumClass.getName()+" required, but not specified");
  383. if (enumClass.isInstance(val))
  384. return (T)val;
  385. try {
  386. return Enum.valueOf(enumClass, String.valueOf(val));
  387. } catch (Exception e) {
  388. throw new IllegalArgumentException("Enum value of type "+enumClass.getName()+" required, but found: "+val);
  389. }
  390. }
  391. /**
  392. * Returns an enumeration of &lt;T&gt; from the set. If the enumeration is empty, the method returns the value of the parameter "deflt".
  393. * @param <T> : Class of the enumeration returned
  394. * @param name : String designating the key in the set
  395. * @param enumClass : Class designating the class of the value associated with the key in the set
  396. * @param deflt : <T> designating the value by default
  397. * @return Enum<T>
  398. */
  399. @SuppressWarnings("unchecked")
  400. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass, T deflt)
  401. {
  402. Object val = _set.get(name);
  403. if (val == null)
  404. return deflt;
  405. if (enumClass.isInstance(val))
  406. return (T)val;
  407. try {
  408. return Enum.valueOf(enumClass, String.valueOf(val));
  409. } catch (Exception e) {
  410. throw new IllegalArgumentException("Enum value of type "+enumClass.getName()+"required, but found: "+val);
  411. }
  412. }
  413. /**
  414. * Add the String hold in param "value" for the key "name"
  415. * @param name : String designating the key in the set
  416. * @param value : String corresponding to the value associated with the key
  417. */
  418. public void set(String name, String value)
  419. {
  420. _set.put(name, value);
  421. }
  422. /**
  423. * Add the boolean hold in param "value" for the key "name"
  424. * @param name : String designating the key in the set
  425. * @param value : boolean corresponding to the value associated with the key
  426. */
  427. public void set(String name, boolean value)
  428. {
  429. _set.put(name, value);
  430. }
  431. /**
  432. * Add the int hold in param "value" for the key "name"
  433. * @param name : String designating the key in the set
  434. * @param value : int corresponding to the value associated with the key
  435. */
  436. public void set(String name, int value)
  437. {
  438. _set.put(name, value);
  439. }
  440. /**
  441. * Safe version of "set". Expected values are within [min, max[<br>
  442. * Add the int hold in param "value" for the key "name".
  443. *
  444. * @param name : String designating the key in the set
  445. * @param value : int corresponding to the value associated with the key
  446. * @param min
  447. * @param max
  448. * @param reference
  449. */
  450. public void safeSet(String name, int value, int min, int max, String reference)
  451. {
  452. assert !((min <= max && (value < min || value >= max)));
  453. if (min <= max && (value < min || value >= max))
  454. {
  455. _log.log(Level.SEVERE, "Incorrect value: "+value+"for: "+name+ "Ref: "+ reference);
  456. }
  457. set(name, value);
  458. }
  459. /**
  460. * Add the double hold in param "value" for the key "name"
  461. * @param name : String designating the key in the set
  462. * @param value : double corresponding to the value associated with the key
  463. */
  464. public void set(String name, double value)
  465. {
  466. _set.put(name, value);
  467. }
  468. /**
  469. * Add the long hold in param "value" for the key "name"
  470. * @param name : String designating the key in the set
  471. * @param value : double corresponding to the value associated with the key
  472. */
  473. public void set(String name, long value)
  474. {
  475. _set.put(name, value);
  476. }
  477. /**
  478. * Add the Enum hold in param "value" for the key "name"
  479. * @param name : String designating the key in the set
  480. * @param value : Enum corresponding to the value associated with the key
  481. */
  482. public void set(String name, Enum<?> value)
  483. {
  484. _set.put(name, value);
  485. }
  486. }