Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TypedProperties.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.scriptplugin;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Properties;
  24. /**
  25. * Properties file that allows for getting/setting of typed properties.
  26. */
  27. public class TypedProperties extends Properties {
  28. /** A version number for this class. */
  29. private static final long serialVersionUID = 200711071;
  30. /** Is this properties file Case Sensitive */
  31. private boolean caseSensitive = true;
  32. /**
  33. * Creates an empty property list with no default values.
  34. */
  35. public TypedProperties() {
  36. }
  37. /**
  38. * Creates an empty property list with the specified defaults.
  39. *
  40. * @param defaults The Defaults
  41. */
  42. public TypedProperties(final Properties defaults) {
  43. super(defaults);
  44. }
  45. /**
  46. * Set case sensitivity of this properties file.
  47. *
  48. * @param value True/False for the case sensitivity of this file
  49. */
  50. public void setCaseSensitivity(final boolean value) {
  51. // Set all existing values to lowercase.
  52. if (!value) {
  53. keySet().stream().filter(property -> property instanceof String).forEach(property -> {
  54. final String propertyName = (String) property;
  55. if (!propertyName.equals(propertyName.toLowerCase())) {
  56. super.setProperty(propertyName.toLowerCase(), getProperty(propertyName));
  57. remove(propertyName);
  58. }
  59. });
  60. }
  61. caseSensitive = value;
  62. }
  63. /**
  64. * Load properties from an InputStream. After loading, setCaseSensitivity(caseSensitive) is
  65. * called. If this properties file is meant to be case Insensitive, all non-lowercase property
  66. * names will be lowercased.
  67. *
  68. * @param inStream InputStream to load from.
  69. *
  70. * @throws IOException If there is a problem reading from the Input Stream
  71. */
  72. @Override
  73. public synchronized void load(final InputStream inStream) throws IOException {
  74. super.load(inStream);
  75. setCaseSensitivity(caseSensitive);
  76. }
  77. /**
  78. * Get a property from the config
  79. *
  80. * @param key key for property
  81. *
  82. * @return the requested property, or null if not defined
  83. */
  84. @Override
  85. public String getProperty(final String key) {
  86. if (caseSensitive) {
  87. return super.getProperty(key);
  88. } else {
  89. return super.getProperty(key.toLowerCase());
  90. }
  91. }
  92. /**
  93. * Get a property from the config
  94. *
  95. * @param key key for property
  96. * @param fallback Value to return if key is not found
  97. *
  98. * @return the requested property, or the fallback value if not defined
  99. */
  100. @Override
  101. public String getProperty(final String key, final String fallback) {
  102. if (caseSensitive) {
  103. return super.getProperty(key, fallback);
  104. } else {
  105. return super.getProperty(key.toLowerCase(), fallback);
  106. }
  107. }
  108. /**
  109. * Set a property in the config
  110. *
  111. * @param key key for property
  112. * @param value Value for property
  113. *
  114. * @return Old value of property
  115. */
  116. @Override
  117. public synchronized Object setProperty(final String key, final String value) {
  118. if (!caseSensitive) {
  119. return super.setProperty(key.toLowerCase(), value);
  120. } else {
  121. return super.setProperty(key, value);
  122. }
  123. }
  124. /**
  125. * Check if a property exists
  126. *
  127. * @param key key for property
  128. *
  129. * @return True if the property exists, else false
  130. */
  131. public boolean hasProperty(final String key) {
  132. return getProperty(key) != null;
  133. }
  134. /**
  135. * Get a Byte property from the config
  136. *
  137. * @param key key for property
  138. * @param fallback Value to return if key is not found
  139. *
  140. * @return the requested property, or the fallback value if not defined
  141. */
  142. public byte getByteProperty(final String key, final byte fallback) {
  143. try {
  144. return Byte.parseByte(getProperty(key, Byte.toString(fallback)));
  145. } catch (final NumberFormatException nfe) {
  146. return fallback;
  147. }
  148. }
  149. /**
  150. * Set a Byte property in the config
  151. *
  152. * @param key key for property
  153. * @param value Value for property
  154. */
  155. public void setByteProperty(final String key, final byte value) {
  156. setProperty(key, Byte.toString(value));
  157. }
  158. /**
  159. * Get a Short property from the config
  160. *
  161. * @param key key for property
  162. * @param fallback Value to return if key is not found
  163. *
  164. * @return the requested property, or the fallback value if not defined
  165. */
  166. public short getShortProperty(final String key, final short fallback) {
  167. try {
  168. return Short.parseShort(getProperty(key, Short.toString(fallback)));
  169. } catch (final NumberFormatException nfe) {
  170. return fallback;
  171. }
  172. }
  173. /**
  174. * Set a Short property in the config
  175. *
  176. * @param key key for property
  177. * @param value Value for property
  178. */
  179. public void setShortProperty(final String key, final short value) {
  180. setProperty(key, Short.toString(value));
  181. }
  182. /**
  183. * Get an integer property from the config
  184. *
  185. * @param key key for property
  186. * @param fallback Value to return if key is not found
  187. *
  188. * @return the requested property, or the fallback value if not defined
  189. */
  190. public int getIntProperty(final String key, final int fallback) {
  191. try {
  192. return Integer.parseInt(getProperty(key, Integer.toString(fallback)));
  193. } catch (final NumberFormatException nfe) {
  194. return fallback;
  195. }
  196. }
  197. /**
  198. * Set an integer property in the config
  199. *
  200. * @param key key for property
  201. * @param value Value for property
  202. */
  203. public void setIntProperty(final String key, final int value) {
  204. setProperty(key, Integer.toString(value));
  205. }
  206. /**
  207. * Get a Long property from the config
  208. *
  209. * @param key key for property
  210. * @param fallback Value to return if key is not found
  211. *
  212. * @return the requested property, or the fallback value if not defined
  213. */
  214. public long getLongProperty(final String key, final long fallback) {
  215. try {
  216. return Long.parseLong(getProperty(key, Long.toString(fallback)));
  217. } catch (final NumberFormatException nfe) {
  218. return fallback;
  219. }
  220. }
  221. /**
  222. * Set a Long property in the config
  223. *
  224. * @param key key for property
  225. * @param value Value for property
  226. */
  227. public void setLongProperty(final String key, final long value) {
  228. setProperty(key, Long.toString(value));
  229. }
  230. /**
  231. * Get a float property from the config
  232. *
  233. * @param key key for property
  234. * @param fallback Value to return if key is not found
  235. *
  236. * @return the requested property, or the fallback value if not defined
  237. */
  238. public float getFloatProperty(final String key, final float fallback) {
  239. try {
  240. return Float.parseFloat(getProperty(key, Float.toString(fallback)));
  241. } catch (final NumberFormatException nfe) {
  242. return fallback;
  243. }
  244. }
  245. /**
  246. * Set a float property in the config
  247. *
  248. * @param key key for property
  249. * @param value Value for property
  250. */
  251. public void setFloatProperty(final String key, final float value) {
  252. setProperty(key, Float.toString(value));
  253. }
  254. /**
  255. * Get a double property from the config
  256. *
  257. * @param key key for property
  258. * @param fallback Value to return if key is not found
  259. *
  260. * @return the requested property, or the fallback value if not defined
  261. */
  262. public double getDoubleProperty(final String key, final double fallback) {
  263. try {
  264. return Double.parseDouble(getProperty(key, Double.toString(fallback)));
  265. } catch (final NumberFormatException nfe) {
  266. return fallback;
  267. }
  268. }
  269. /**
  270. * Set a double property in the config
  271. *
  272. * @param key key for property
  273. * @param value Value for property
  274. */
  275. public void setDoubleProperty(final String key, final double value) {
  276. setProperty(key, Double.toString(value));
  277. }
  278. /**
  279. * Get a boolean property from the config
  280. *
  281. * @param key key for property
  282. * @param fallback Value to return if key is not found
  283. *
  284. * @return the requested property, or the fallback value if not defined
  285. */
  286. public boolean getBoolProperty(final String key, final boolean fallback) {
  287. return Boolean.parseBoolean(getProperty(key, Boolean.toString(fallback)));
  288. }
  289. /**
  290. * Set a Boolean property in the config
  291. *
  292. * @param key key for property
  293. * @param value Value for property
  294. */
  295. public void setBoolProperty(final String key, final boolean value) {
  296. setProperty(key, Boolean.toString(value));
  297. }
  298. /**
  299. * Get a Char property from the config
  300. *
  301. * @param key key for property
  302. * @param fallback Value to return if key is not found
  303. *
  304. * @return the requested property, or the fallback value if not defined
  305. */
  306. public char getCharProperty(final String key, final char fallback) {
  307. final String res = getProperty(key, Character.toString(fallback));
  308. if (res == null || res.isEmpty()) {
  309. return fallback;
  310. } else {
  311. return res.charAt(0);
  312. }
  313. }
  314. /**
  315. * Set a Char property in the config
  316. *
  317. * @param key key for property
  318. * @param value Value for property
  319. */
  320. public void setCharProperty(final String key, final char value) {
  321. setProperty(key, Character.toString(value));
  322. }
  323. /**
  324. * Get a List property from the config.
  325. *
  326. * @param key key for property
  327. * @param fallback List to return if key is not found
  328. *
  329. * @return the requested property, or the fallback value if not defined
  330. */
  331. public List<String> getListProperty(final String key, final List<String> fallback) {
  332. final String res = getProperty(key, "");
  333. if (res == null || res.isEmpty()) {
  334. return fallback;
  335. } else {
  336. final String[] bits = res.split("\n");
  337. final List<String> result = new ArrayList<>();
  338. Collections.addAll(result, bits);
  339. return result;
  340. }
  341. }
  342. /**
  343. * Set a List property in the config
  344. *
  345. * @param key key for property
  346. * @param value Value for property
  347. */
  348. public void setListProperty(final String key, final Iterable<String> value) {
  349. final StringBuilder val = new StringBuilder();
  350. final String lf = "\n";
  351. boolean first = true;
  352. for (final String bit : value) {
  353. if (first) {
  354. first = false;
  355. } else {
  356. val.append(lf);
  357. }
  358. val.append(bit);
  359. }
  360. setProperty(key, val.toString());
  361. }
  362. }