001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.config.internal;
021
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Optional;
025import org.sonar.api.config.Configuration;
026import org.sonar.api.config.Encryption;
027import org.sonar.api.config.PropertyDefinitions;
028import org.sonar.api.config.Settings;
029
030import static java.util.Collections.unmodifiableMap;
031import static java.util.Objects.requireNonNull;
032
033/**
034 * In-memory map-based implementation of {@link Settings}. It must be used
035 * <b>only for unit tests</b>. This is not the implementation
036 * deployed at runtime, so non-test code must never cast
037 * {@link Settings} to {@link MapSettings}.
038 *
039 * @since 6.1
040 */
041public class MapSettings extends Settings {
042
043  private final Map<String, String> props = new HashMap<>();
044  private final ConfigurationBridge configurationBridge;
045
046  public MapSettings() {
047    this(new PropertyDefinitions());
048  }
049
050  public MapSettings(PropertyDefinitions definitions) {
051    super(definitions, new Encryption(null));
052    configurationBridge = new ConfigurationBridge(this);
053  }
054
055  @Override
056  protected Optional<String> get(String key) {
057    return Optional.ofNullable(props.get(key));
058  }
059
060  @Override
061  protected void set(String key, String value) {
062    props.put(
063      requireNonNull(key, "key can't be null"),
064      requireNonNull(value, "value can't be null").trim());
065  }
066
067  @Override
068  protected void remove(String key) {
069    props.remove(key);
070  }
071
072  @Override
073  public Map<String, String> getProperties() {
074    return unmodifiableMap(props);
075  }
076
077  /**
078   * Delete all properties
079   */
080  public MapSettings clear() {
081    props.clear();
082    return this;
083  }
084
085  @Override
086  public MapSettings setProperty(String key, String value) {
087    return (MapSettings) super.setProperty(key, value);
088  }
089
090  @Override
091  public MapSettings setProperty(String key, Integer value) {
092    return (MapSettings) super.setProperty(key, value);
093  }
094
095  @Override
096  public MapSettings setProperty(String key, Boolean value) {
097    return (MapSettings) super.setProperty(key, value);
098  }
099
100  @Override
101  public MapSettings setProperty(String key, Long value) {
102    return (MapSettings) super.setProperty(key, value);
103  }
104
105  /**
106   * @return a {@link Configuration} proxy on top of this existing {@link Settings} implementation. Changes are reflected in the {@link Configuration} object.
107   * @since 6.5
108   */
109  public Configuration asConfig() {
110    return configurationBridge;
111  }
112}