You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TypedProperties.java 12KB

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