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.

MapSettings.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.config.internal;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import org.sonar.api.config.Configuration;
  25. import org.sonar.api.config.PropertyDefinitions;
  26. import static java.util.Collections.unmodifiableMap;
  27. import static java.util.Objects.requireNonNull;
  28. /**
  29. * In-memory map-based implementation of {@link Settings}. It must be used
  30. * <b>only for unit tests</b>. This is not the implementation
  31. * deployed at runtime, so non-test code must never cast
  32. * {@link Settings} to {@link MapSettings}.
  33. *
  34. * @since 6.1
  35. */
  36. public class MapSettings extends Settings {
  37. private final Map<String, String> props = new HashMap<>();
  38. private final ConfigurationBridge configurationBridge;
  39. public MapSettings() {
  40. this(new PropertyDefinitions());
  41. }
  42. public MapSettings(PropertyDefinitions definitions) {
  43. super(definitions, new Encryption(null));
  44. configurationBridge = new ConfigurationBridge(this);
  45. }
  46. @Override
  47. protected Optional<String> get(String key) {
  48. return Optional.ofNullable(props.get(key));
  49. }
  50. @Override
  51. protected void set(String key, String value) {
  52. props.put(
  53. requireNonNull(key, "key can't be null"),
  54. requireNonNull(value, "value can't be null").trim());
  55. }
  56. @Override
  57. protected void remove(String key) {
  58. props.remove(key);
  59. }
  60. @Override
  61. public Map<String, String> getProperties() {
  62. return unmodifiableMap(props);
  63. }
  64. /**
  65. * Delete all properties
  66. */
  67. public MapSettings clear() {
  68. props.clear();
  69. return this;
  70. }
  71. @Override
  72. public MapSettings setProperty(String key, String value) {
  73. return (MapSettings) super.setProperty(key, value);
  74. }
  75. @Override
  76. public MapSettings setProperty(String key, Integer value) {
  77. return (MapSettings) super.setProperty(key, value);
  78. }
  79. @Override
  80. public MapSettings setProperty(String key, Boolean value) {
  81. return (MapSettings) super.setProperty(key, value);
  82. }
  83. @Override
  84. public MapSettings setProperty(String key, Long value) {
  85. return (MapSettings) super.setProperty(key, value);
  86. }
  87. /**
  88. * @return a {@link Configuration} proxy on top of this existing {@link Settings} implementation. Changes are reflected in the {@link Configuration} object.
  89. * @since 6.5
  90. */
  91. public Configuration asConfig() {
  92. return configurationBridge;
  93. }
  94. }