Skip to content

Commit acfb5e2

Browse files
committed
Clarify javadocs. Refactor migration strategies.
1 parent 1192c14 commit acfb5e2

4 files changed

Lines changed: 52 additions & 44 deletions

File tree

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/data/YamlDataFile.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,9 @@ public void migrateValues(List<YamlValue<?>> values, ConfigurationSection existi
162162
{
163163
for (YamlValue<?> value : values)
164164
{
165-
if (value.migrations().isEmpty()) { continue; }
166-
167-
for (Migration policy : value.migrations())
165+
for (Migration migration : value.migrations())
168166
{
169-
policy.migrate(existing, this, value);
167+
migration.migrate(existing, this, value.key());
170168
}
171169
}
172170
}

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/migrations/Migration.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
package community.leaf.configvalues.bukkit.migrations;
99

10-
import community.leaf.configvalues.bukkit.YamlValue;
1110
import community.leaf.configvalues.bukkit.data.YamlDataSource;
1211
import org.bukkit.configuration.ConfigurationSection;
1312

@@ -35,9 +34,8 @@ static Migration from(String key, MigrationStrategy strategy)
3534
* Migrates values at the specified key by
3635
* copying them from one config to another.
3736
*
38-
* <p>This is the simplest migration strategy:
39-
* it leaves the existing value untouched
40-
* and intact.</p>
37+
* <p>This is the simplest migration strategy: it
38+
* leaves the existing value untouched and intact.</p>
4139
*
4240
* @param key the old value's key
4341
*
@@ -53,9 +51,11 @@ static Migration copy(String key)
5351
* Migrates values at the specified key by
5452
* moving them from one config to another.
5553
*
56-
* <p>If migration is successful, the old
57-
* value is <b>removed</b> from the source
58-
* configuration.</p>
54+
* <p>Note: the value located at this migration's
55+
* key is always <b>removed</b> from the source
56+
* configuration regardless of whether migration
57+
* is successful (thus ensuring the value is
58+
* always "moved" to the destination).</p>
5959
*
6060
* @param key the old value's key
6161
*
@@ -82,20 +82,21 @@ static Migration move(String key)
8282
MigrationStrategy strategy();
8383

8484
/**
85-
* Migrates the specific value (retrieved with
86-
* {@link #key()}) from an existing configuration
87-
* into another by submitting it to the destination
88-
* YAML data at the provided value's key.
85+
* Migrates the specific value from an existing
86+
* configuration (retrieved with {@link #key()})
87+
* to the destination YAML data at the provided key.
8988
*
90-
* <p>If the retrieved value doesn't exist, then
91-
* nothing is migrated.</p>
89+
* <p>If the retrieved "existing" value doesn't exist,
90+
* then nothing is migrated.</p>
9291
*
93-
* @param existing existing configuration
94-
* @param data destination yaml data
95-
* @param value destination yaml value
92+
* @param existing existing configuration
93+
* @param destination destination yaml data
94+
* @param destinationKey destination value key
95+
*
96+
* @see MigrationStrategy#migrate(ConfigurationSection, String, YamlDataSource, String)
9697
*/
97-
default void migrate(ConfigurationSection existing, YamlDataSource data, YamlValue<?> value)
98+
default void migrate(ConfigurationSection existing, YamlDataSource destination, String destinationKey)
9899
{
99-
strategy().migrate(existing, key(), data, value);
100+
strategy().migrate(existing, key(), destination, destinationKey);
100101
}
101102
}

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/migrations/MigrationStrategy.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,39 @@
77
*/
88
package community.leaf.configvalues.bukkit.migrations;
99

10-
import community.leaf.configvalues.bukkit.YamlValue;
1110
import community.leaf.configvalues.bukkit.data.YamlDataSource;
1211
import org.bukkit.configuration.ConfigurationSection;
1312

1413
/**
15-
* Mechanism that migrates values from
16-
* one configuration to another.
14+
* Migrates values from one configuration to another.
1715
*/
1816
@FunctionalInterface
1917
public interface MigrationStrategy
2018
{
2119
/**
2220
* Migrates a value from an existing configuration
23-
* into another by retrieving it from the specified
24-
* key then submitting it to the destination YAML
25-
* data at the provided value's key.
21+
* to another by retrieving then submitting it to
22+
* the destination YAML.
2623
*
27-
* <p>If the retrieved value doesn't exist, then
28-
* nothing is migrated.</p>
24+
* <p>Note:</p>
2925
*
30-
* @param existing existing configuration
31-
* @param key key to retrieve the existing value from
32-
* @param data destination yaml data
33-
* @param value destination yaml value
26+
* <ul>
27+
* <li>
28+
* If the destination YAML already has a value at
29+
* the destination key, then migration is skipped
30+
* (but some implementations may still modify the
31+
* existing value, such as removing it).
32+
* </li>
33+
* <li>
34+
* If the retrieved "existing" value doesn't exist,
35+
* then nothing is migrated.
36+
* </li>
37+
* </ul>
38+
*
39+
* @param existing existing configuration
40+
* @param existingKey key to retrieve the value from
41+
* @param destination destination yaml data
42+
* @param destinationKey destination value key
3443
*/
35-
void migrate(ConfigurationSection existing, String key, YamlDataSource data, YamlValue<?> value);
44+
void migrate(ConfigurationSection existing, String existingKey, YamlDataSource destination, String destinationKey);
3645
}

config-values-bukkit/src/main/java/community/leaf/configvalues/bukkit/migrations/Migrations.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
package community.leaf.configvalues.bukkit.migrations;
99

10-
import community.leaf.configvalues.bukkit.YamlValue;
1110
import community.leaf.configvalues.bukkit.data.YamlDataSource;
1211
import org.bukkit.configuration.ConfigurationSection;
1312
import pl.tlinkowski.annotation.basic.NullOr;
@@ -18,19 +17,20 @@ final class Migrations
1817
{
1918
private Migrations() { throw new UnsupportedOperationException(); }
2019

21-
static void copy(ConfigurationSection existing, String key, YamlDataSource data, YamlValue<?> value)
20+
static void copy(ConfigurationSection existing, String existingKey, YamlDataSource destination, String destinationKey)
2221
{
23-
@NullOr Object maybe = existing.get(key);
24-
if (maybe != null) { data.set(value.key(), maybe); }
22+
if (destination.data().isSet(destinationKey)) { return; }
23+
@NullOr Object maybe = existing.get(existingKey);
24+
if (maybe != null) { destination.set(destinationKey, maybe); }
2525
}
2626

27-
static void move(ConfigurationSection existing, String key, YamlDataSource data, YamlValue<?> value)
27+
static void move(ConfigurationSection existing, String existingKey, YamlDataSource destination, String destinationKey)
2828
{
29-
@NullOr Object maybe = existing.get(key);
30-
if (maybe == null) { return; }
29+
copy(existing, existingKey, destination, destinationKey);
3130

32-
data.set(value.key(), maybe);
33-
existing.set(key, null);
31+
// Only remove from source config if keys are distinct
32+
// (protects the destination value if the config is migrating from itself)
33+
if (!existingKey.equals(destinationKey)) { existing.set(existingKey, null); }
3434
}
3535

3636
static final class Impl implements Migration

0 commit comments

Comments
 (0)