2626import java .time .LocalDate ;
2727import java .util .List ;
2828import java .util .Map ;
29+ import java .util .Objects ;
2930import java .util .function .Consumer ;
3031
3132public class YamlDataFile implements UpdatableYamlDataSource
@@ -38,6 +39,7 @@ public class YamlDataFile implements UpdatableYamlDataSource
3839 private boolean isLoaded = false ;
3940 private boolean isUpdated = false ;
4041 private @ NullOr Exception invalid = null ;
42+ private @ NullOr Runnable reloadHandler = null ;
4143
4244 public YamlDataFile (Path directoryPath , String name )
4345 {
@@ -72,6 +74,12 @@ public YamlDataFile(Path directoryPath, String name, Consumer<Exception> excepti
7274
7375 public final int totalReloads () { return reloads ; }
7476
77+ protected void reloadsWith (Runnable reloadHandler )
78+ {
79+ this .reloadHandler = Objects .requireNonNull (reloadHandler , "reloadHandler" );
80+ reloadHandler .run ();
81+ }
82+
7583 public final void reload ()
7684 {
7785 reloads ++;
@@ -92,12 +100,9 @@ public final void reload()
92100 }
93101 }
94102
95- if (isAlreadyLoaded ) { handleReload (); }
103+ if (isAlreadyLoaded && reloadHandler != null ) { reloadHandler . run (); }
96104 }
97105
98- // Override me.
99- protected void handleReload () {}
100-
101106 public String toYamlString () { return data .saveToString (); }
102107
103108 public void save ()
@@ -135,8 +140,10 @@ public void header(String header)
135140 // is changed, that should not constitute rewriting the entire file.
136141 }
137142
138- public void setupHeader (String resource )
143+ public void headerFromResource (String resource )
139144 {
145+ Objects .requireNonNull (resource , "resource" );
146+
140147 try
141148 {
142149 @ NullOr URL resourceUrl = getClass ().getClassLoader ().getResource (resource );
@@ -158,26 +165,28 @@ public void setupHeader(String resource)
158165 }
159166 }
160167
168+ private void migrate (YamlValue <?> value , ConfigurationSection existing )
169+ {
170+ if (value .migrations ().isEmpty ()) { return ; }
171+ for (Migration migration : value .migrations ()) { migration .migrate (existing , this , value .key ()); }
172+ }
173+
161174 public void migrateValues (List <YamlValue <?>> values , ConfigurationSection existing )
162175 {
163- for (YamlValue <?> value : values )
164- {
165- for (Migration migration : value .migrations ())
166- {
167- migration .migrate (existing , this , value .key ());
168- }
169- }
176+ Objects .requireNonNull (values , "values" );
177+ Objects .requireNonNull (existing , "existing" );
178+
179+ for (YamlValue <?> value : values ) { migrate (value , existing ); }
170180 }
171181
172- public void setupDefaults (List <YamlValue <?>> defaults )
182+ public void defaultValues (List <YamlValue <?>> defaults )
173183 {
174184 for (YamlValue <?> value : defaults )
175185 {
186+ migrate (value , data );
176187 if (!(value instanceof DefaultYamlValue <?>)) { continue ; }
177188 setAsDefaultIfUnset ((DefaultYamlValue <?>) value );
178189 }
179-
180- migrateValues (defaults , data );
181190 }
182191
183192 protected static void write (Path filePath , String contents , Consumer <? super IOException > exceptions )
0 commit comments