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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 org.sonar.api.utils.System2;
  27. import static java.util.Collections.unmodifiableMap;
  28. import static java.util.Objects.requireNonNull;
  29. /**
  30. * In-memory map-based implementation of {@link Settings}. It must be used
  31. * <b>only for unit tests</b>. This is not the implementation
  32. * deployed at runtime, so non-test code must never cast
  33. * {@link Settings} to {@link MapSettings}.
  34. *
  35. * @since 6.1
  36. */
  37. public class MapSettings extends Settings {
  38. private final Map<String, String> props = new HashMap<>();
  39. private final ConfigurationBridge configurationBridge;
  40. public MapSettings() {
  41. this(new PropertyDefinitions(System2.INSTANCE));
  42. }
  43. public MapSettings(PropertyDefinitions definitions) {
  44. super(definitions, new Encryption(null));
  45. configurationBridge = new ConfigurationBridge(this);
  46. }
  47. @Override
  48. protected Optional<String> get(String key) {
  49. return Optional.ofNullable(props.get(key));
  50. }
  51. @Override
  52. protected void set(String key, String value) {
  53. props.put(
  54. requireNonNull(key, "key can't be null"),
  55. requireNonNull(value, "value can't be null").trim());
  56. }
  57. @Override
  58. protected void remove(String key) {
  59. props.remove(key);
  60. }
  61. @Override
  62. public Map<String, String> getProperties() {
  63. return unmodifiableMap(props);
  64. }
  65. /**
  66. * Delete all properties
  67. */
  68. public MapSettings clear() {
  69. props.clear();
  70. return this;
  71. }
  72. @Override
  73. public MapSettings setProperty(String key, String value) {
  74. return (MapSettings) super.setProperty(key, value);
  75. }
  76. @Override
  77. public MapSettings setProperty(String key, Integer value) {
  78. return (MapSettings) super.setProperty(key, value);
  79. }
  80. @Override
  81. public MapSettings setProperty(String key, Boolean value) {
  82. return (MapSettings) super.setProperty(key, value);
  83. }
  84. @Override
  85. public MapSettings setProperty(String key, Long value) {
  86. return (MapSettings) super.setProperty(key, value);
  87. }
  88. /**
  89. * @return a {@link Configuration} proxy on top of this existing {@link Settings} implementation. Changes are reflected in the {@link Configuration} object.
  90. * @since 6.5
  91. */
  92. public Configuration asConfig() {
  93. return configurationBridge;
  94. }
  95. }