StatsSet.java 14 KB

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