aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anthonyhilyard/iceberg/util/DynamicSubconfig.java
blob: 3ed17f3cae0e8dbbbd85289f556df4dd4ed7a87b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.anthonyhilyard.iceberg.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import com.electronwill.nightconfig.core.AbstractCommentedConfig;
import com.electronwill.nightconfig.core.ConfigFormat;
import com.electronwill.nightconfig.core.UnmodifiableCommentedConfig;
import com.electronwill.nightconfig.core.UnmodifiableConfig;

/**
 * An exact copy of SimpleCommentedConfig, but this class is specifically meant for subconfigs.
 * That being said--the class of a config is checked during config validation, and subconfigs are allowed
 * extra leniency in config keys.
 */
final public class DynamicSubconfig extends AbstractCommentedConfig
{
	private final ConfigFormat<?> configFormat;

	/**
	 * Creates a Subconfig with the specified format.
	 *
	 * @param configFormat the config's format
	 */
	DynamicSubconfig(ConfigFormat<?> configFormat, boolean concurrent)
	{
		super(concurrent ? new ConcurrentHashMap<>() : new HashMap<>());
		this.configFormat = configFormat;
	}

	/**
	 * Creates a Subconfig with the specified data and format. The map is used as it is and
	 * isn't copied.
	 */
	DynamicSubconfig(Map<String, Object> valueMap, ConfigFormat<?> configFormat)
	{
		super(valueMap);
		this.configFormat = configFormat;
	}
	
	/**
	 * Creates a Subconfig with the specified backing map supplier and format.
	 * 
	 * @param mapCreator the supplier for backing maps
	 * @param configFormat the config's format
	 */
	DynamicSubconfig(Supplier<Map<String, Object>> mapCreator, ConfigFormat<?> configFormat)
	{
		super(mapCreator);
		this.configFormat = configFormat;
	}

	/**
	 * Creates a Subconfig by copying a config and with the specified format.
	 *
	 * @param toCopy       the config to copy
	 * @param configFormat the config's format
	 */
	DynamicSubconfig(UnmodifiableConfig toCopy, ConfigFormat<?> configFormat,
						  boolean concurrent)
	{
		super(toCopy, concurrent);
		this.configFormat = configFormat;
	}
	
	/**
	 * Creates a Subconfig by copying a config, with the specified backing map creator and format.
	 *
	 * @param toCopy       the config to copy
	 * @param mapCreator   the supplier for backing maps
	 * @param configFormat the config's format
	 */
	public DynamicSubconfig(UnmodifiableConfig toCopy, Supplier<Map<String, Object>> mapCreator,
			ConfigFormat<?> configFormat)
	{
		super(toCopy, mapCreator);
		this.configFormat = configFormat;
	}

	/**
	 * Creates a Subconfig by copying a config and with the specified format.
	 *
	 * @param toCopy       the config to copy
	 * @param configFormat the config's format
	 */
	DynamicSubconfig(UnmodifiableCommentedConfig toCopy, ConfigFormat<?> configFormat,
						  boolean concurrent)
	{
		super(toCopy, concurrent);
		this.configFormat = configFormat;
	}
	
	/**
	 * Creates a Subconfig by copying a config, with the specified backing map creator and format.
	 *
	 * @param toCopy       the config to copy
	 * @param mapCreator   the supplier for backing maps
	 * @param configFormat the config's format
	 */
	public DynamicSubconfig(UnmodifiableCommentedConfig toCopy, Supplier<Map<String, Object>> mapCreator,
			ConfigFormat<?> configFormat)
	{
		super(toCopy, mapCreator);
		this.configFormat = configFormat;
	}

	@Override
	public ConfigFormat<?> configFormat()
	{
		return configFormat;
	}

	@Override
	public DynamicSubconfig createSubConfig()
	{
		return new DynamicSubconfig(mapCreator, configFormat);
	}

	@Override
	public AbstractCommentedConfig clone()
	{
		return new DynamicSubconfig(this, mapCreator, configFormat);
	}

	/**
	 * Creates a new Subconfig with the content of the given config. The returned config will have
	 * the same format as the copied config.
	 *
	 * @param config the config to copy
	 * @return a copy of the config
	 */
	static DynamicSubconfig copy(UnmodifiableConfig config)
	{
		return new DynamicSubconfig(config, config.configFormat(), false);
	}
}