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.

ConfigValueRetriever.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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;
  18. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  19. import java.util.List;
  20. /**
  21. * Provides methods to retrieve a value of a certain type from a config provider.
  22. */
  23. public class ConfigValueRetriever {
  24. private final ReadOnlyConfigProvider configProvider;
  25. public ConfigValueRetriever(final ReadOnlyConfigProvider configProvider) {
  26. this.configProvider = configProvider;
  27. }
  28. /**
  29. * Gets a value from the configuration manager, and attempts to coerce it into the given class.
  30. *
  31. * @param targetClass The desired class.
  32. * @param domain The domain of the option to retrieve.
  33. * @param key The key of the option to retrieve.
  34. * @param required Whether the option is required or not (only used for strings).
  35. * @param fallbacks Ordered collection of domain/key pairs to try if the value is not set.
  36. *
  37. * @return An object representing the current value of the configuration key(s) given, of the
  38. * desired target class, or null if the type conversion couldn't be performed.
  39. */
  40. public Object getValue(final Class<?> targetClass, final String domain, final String key,
  41. final boolean required, final String ... fallbacks) {
  42. if (targetClass.equals(String.class)) {
  43. return configProvider.getOptionString(
  44. domain, key, required, ReadOnlyConfigProvider.PERMISSIVE_VALIDATOR, fallbacks);
  45. }
  46. if (targetClass.equals(Boolean.class) || targetClass.equals(Boolean.TYPE)) {
  47. return configProvider.getOptionBool(domain, key);
  48. }
  49. if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE)) {
  50. return configProvider.getOptionChar(domain, key);
  51. }
  52. if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE)) {
  53. return configProvider.getOptionInt(domain, key, fallbacks);
  54. }
  55. if (targetClass.equals(List.class)) {
  56. return configProvider.getOptionList(domain, key);
  57. }
  58. return null;
  59. }
  60. /**
  61. * Gets a value from the configuration manager, and attempts to coerce it into the given class.
  62. *
  63. * @param targetClass The desired class.
  64. * @param domain The domain of the option to retrieve.
  65. * @param key The key of the option to retrieve.
  66. *
  67. * @return An object representing the current value of the configuration key(s) given, of the
  68. * desired target class, or null if the type conversion couldn't be performed.
  69. */
  70. public Object getValue(final Class<?> targetClass, final String domain, final String key) {
  71. return getValue(targetClass, domain, key, true);
  72. }
  73. }