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;
021
022import com.google.common.collect.ImmutableMap;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.Optional;
026
027/**
028 * In-memory map-based implementation of {@link Settings}. It must be used
029 * <b>only for unit tests</b>. This is not the implementation
030 * deployed at runtime, so non-test code must never cast
031 * {@link Settings} to {@link MapSettings}.
032 *
033 * @since 6.1
034 */
035public class MapSettings extends Settings {
036
037  private final Map<String, String> props = new HashMap<>();
038
039  public MapSettings() {
040    super(new PropertyDefinitions(), new Encryption(null));
041  }
042
043  public MapSettings(PropertyDefinitions definitions) {
044    super(definitions, new Encryption(null));
045  }
046
047  @Override
048  protected Optional<String> get(String key) {
049    return Optional.ofNullable(props.get(key));
050  }
051
052  @Override
053  protected void set(String key, String value) {
054    props.put(key, value);
055  }
056
057  @Override
058  protected void remove(String key) {
059    props.remove(key);
060  }
061
062  @Override
063  public Map<String, String> getProperties() {
064    return ImmutableMap.copyOf(props);
065  }
066
067  /**
068   * Delete all properties
069   */
070  public MapSettings clear() {
071    props.clear();
072    return this;
073  }
074}