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

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