Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ReadOnlyConfigProvider.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.config.provider;
  18. import com.dmdirc.util.validators.DisabledOptionValidator;
  19. import com.dmdirc.util.validators.NumericalValidator;
  20. import com.dmdirc.util.validators.OptionalValidator;
  21. import com.dmdirc.util.validators.PermissiveValidator;
  22. import com.dmdirc.util.validators.Validator;
  23. import com.dmdirc.util.validators.ValidatorChain;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * A source of configuration settings that can only be read from.
  30. *
  31. * <p>
  32. * This implementation supports the idea of optional/disabled settings. This is where values may be
  33. * prefixed with the strings <code>true:</code> or <code>false:</code>, where the former has no
  34. * effect on the value of the setting, and the latter indicates that the user wishes to disable the
  35. * setting.
  36. *
  37. * <p>
  38. * Disabled settings allow for optional settings to have default values; a value can be added with
  39. * the <code>false:</code> prefix which effectively disables the default value and makes the setting
  40. * act as though it does not have a value.
  41. */
  42. public interface ReadOnlyConfigProvider {
  43. /** A permissive validator to use when callers don't specify one. */
  44. Validator<String> PERMISSIVE_VALIDATOR = new PermissiveValidator<>();
  45. /** A validator for integer settings. */
  46. Validator<String> INT_VALIDATOR = new OptionalValidator(new NumericalValidator(-1, -1));
  47. /**
  48. * Retrieves the specified option from this config source.
  49. *
  50. * @param domain The domain of the option
  51. * @param option The name of the option
  52. *
  53. * @return The value of the option, or null if it doesn't exist
  54. */
  55. default String getOption(final String domain, final String option) {
  56. return getOption(domain, option, PERMISSIVE_VALIDATOR);
  57. }
  58. /**
  59. * Retrieves the first value for the specified option that matches the specified validator.
  60. *
  61. * @param domain The domain of the option
  62. * @param option The name of the option
  63. * @param validator The validator to use to check legal values
  64. *
  65. * @return The value of the option, or null if no matching values exist
  66. */
  67. String getOption(String domain, String option, Validator<String> validator);
  68. /**
  69. * Determines if this source has a value for the specified option which matches the specified
  70. * validator.
  71. *
  72. * @param domain The domain of the option
  73. * @param option The name of the option
  74. * @param validator The validator to use to check legal values
  75. *
  76. * @return True iff a matching option exists, false otherwise.
  77. */
  78. boolean hasOption(String domain, String option, Validator<String> validator);
  79. /**
  80. * Retrieves a boolean representation of the specified option.
  81. *
  82. * @param domain The domain of the option
  83. * @param option The name of the option
  84. *
  85. * @return The boolean representation of the option
  86. */
  87. default boolean getOptionBool(final String domain, final String option) {
  88. return Boolean.parseBoolean(getOption(domain, option));
  89. }
  90. /**
  91. * Retrieves the specified option as a character.
  92. *
  93. * @param domain The domain of the option
  94. * @param option The name of the option
  95. *
  96. * @return The value of the option
  97. */
  98. default char getOptionChar(final String domain, final String option) {
  99. return getOption(domain, option).charAt(0);
  100. }
  101. /**
  102. * Retrieves an integral representation of the specified option.
  103. *
  104. * @param domain The domain of the option
  105. * @param option The name of the option
  106. * @param fallbacks An ordered array of further domains and options (in pairs) to try if the
  107. * specified domain/option isn't found
  108. *
  109. * @return The integer representation of the option
  110. */
  111. default Integer getOptionInt(final String domain, final String option,
  112. final String... fallbacks) {
  113. return getOptionInt(domain, option, true, fallbacks);
  114. }
  115. /**
  116. * Retrieves an integral representation of the specified option.
  117. *
  118. * @param domain The domain of the option
  119. * @param option The name of the option
  120. * @param required Whether this option is required or optional
  121. * @param fallbacks An ordered array of further domains and options (in pairs) to try if the
  122. * specified domain/option isn't found
  123. *
  124. * @return The integer representation of the option
  125. *
  126. * @since 0.6.5
  127. */
  128. default Integer getOptionInt(final String domain, final String option, final boolean required,
  129. final String... fallbacks) {
  130. final String value = getOptionString(domain, option, required, INT_VALIDATOR, fallbacks);
  131. return value == null ? null : Integer.parseInt(value.trim());
  132. }
  133. /**
  134. * Retrieves a list representation of the specified option.
  135. *
  136. * @param domain The domain of the option
  137. * @param option The name of the option
  138. * @param trimEmpty Whether or not to trim empty lines
  139. *
  140. * @return The list representation of the option
  141. */
  142. default List<String> getOptionList(final String domain, final String option,
  143. final boolean trimEmpty) {
  144. final List<String> res = new ArrayList<>();
  145. if (hasOption(domain, option, PERMISSIVE_VALIDATOR)) {
  146. for (String line : getOption(domain, option).split("\n")) {
  147. if (!line.isEmpty() || !trimEmpty) {
  148. res.add(line);
  149. }
  150. }
  151. }
  152. return res;
  153. }
  154. /**
  155. * Retrieves a list representation of the specified option, trimming empty lines by default.
  156. *
  157. * @param domain The domain of the option
  158. * @param option The name of the option
  159. *
  160. * @return The list representation of the option
  161. */
  162. default List<String> getOptionList(final String domain, final String option) {
  163. return getOptionList(domain, option, true);
  164. }
  165. /**
  166. * Retrieves the specified option as a String, if it exists and satisfies the specified
  167. * validator.
  168. * <p>
  169. * If no fallback settings are supplied (or if fallback settings are supplied and execution
  170. * reaches the last fallback setting) AND if the 'required' parameter is true, then all disabled
  171. * values (i.e., those starting with <code>false:</code>) will be ignored. To check if a valid
  172. * non-disabled value exists, call {@link #hasOptionString(String, String, Validator)}.
  173. *
  174. * @param domain The domain of the option
  175. * @param option The name of the option
  176. * @param required Whether the specified option is required or not
  177. * @param validator The validator to use to check the value
  178. * @param fallbacks An ordered array of further domains and options (in pairs) to try if the
  179. * specified domain/option isn't found
  180. *
  181. * @return The string representation of the option or null if optional setting is not specified
  182. */
  183. default String getOptionString(final String domain, final String option, final boolean required,
  184. final Validator<String> validator, final String... fallbacks) {
  185. final String value;
  186. final Validator<String> newValidator = required && fallbacks.length == 0
  187. ? ValidatorChain.<String>builder().addValidator(
  188. new DisabledOptionValidator()).addValidator(validator).build()
  189. : validator;
  190. if (!hasOption(domain, option, newValidator)
  191. || (value = getOption(domain, option, newValidator)).startsWith("false:")) {
  192. return fallbacks.length >= 2 ? getOptionString(fallbacks[0],
  193. fallbacks[1], required, validator,
  194. Arrays.copyOfRange(fallbacks, 2, fallbacks.length)) : null;
  195. } else if (value.startsWith("true:")) {
  196. return value.substring(5);
  197. } else {
  198. return value;
  199. }
  200. }
  201. /**
  202. * Retrieves the specified option as a String, if it exists.
  203. *
  204. * @param domain The domain of the option
  205. * @param option The name of the option
  206. * @param fallbacks An ordered array of further domains and options (in pairs) to try if the
  207. * specified domain/option isn't found
  208. *
  209. * @return The string representation of the option or null if optional setting is not specified
  210. */
  211. default String getOptionString(final String domain, final String option,
  212. final String... fallbacks) {
  213. return getOptionString(domain, option, true, PERMISSIVE_VALIDATOR, fallbacks);
  214. }
  215. /**
  216. * Retrieves a boolean representation of the specified option.
  217. *
  218. * @param domain The domain of the option
  219. * @param option The name of the option
  220. *
  221. * @return The boolean representation of the option
  222. */
  223. default boolean hasOptionBool(final String domain, final String option) {
  224. return hasOption(domain, option, PERMISSIVE_VALIDATOR);
  225. }
  226. /**
  227. * Determines if this source has the specified character option.
  228. *
  229. * @param domain The domain of the option
  230. * @param option The name of the option
  231. *
  232. * @return True iff the option exists and is parsable as a char, false otherwise.
  233. */
  234. default boolean hasOptionChar(final String domain, final String option) {
  235. return hasOptionString(domain, option);
  236. }
  237. /**
  238. * Determines if this source has the specified integer option.
  239. *
  240. * @param domain The domain of the option
  241. * @param option The name of the option
  242. *
  243. * @return True iff the option exists and is parsable as an integer, false otherwise.
  244. */
  245. default boolean hasOptionInt(final String domain, final String option) {
  246. return hasOptionString(domain, option, INT_VALIDATOR);
  247. }
  248. /**
  249. * Determines if this source has the specified String option.
  250. *
  251. * <p>A String option is considered to exist if:
  252. *
  253. * <ul>
  254. * <li>there is a value for the specified option,
  255. * <li>that value is not empty, and
  256. * <li>that value is not disabled (i.e., it doesn't begin with "false:")
  257. * </ul>
  258. *
  259. * @param domain The domain of the option
  260. * @param option The name of the option
  261. *
  262. * @return True iff the option exists and is not empty, false otherwise
  263. */
  264. default boolean hasOptionString(final String domain, final String option) {
  265. return hasOptionString(domain, option, PERMISSIVE_VALIDATOR);
  266. }
  267. /**
  268. * Determines if this source has the specified String option.
  269. *
  270. * <p>A String option is considered to exist if:
  271. *
  272. * <ul>
  273. * <li>there is a value for the specified option that satisfies the specified validator,
  274. * <li>that value is not empty, and
  275. * <li>that value is not disabled (i.e., it doesn't begin with "false:")
  276. * </ul>
  277. *
  278. * @param domain The domain of the option
  279. * @param option The name of the option
  280. * @param validator The validator to use to check legal values
  281. *
  282. * @return True iff the option exists and is not empty, false otherwise
  283. */
  284. default boolean hasOptionString(final String domain, final String option,
  285. final Validator<String> validator) {
  286. final String value;
  287. return hasOption(domain, option, validator)
  288. && !(value = getOption(domain, option, validator)).isEmpty()
  289. && !value.startsWith("false:");
  290. }
  291. /**
  292. * Returns the name of all the options in the specified domain. If the domain doesn't exist, an
  293. * empty list is returned.
  294. *
  295. * @param domain The domain to search
  296. *
  297. * @return A list of options in the specified domain
  298. */
  299. Map<String, String> getOptions(String domain);
  300. }