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;
031
032/**
033 * In-memory map-based implementation of {@link Settings}. It must be used
034 * <b>only for unit tests</b>. This is not the implementation
035 * deployed at runtime, so non-test code must never cast
036 * {@link Settings} to {@link MapSettings}.
037 *
038 * @since 6.1
039 */
040public class MapSettings extends Settings {
041
042  private final Map<String, String> props = new HashMap<>();
043  private final ConfigurationBridge configurationBridge;
044
045  public MapSettings() {
046    this(new PropertyDefinitions());
047  }
048
049  public MapSettings(PropertyDefinitions definitions) {
050    super(definitions, new Encryption(null));
051    configurationBridge = new ConfigurationBridge(this);
052  }
053
054  @Override
055  protected Optional<String> get(String key) {
056    return Optional.ofNullable(props.get(key));
057  }
058
059  @Override
060  protected void set(String key, String value) {
061    props.put(key, value);
062  }
063
064  @Override
065  protected void remove(String key) {
066    props.remove(key);
067  }
068
069  @Override
070  public Map<String, String> getProperties() {
071    return unmodifiableMap(props);
072  }
073
074  /**
075   * Delete all properties
076   */
077  public MapSettings clear() {
078    props.clear();
079    return this;
080  }
081
082  @Override
083  public MapSettings setProperty(String key, String value) {
084    return (MapSettings) super.setProperty(key, value);
085  }
086
087  @Override
088  public MapSettings setProperty(String key, Integer value) {
089    return (MapSettings) super.setProperty(key, value);
090  }
091
092  @Override
093  public MapSettings setProperty(String key, Boolean value) {
094    return (MapSettings) super.setProperty(key, value);
095  }
096
097  @Override
098  public MapSettings setProperty(String key, Long value) {
099    return (MapSettings) super.setProperty(key, value);
100  }
101
102  /**
103   * @return a {@link Configuration} proxy on top of this existing {@link Settings} implementation. Changes are reflected in the {@link Configuration} object.
104   * @since 6.5
105   */
106  public Configuration asConfig() {
107    return configurationBridge;
108  }
109}