Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ReadOnlyConfigProvider.java 13KB

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