001 package org.bukkit.configuration;
002
003 import java.util.Map;
004
005 import org.apache.commons.lang.Validate;
006
007 /**
008 * This is a {@link Configuration} implementation that does not save or load
009 * from any source, and stores all values in memory only.
010 * This is useful for temporary Configurations for providing defaults.
011 */
012 public class MemoryConfiguration extends MemorySection implements Configuration {
013 protected Configuration defaults;
014 protected MemoryConfigurationOptions options;
015
016 /**
017 * Creates an empty {@link MemoryConfiguration} with no default values.
018 */
019 public MemoryConfiguration() {}
020
021 /**
022 * Creates an empty {@link MemoryConfiguration} using the specified {@link
023 * Configuration} as a source for all default values.
024 *
025 * @param defaults Default value provider
026 * @throws IllegalArgumentException Thrown if defaults is null
027 */
028 public MemoryConfiguration(Configuration defaults) {
029 this.defaults = defaults;
030 }
031
032 @Override
033 public void addDefault(String path, Object value) {
034 Validate.notNull(path, "Path may not be null");
035
036 if (defaults == null) {
037 defaults = new MemoryConfiguration();
038 }
039
040 defaults.set(path, value);
041 }
042
043 public void addDefaults(Map<String, Object> defaults) {
044 Validate.notNull(defaults, "Defaults may not be null");
045
046 for (Map.Entry<String, Object> entry : defaults.entrySet()) {
047 addDefault(entry.getKey(), entry.getValue());
048 }
049 }
050
051 public void addDefaults(Configuration defaults) {
052 Validate.notNull(defaults, "Defaults may not be null");
053
054 addDefaults(defaults.getValues(true));
055 }
056
057 public void setDefaults(Configuration defaults) {
058 Validate.notNull(defaults, "Defaults may not be null");
059
060 this.defaults = defaults;
061 }
062
063 public Configuration getDefaults() {
064 return defaults;
065 }
066
067 @Override
068 public ConfigurationSection getParent() {
069 return null;
070 }
071
072 public MemoryConfigurationOptions options() {
073 if (options == null) {
074 options = new MemoryConfigurationOptions(this);
075 }
076
077 return options;
078 }
079 }