StatsSet.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastMap;
  27. import com.l2jserver.gameserver.model.interfaces.IParserAdvUtils;
  28. /**
  29. * This class is meant to hold a set of (key,value) pairs.<br>
  30. * They are stored as object but can be retrieved in any type wanted. As long as cast is available.<br>
  31. * @author mkizub
  32. */
  33. public class StatsSet implements IParserAdvUtils
  34. {
  35. private static final Logger _log = Logger.getLogger(StatsSet.class.getName());
  36. private final Map<String, Object> _set;
  37. public StatsSet()
  38. {
  39. this(new FastMap<String, Object>());
  40. }
  41. public StatsSet(Map<String, Object> map)
  42. {
  43. _set = map;
  44. }
  45. /**
  46. * Returns the set of values
  47. * @return HashMap
  48. */
  49. public final Map<String, Object> getSet()
  50. {
  51. return _set;
  52. }
  53. /**
  54. * Add a set of couple values in the current set
  55. * @param newSet : StatsSet pointing out the list of couples to add in the current set
  56. */
  57. public void add(StatsSet newSet)
  58. {
  59. Map<String, Object> newMap = newSet.getSet();
  60. for (Entry<String, Object> entry : newMap.entrySet())
  61. {
  62. _set.put(entry.getKey(), entry.getValue());
  63. }
  64. }
  65. /**
  66. * Return the boolean value associated with key.
  67. * @param key : String designating the key in the set
  68. * @return boolean : value associated to the key
  69. * @throws IllegalArgumentException : If value is not set or value is not boolean
  70. */
  71. @Override
  72. public boolean getBoolean(String key)
  73. {
  74. Object val = _set.get(key);
  75. if (val == null)
  76. {
  77. throw new IllegalArgumentException("Boolean value required, but not specified");
  78. }
  79. if (val instanceof Boolean)
  80. {
  81. return ((Boolean) val).booleanValue();
  82. }
  83. try
  84. {
  85. return Boolean.parseBoolean((String) val);
  86. }
  87. catch (Exception e)
  88. {
  89. throw new IllegalArgumentException("Boolean value required, but found: " + val);
  90. }
  91. }
  92. /**
  93. * Return the boolean value associated with key.<br>
  94. * If no value is associated with key, or type of value is wrong, returns defaultValue.
  95. * @param key : String designating the key in the entry set
  96. * @return boolean : value associated to the key
  97. */
  98. @Override
  99. public boolean getBoolean(String key, boolean defaultValue)
  100. {
  101. Object val = _set.get(key);
  102. if (val == null)
  103. {
  104. return defaultValue;
  105. }
  106. if (val instanceof Boolean)
  107. {
  108. return ((Boolean) val).booleanValue();
  109. }
  110. try
  111. {
  112. return Boolean.parseBoolean((String) val);
  113. }
  114. catch (Exception e)
  115. {
  116. return defaultValue;
  117. }
  118. }
  119. @Override
  120. public byte getByte(String key)
  121. {
  122. Object val = _set.get(key);
  123. if (val == null)
  124. {
  125. throw new IllegalArgumentException("Byte value required, but not specified");
  126. }
  127. if (val instanceof Number)
  128. {
  129. return ((Number) val).byteValue();
  130. }
  131. try
  132. {
  133. return Byte.parseByte((String) val);
  134. }
  135. catch (Exception e)
  136. {
  137. throw new IllegalArgumentException("Byte value required, but found: " + val);
  138. }
  139. }
  140. @Override
  141. public byte getByte(String key, byte defaultValue)
  142. {
  143. Object val = _set.get(key);
  144. if (val == null)
  145. {
  146. return defaultValue;
  147. }
  148. if (val instanceof Number)
  149. {
  150. return ((Number) val).byteValue();
  151. }
  152. try
  153. {
  154. return Byte.parseByte((String) val);
  155. }
  156. catch (Exception e)
  157. {
  158. throw new IllegalArgumentException("Byte value required, but found: " + val);
  159. }
  160. }
  161. public byte[] getByteArray(String key, String splitOn)
  162. {
  163. Object val = _set.get(key);
  164. if (val == null)
  165. {
  166. throw new IllegalArgumentException("Byte value required, but not specified");
  167. }
  168. if (val instanceof Number)
  169. {
  170. byte[] result =
  171. {
  172. ((Number) val).byteValue()
  173. };
  174. return result;
  175. }
  176. int c = 0;
  177. String[] vals = ((String) val).split(splitOn);
  178. byte[] result = new byte[vals.length];
  179. for (String v : vals)
  180. {
  181. try
  182. {
  183. result[c++] = Byte.parseByte(v);
  184. }
  185. catch (Exception e)
  186. {
  187. throw new IllegalArgumentException("Byte value required, but found: " + val);
  188. }
  189. }
  190. return result;
  191. }
  192. public List<Byte> getByteList(String key, String splitOn)
  193. {
  194. List<Byte> result = new ArrayList<>();
  195. for (Byte i : getByteArray(key, splitOn))
  196. {
  197. result.add(i);
  198. }
  199. return result;
  200. }
  201. @Override
  202. public short getShort(String key)
  203. {
  204. Object val = _set.get(key);
  205. if (val == null)
  206. {
  207. throw new IllegalArgumentException("Short value required, but not specified");
  208. }
  209. if (val instanceof Number)
  210. {
  211. return ((Number) val).shortValue();
  212. }
  213. try
  214. {
  215. return Short.parseShort((String) val);
  216. }
  217. catch (Exception e)
  218. {
  219. throw new IllegalArgumentException("Short value required, but found: " + val);
  220. }
  221. }
  222. @Override
  223. public short getShort(String key, short defaultValue)
  224. {
  225. Object val = _set.get(key);
  226. if (val == null)
  227. {
  228. return defaultValue;
  229. }
  230. if (val instanceof Number)
  231. {
  232. return ((Number) val).shortValue();
  233. }
  234. try
  235. {
  236. return Short.parseShort((String) val);
  237. }
  238. catch (Exception e)
  239. {
  240. throw new IllegalArgumentException("Short value required, but found: " + val);
  241. }
  242. }
  243. @Override
  244. public int getInt(String key)
  245. {
  246. final Object val = _set.get(key);
  247. if (val == null)
  248. {
  249. throw new IllegalArgumentException("Integer value required, but not specified: " + key + "!");
  250. }
  251. if (val instanceof Number)
  252. {
  253. return ((Number) val).intValue();
  254. }
  255. try
  256. {
  257. return Integer.parseInt((String) val);
  258. }
  259. catch (Exception e)
  260. {
  261. throw new IllegalArgumentException("Integer value required, but found: " + val + "!");
  262. }
  263. }
  264. @Override
  265. public int getInt(String key, int defaultValue)
  266. {
  267. Object val = _set.get(key);
  268. if (val == null)
  269. {
  270. return defaultValue;
  271. }
  272. if (val instanceof Number)
  273. {
  274. return ((Number) val).intValue();
  275. }
  276. try
  277. {
  278. return Integer.parseInt((String) val);
  279. }
  280. catch (Exception e)
  281. {
  282. throw new IllegalArgumentException("Integer value required, but found: " + val);
  283. }
  284. }
  285. public int[] getIntArray(String key, String splitOn)
  286. {
  287. Object val = _set.get(key);
  288. if (val == null)
  289. {
  290. throw new IllegalArgumentException("Integer value required, but not specified");
  291. }
  292. if (val instanceof Number)
  293. {
  294. int[] result =
  295. {
  296. ((Number) val).intValue()
  297. };
  298. return result;
  299. }
  300. int c = 0;
  301. String[] vals = ((String) val).split(splitOn);
  302. int[] result = new int[vals.length];
  303. for (String v : vals)
  304. {
  305. try
  306. {
  307. result[c++] = Integer.parseInt(v);
  308. }
  309. catch (Exception e)
  310. {
  311. throw new IllegalArgumentException("Integer value required, but found: " + val);
  312. }
  313. }
  314. return result;
  315. }
  316. public List<Integer> getIntegerList(String key, String splitOn)
  317. {
  318. List<Integer> result = new ArrayList<>();
  319. for (int i : getIntArray(key, splitOn))
  320. {
  321. result.add(i);
  322. }
  323. return result;
  324. }
  325. @Override
  326. public long getLong(String key)
  327. {
  328. Object val = _set.get(key);
  329. if (val == null)
  330. {
  331. throw new IllegalArgumentException("Integer value required, but not specified");
  332. }
  333. if (val instanceof Number)
  334. {
  335. return ((Number) val).longValue();
  336. }
  337. try
  338. {
  339. return Long.parseLong((String) val);
  340. }
  341. catch (Exception e)
  342. {
  343. throw new IllegalArgumentException("Integer value required, but found: " + val);
  344. }
  345. }
  346. @Override
  347. public long getLong(String key, long defaultValue)
  348. {
  349. Object val = _set.get(key);
  350. if (val == null)
  351. {
  352. return defaultValue;
  353. }
  354. if (val instanceof Number)
  355. {
  356. return ((Number) val).longValue();
  357. }
  358. try
  359. {
  360. return Long.parseLong((String) val);
  361. }
  362. catch (Exception e)
  363. {
  364. throw new IllegalArgumentException("Integer value required, but found: " + val);
  365. }
  366. }
  367. @Override
  368. public float getFloat(String key)
  369. {
  370. Object val = _set.get(key);
  371. if (val == null)
  372. {
  373. throw new IllegalArgumentException("Float value required, but not specified");
  374. }
  375. if (val instanceof Number)
  376. {
  377. return ((Number) val).floatValue();
  378. }
  379. try
  380. {
  381. return (float) Double.parseDouble((String) val);
  382. }
  383. catch (Exception e)
  384. {
  385. throw new IllegalArgumentException("Float value required, but found: " + val);
  386. }
  387. }
  388. @Override
  389. public float getFloat(String key, float defaultValue)
  390. {
  391. Object val = _set.get(key);
  392. if (val == null)
  393. {
  394. return defaultValue;
  395. }
  396. if (val instanceof Number)
  397. {
  398. return ((Number) val).floatValue();
  399. }
  400. try
  401. {
  402. return (float) Double.parseDouble((String) val);
  403. }
  404. catch (Exception e)
  405. {
  406. throw new IllegalArgumentException("Float value required, but found: " + val);
  407. }
  408. }
  409. @Override
  410. public double getDouble(String key)
  411. {
  412. Object val = _set.get(key);
  413. if (val == null)
  414. {
  415. throw new IllegalArgumentException("Float value required, but not specified");
  416. }
  417. if (val instanceof Number)
  418. {
  419. return ((Number) val).doubleValue();
  420. }
  421. try
  422. {
  423. return Double.parseDouble((String) val);
  424. }
  425. catch (Exception e)
  426. {
  427. throw new IllegalArgumentException("Float value required, but found: " + val);
  428. }
  429. }
  430. @Override
  431. public double getDouble(String key, double defaultValue)
  432. {
  433. Object val = _set.get(key);
  434. if (val == null)
  435. {
  436. return defaultValue;
  437. }
  438. if (val instanceof Number)
  439. {
  440. return ((Number) val).doubleValue();
  441. }
  442. try
  443. {
  444. return Double.parseDouble((String) val);
  445. }
  446. catch (Exception e)
  447. {
  448. throw new IllegalArgumentException("Float value required, but found: " + val);
  449. }
  450. }
  451. @Override
  452. public String getString(String key)
  453. {
  454. Object val = _set.get(key);
  455. if (val == null)
  456. {
  457. throw new IllegalArgumentException("String value required, but not specified");
  458. }
  459. return String.valueOf(val);
  460. }
  461. @Override
  462. public String getString(String key, String defaultValue)
  463. {
  464. Object val = _set.get(key);
  465. if (val == null)
  466. {
  467. return defaultValue;
  468. }
  469. return String.valueOf(val);
  470. }
  471. @Override
  472. @SuppressWarnings("unchecked")
  473. public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass)
  474. {
  475. Object val = _set.get(key);
  476. if (val == null)
  477. {
  478. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but not specified");
  479. }
  480. if (enumClass.isInstance(val))
  481. {
  482. return (T) val;
  483. }
  484. try
  485. {
  486. return Enum.valueOf(enumClass, String.valueOf(val));
  487. }
  488. catch (Exception e)
  489. {
  490. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
  491. }
  492. }
  493. @Override
  494. @SuppressWarnings("unchecked")
  495. public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass, T defaultValue)
  496. {
  497. Object val = _set.get(key);
  498. if (val == null)
  499. {
  500. return defaultValue;
  501. }
  502. if (enumClass.isInstance(val))
  503. {
  504. return (T) val;
  505. }
  506. try
  507. {
  508. return Enum.valueOf(enumClass, String.valueOf(val));
  509. }
  510. catch (Exception e)
  511. {
  512. throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
  513. }
  514. }
  515. public void set(String name, Object value)
  516. {
  517. _set.put(name, value);
  518. }
  519. public void set(String key, boolean value)
  520. {
  521. _set.put(key, value);
  522. }
  523. public void set(String key, byte value)
  524. {
  525. _set.put(key, value);
  526. }
  527. public void set(String key, short value)
  528. {
  529. _set.put(key, value);
  530. }
  531. public void set(String key, int value)
  532. {
  533. _set.put(key, value);
  534. }
  535. public void set(String key, long value)
  536. {
  537. _set.put(key, value);
  538. }
  539. public void set(String key, float value)
  540. {
  541. _set.put(key, value);
  542. }
  543. public void set(String key, double value)
  544. {
  545. _set.put(key, value);
  546. }
  547. public void set(String key, String value)
  548. {
  549. _set.put(key, value);
  550. }
  551. public void set(String key, Enum<?> value)
  552. {
  553. _set.put(key, value);
  554. }
  555. public void safeSet(String key, int value, int min, int max, String reference)
  556. {
  557. assert !(((min <= max) && ((value < min) || (value >= max))));
  558. if ((min <= max) && ((value < min) || (value >= max)))
  559. {
  560. _log.log(Level.SEVERE, "Incorrect value: " + value + "for: " + key + "Ref: " + reference);
  561. }
  562. set(key, value);
  563. }
  564. @SuppressWarnings("unchecked")
  565. public final <A> A getObject(String name, Class<A> type)
  566. {
  567. Object obj = _set.get(name);
  568. if ((obj == null) || !type.isAssignableFrom(obj.getClass()))
  569. {
  570. return null;
  571. }
  572. return (A) obj;
  573. }
  574. }