StatsSet.java 13 KB

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