StatsSet.java 17 KB

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