123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653 |
- package com.l2jserver.gameserver.model;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastMap;
- import com.l2jserver.gameserver.model.holders.MinionHolder;
- import com.l2jserver.gameserver.model.holders.SkillHolder;
- import com.l2jserver.gameserver.model.interfaces.IParserAdvUtils;
- public class StatsSet implements IParserAdvUtils
- {
- private static final Logger _log = Logger.getLogger(StatsSet.class.getName());
-
- public static final StatsSet EMPTY_STATSET = new StatsSet(Collections.<String, Object> emptyMap());
-
- private final Map<String, Object> _set;
-
- public StatsSet()
- {
- this(new FastMap<String, Object>());
- }
-
- public StatsSet(Map<String, Object> map)
- {
- _set = map;
- }
-
-
- public final Map<String, Object> getSet()
- {
- return _set;
- }
-
-
- public void add(StatsSet newSet)
- {
- _set.putAll(newSet.getSet());
- }
-
-
- public boolean isEmpty()
- {
- return _set.isEmpty();
- }
-
-
- @Override
- public boolean getBoolean(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Boolean value required, but not specified");
- }
- if (val instanceof Boolean)
- {
- return ((Boolean) val).booleanValue();
- }
- try
- {
- return Boolean.parseBoolean((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Boolean value required, but found: " + val);
- }
- }
-
-
- @Override
- public boolean getBoolean(String key, boolean defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Boolean)
- {
- return ((Boolean) val).booleanValue();
- }
- try
- {
- return Boolean.parseBoolean((String) val);
- }
- catch (Exception e)
- {
- return defaultValue;
- }
- }
-
- @Override
- public byte getByte(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Byte value required, but not specified");
- }
- if (val instanceof Number)
- {
- return ((Number) val).byteValue();
- }
- try
- {
- return Byte.parseByte((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Byte value required, but found: " + val);
- }
- }
-
- @Override
- public byte getByte(String key, byte defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Number)
- {
- return ((Number) val).byteValue();
- }
- try
- {
- return Byte.parseByte((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Byte value required, but found: " + val);
- }
- }
-
- public byte[] getByteArray(String key, String splitOn)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Byte value required, but not specified");
- }
- if (val instanceof Number)
- {
- byte[] result =
- {
- ((Number) val).byteValue()
- };
- return result;
- }
- int c = 0;
- String[] vals = ((String) val).split(splitOn);
- byte[] result = new byte[vals.length];
- for (String v : vals)
- {
- try
- {
- result[c++] = Byte.parseByte(v);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Byte value required, but found: " + val);
- }
- }
- return result;
- }
-
- public List<Byte> getByteList(String key, String splitOn)
- {
- List<Byte> result = new ArrayList<>();
- for (Byte i : getByteArray(key, splitOn))
- {
- result.add(i);
- }
- return result;
- }
-
- @Override
- public short getShort(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Short value required, but not specified");
- }
- if (val instanceof Number)
- {
- return ((Number) val).shortValue();
- }
- try
- {
- return Short.parseShort((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Short value required, but found: " + val);
- }
- }
-
- @Override
- public short getShort(String key, short defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Number)
- {
- return ((Number) val).shortValue();
- }
- try
- {
- return Short.parseShort((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Short value required, but found: " + val);
- }
- }
-
- @Override
- public int getInt(String key)
- {
- final Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Integer value required, but not specified: " + key + "!");
- }
-
- if (val instanceof Number)
- {
- return ((Number) val).intValue();
- }
-
- try
- {
- return Integer.parseInt((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Integer value required, but found: " + val + "!");
- }
- }
-
- @Override
- public int getInt(String key, int defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Number)
- {
- return ((Number) val).intValue();
- }
- try
- {
- return Integer.parseInt((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Integer value required, but found: " + val);
- }
- }
-
- public int[] getIntArray(String key, String splitOn)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Integer value required, but not specified");
- }
- if (val instanceof Number)
- {
- int[] result =
- {
- ((Number) val).intValue()
- };
- return result;
- }
- int c = 0;
- String[] vals = ((String) val).split(splitOn);
- int[] result = new int[vals.length];
- for (String v : vals)
- {
- try
- {
- result[c++] = Integer.parseInt(v);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Integer value required, but found: " + val);
- }
- }
- return result;
- }
-
- public List<Integer> getIntegerList(String key, String splitOn)
- {
- List<Integer> result = new ArrayList<>();
- for (int i : getIntArray(key, splitOn))
- {
- result.add(i);
- }
- return result;
- }
-
- @Override
- public long getLong(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Integer value required, but not specified");
- }
- if (val instanceof Number)
- {
- return ((Number) val).longValue();
- }
- try
- {
- return Long.parseLong((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Integer value required, but found: " + val);
- }
- }
-
- @Override
- public long getLong(String key, long defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Number)
- {
- return ((Number) val).longValue();
- }
- try
- {
- return Long.parseLong((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Integer value required, but found: " + val);
- }
- }
-
- @Override
- public float getFloat(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Float value required, but not specified");
- }
- if (val instanceof Number)
- {
- return ((Number) val).floatValue();
- }
- try
- {
- return (float) Double.parseDouble((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Float value required, but found: " + val);
- }
- }
-
- @Override
- public float getFloat(String key, float defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Number)
- {
- return ((Number) val).floatValue();
- }
- try
- {
- return (float) Double.parseDouble((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Float value required, but found: " + val);
- }
- }
-
- @Override
- public double getDouble(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Float value required, but not specified");
- }
- if (val instanceof Number)
- {
- return ((Number) val).doubleValue();
- }
- try
- {
- return Double.parseDouble((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Float value required, but found: " + val);
- }
- }
-
- @Override
- public double getDouble(String key, double defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (val instanceof Number)
- {
- return ((Number) val).doubleValue();
- }
- try
- {
- return Double.parseDouble((String) val);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Float value required, but found: " + val);
- }
- }
-
- @Override
- public String getString(String key)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("String value required, but not specified");
- }
- return String.valueOf(val);
- }
-
- @Override
- public String getString(String key, String defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- return String.valueOf(val);
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but not specified");
- }
- if (enumClass.isInstance(val))
- {
- return (T) val;
- }
- try
- {
- return Enum.valueOf(enumClass, String.valueOf(val));
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
- }
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T extends Enum<T>> T getEnum(String key, Class<T> enumClass, T defaultValue)
- {
- Object val = _set.get(key);
- if (val == null)
- {
- return defaultValue;
- }
- if (enumClass.isInstance(val))
- {
- return (T) val;
- }
- try
- {
- return Enum.valueOf(enumClass, String.valueOf(val));
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Enum value of type " + enumClass.getName() + " required, but found: " + val);
- }
- }
-
- @SuppressWarnings("unchecked")
- public final <A> A getObject(String name, Class<A> type)
- {
- Object obj = _set.get(name);
- if ((obj == null) || !type.isAssignableFrom(obj.getClass()))
- {
- return null;
- }
-
- return (A) obj;
- }
-
- public SkillHolder getSkillHolder(String key)
- {
- Object obj = _set.get(key);
- if ((obj == null) || !(obj instanceof SkillHolder))
- {
- return null;
- }
-
- return (SkillHolder) obj;
- }
-
- @SuppressWarnings("unchecked")
- public List<MinionHolder> getMinionList(String key)
- {
- Object obj = _set.get(key);
- if ((obj == null) || !(obj instanceof List<?>))
- {
- return Collections.EMPTY_LIST;
- }
-
- return (List<MinionHolder>) obj;
- }
-
- public void set(String name, Object value)
- {
- _set.put(name, value);
- }
-
- public void set(String key, boolean value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, byte value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, short value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, int value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, long value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, float value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, double value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, String value)
- {
- _set.put(key, value);
- }
-
- public void set(String key, Enum<?> value)
- {
- _set.put(key, value);
- }
-
- public void safeSet(String key, int value, int min, int max, String reference)
- {
- assert !(((min <= max) && ((value < min) || (value >= max))));
- if ((min <= max) && ((value < min) || (value >= max)))
- {
- _log.log(Level.SEVERE, "Incorrect value: " + value + "for: " + key + "Ref: " + reference);
- }
-
- set(key, value);
- }
- }
|