StatsSet.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. } catch (Exception e) {
  222. throw new IllegalArgumentException("Integer value required, but found: "+val);
  223. }
  224. }
  225. return result;
  226. }
  227. /**
  228. * Returns the long associated to the key put in parameter ("name").
  229. * @param name : String designating the key in the set
  230. * @return long : value associated to the key
  231. */
  232. public long getLong(String name)
  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. return ((Number)val).longValue();
  239. try {
  240. return Long.parseLong((String)val);
  241. } catch (Exception e) {
  242. throw new IllegalArgumentException("Integer value required, but found: "+val);
  243. }
  244. }
  245. /**
  246. * 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
  247. * deflt.
  248. * @param name : String designating the key in the set
  249. * @param deflt : long designating the default value if value associated with the key is null
  250. * @return long : value associated to the key
  251. */
  252. public long getLong(String name, int deflt)
  253. {
  254. Object val = _set.get(name);
  255. if (val == null)
  256. return deflt;
  257. if (val instanceof Number)
  258. return ((Number)val).longValue();
  259. try {
  260. return Long.parseLong((String)val);
  261. } catch (Exception e) {
  262. throw new IllegalArgumentException("Integer value required, but found: "+val);
  263. }
  264. }
  265. /**
  266. * Returns the float associated to the key put in parameter ("name").
  267. * @param name : String designating the key in the set
  268. * @return float : value associated to the key
  269. */
  270. public float getFloat(String name)
  271. {
  272. Object val = _set.get(name);
  273. if (val == null)
  274. throw new IllegalArgumentException("Float value required, but not specified");
  275. if (val instanceof Number)
  276. return ((Number)val).floatValue();
  277. try {
  278. return (float)Double.parseDouble((String)val);
  279. } catch (Exception e) {
  280. throw new IllegalArgumentException("Float value required, but found: "+val);
  281. }
  282. }
  283. /**
  284. * 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
  285. * deflt.
  286. * @param name : String designating the key in the set
  287. * @param deflt : float designating the default value if value associated with the key is null
  288. * @return float : value associated to the key
  289. */
  290. public float getFloat(String name, float deflt)
  291. {
  292. Object val = _set.get(name);
  293. if (val == null)
  294. return deflt;
  295. if (val instanceof Number)
  296. return ((Number)val).floatValue();
  297. try {
  298. return (float)Double.parseDouble((String)val);
  299. } catch (Exception e) {
  300. throw new IllegalArgumentException("Float value required, but found: "+val);
  301. }
  302. }
  303. /**
  304. * Returns the double associated to the key put in parameter ("name").
  305. * @param name : String designating the key in the set
  306. * @return double : value associated to the key
  307. */
  308. public double getDouble(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).doubleValue();
  315. try {
  316. return Double.parseDouble((String)val);
  317. } catch (Exception e) {
  318. throw new IllegalArgumentException("Float value required, but found: "+val);
  319. }
  320. }
  321. /**
  322. * 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
  323. * deflt.
  324. * @param name : String designating the key in the set
  325. * @param deflt : float designating the default value if value associated with the key is null
  326. * @return double : value associated to the key
  327. */
  328. public double getDouble(String name, float deflt)
  329. {
  330. Object val = _set.get(name);
  331. if (val == null)
  332. return deflt;
  333. if (val instanceof Number)
  334. return ((Number)val).doubleValue();
  335. try {
  336. return Double.parseDouble((String)val);
  337. } catch (Exception e) {
  338. throw new IllegalArgumentException("Float value required, but found: "+val);
  339. }
  340. }
  341. /**
  342. * Returns the String associated to the key put in parameter ("name").
  343. * @param name : String designating the key in the set
  344. * @return String : value associated to the key
  345. */
  346. public String getString(String name)
  347. {
  348. Object val = _set.get(name);
  349. if (val == null)
  350. throw new IllegalArgumentException("String value required, but not specified");
  351. return String.valueOf(val);
  352. }
  353. /**
  354. * 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
  355. * deflt.
  356. * @param name : String designating the key in the set
  357. * @param deflt : String designating the default value if value associated with the key is null
  358. * @return String : value associated to the key
  359. */
  360. public String getString(String name, String deflt)
  361. {
  362. Object val = _set.get(name);
  363. if (val == null)
  364. return deflt;
  365. return String.valueOf(val);
  366. }
  367. /**
  368. * Returns an enumeration of &lt;T&gt; from the set
  369. * @param <T> : Class of the enumeration returned
  370. * @param name : String designating the key in the set
  371. * @param enumClass : Class designating the class of the value associated with the key in the set
  372. * @return Enum<T>
  373. */
  374. @SuppressWarnings("unchecked")
  375. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass)
  376. {
  377. Object val = _set.get(name);
  378. if (val == null)
  379. throw new IllegalArgumentException("Enum value of type "+enumClass.getName()+" required, but not specified");
  380. if (enumClass.isInstance(val))
  381. return (T)val;
  382. try {
  383. return Enum.valueOf(enumClass, String.valueOf(val));
  384. } catch (Exception e) {
  385. throw new IllegalArgumentException("Enum value of type "+enumClass.getName()+"required, but found: "+val);
  386. }
  387. }
  388. /**
  389. * Returns an enumeration of &lt;T&gt; from the set. If the enumeration is empty, the method returns the value of the parameter "deflt".
  390. * @param <T> : Class of the enumeration returned
  391. * @param name : String designating the key in the set
  392. * @param enumClass : Class designating the class of the value associated with the key in the set
  393. * @param deflt : <T> designating the value by default
  394. * @return Enum<T>
  395. */
  396. @SuppressWarnings("unchecked")
  397. public <T extends Enum<T>> T getEnum(String name, Class<T> enumClass, T deflt)
  398. {
  399. Object val = _set.get(name);
  400. if (val == null)
  401. return deflt;
  402. if (enumClass.isInstance(val))
  403. return (T)val;
  404. try {
  405. return Enum.valueOf(enumClass, String.valueOf(val));
  406. } catch (Exception e) {
  407. throw new IllegalArgumentException("Enum value of type "+enumClass.getName()+"required, but found: "+val);
  408. }
  409. }
  410. /**
  411. * Add the String hold in param "value" for the key "name"
  412. * @param name : String designating the key in the set
  413. * @param value : String corresponding to the value associated with the key
  414. */
  415. public void set(String name, String value)
  416. {
  417. _set.put(name, value);
  418. }
  419. /**
  420. * Add the boolean hold in param "value" for the key "name"
  421. * @param name : String designating the key in the set
  422. * @param value : boolean corresponding to the value associated with the key
  423. */
  424. public void set(String name, boolean value)
  425. {
  426. _set.put(name, value);
  427. }
  428. /**
  429. * Add the int hold in param "value" for the key "name"
  430. * @param name : String designating the key in the set
  431. * @param value : int corresponding to the value associated with the key
  432. */
  433. public void set(String name, int value)
  434. {
  435. _set.put(name, value);
  436. }
  437. /**
  438. * Add the double hold in param "value" for the key "name"
  439. * @param name : String designating the key in the set
  440. * @param value : double corresponding to the value associated with the key
  441. */
  442. public void set(String name, double value)
  443. {
  444. _set.put(name, value);
  445. }
  446. /**
  447. * Add the long hold in param "value" for the key "name"
  448. * @param name : String designating the key in the set
  449. * @param value : double corresponding to the value associated with the key
  450. */
  451. public void set(String name, long value)
  452. {
  453. _set.put(name, value);
  454. }
  455. /**
  456. * Add the Enum hold in param "value" for the key "name"
  457. * @param name : String designating the key in the set
  458. * @param value : Enum corresponding to the value associated with the key
  459. */
  460. public void set(String name, Enum<?> value)
  461. {
  462. _set.put(name, value);
  463. }
  464. }