001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (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
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.configuration;
020
021import java.util.Map;
022
023/**
024 * This is a {@link Configuration} implementation that does not save or load
025 * from any source, and stores all values in memory only.
026 * This is useful for temporary Configurations for providing defaults.
027 */
028public class MemoryConfiguration extends MemorySection implements Configuration {
029
030    protected Configuration defaults;
031    protected MemoryConfigurationOptions options;
032
033    /**
034     * Creates an empty {@link MemoryConfiguration} with no default values.
035     */
036    public MemoryConfiguration() {
037    }
038
039    /**
040     * Creates an empty {@link MemoryConfiguration} using the specified {@link
041     * Configuration} as a source for all default values.
042     *
043     * @param defaults Default value provider
044     * @throws IllegalArgumentException Thrown if defaults is null
045     */
046    public MemoryConfiguration(Configuration defaults) {
047        this.defaults = defaults;
048    }
049
050    @Override
051    public void addDefault(String path, Object value) {
052        if (this.defaults == null) {
053            this.defaults = new MemoryConfiguration();
054        }
055
056        this.defaults.set(path, value);
057    }
058
059    @Override
060    public void addDefaults(Map<String, Object> defaults) {
061        for (Map.Entry<String, Object> entry : defaults.entrySet()) {
062            addDefault(entry.getKey(), entry.getValue());
063        }
064    }
065
066    @Override
067    public void addDefaults(Configuration defaults) {
068        addDefaults(defaults.getValues(true));
069    }
070
071    @Override
072    public Configuration getDefaults() {
073        return this.defaults;
074    }
075
076    @Override
077    public void setDefaults(Configuration defaults) {
078        if (defaults == null) {
079            throw new NullPointerException("Defaults may not be null");
080        }
081
082        this.defaults = defaults;
083    }
084
085    @Override
086    public ConfigurationSection getParent() {
087        return null;
088    }
089
090    @Override
091    public MemoryConfigurationOptions options() {
092        if (this.options == null) {
093            this.options = new MemoryConfigurationOptions(this);
094        }
095
096        return this.options;
097    }
098
099}