StatsSet.java 17 KB

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