Skip to content

Migrating from 0.x to 1.x

ljacqu edited this page Sep 8, 2018 · 4 revisions

Creating settings manager

Previously the SettingsManager had a public constructor and other helper methods, now a builder SettingsManagerBuilder exists to get instances from.

Old:

SettingsManager settingsManager = new SettingsManager(
  myPropertyResource, myMigrationService, CommandSettings.class, DelaySettings.class);

New:

SettingsManager settingsManager = SettingsManagerBuilder
  .withResource(myPropertyResource)
  .configurationData(CommandSettings.class, DelaySettings.class)
  .migrationService(myMigrationService)
  .create();

Renamings / interfaces

Many classes now have their own interface (e.g. SettingsManager, Mapper, ConfigurationData). Typically a builder class or a standard -Impl class exists to get instances of those types.

Among others, please note also that some methods have been renamed: Property#getValue(PropertyResource) is now Property#determineValue(PropertyReader), and ConfigurationDataBuilder#collectData has been renamed to createConfiguration.

Export value

The Property<T> interface has a new method Object toExportValue(T). This method should convert the value from the argument to a value suitable for exporting. The return value should be limited to null, String, boolean, any Number types as well as lists and maps of such types.

Essentially it is the opposite of Property#determineValue(Reader), which constructs the value of the property from values of rudimentary types from the reader. Consider the examples from EnumProperty (simplified for brevity):

@Override
protected MyEnum getFromReader(PropertyReader reader) {
  String value = reader.getString(getPath());
  return value == null ? null : MyEnum.valueOf(value);
}

@Override
public Object toExportValue(MyEnum value) {
  return value.name();
}

Property resource / Configuration data

In ConfigMe 0.x, the PropertyResource kept track of all properties' values. ConfigurationData only contained a list of all known properties and configured comments. Now, in ConfigMe 1.x, the responsibilities of PropertyResource have been reduced to providing a read-only PropertyReader (file reader) and to exporting property values. The values of the properties are now being managed by ConfigurationData.

Property reader can no longer be refreshed: for a newer state of a file you should get a new PropertyReader from PropertyResource. Typically these changes shouldn't affect you directly. However, as a consequence, the migration service interface has changed. See the next section for more details.

Migration service

Since the property resource no longer manages the property's values, while migrating you can set the value of a property to the configuration data instance instead.

Old:

@Override
public boolean checkAndMigrate(PropertyResource resource, List<Property<?>> properties) {
  if (someCheck()) {
    resource.setValue("property", new Value());
    return true;
  }
  return false;
}

New:

@Override
public boolean checkAndMigrate(PropertyReader reader, ConfigurationData configurationData) {
  if (someCheck()) {
    Property<Value> property = SettingsHolderImpl.VALUE_PROP;
    configurationData.setValue(property, new Value());
    return true;
  }
  return false;
}

In other words, PropertyResource#setValue(String, Object) has been changed to ConfigurationData#setValue(Property<T>, T). You read from property reader and you write to configuration data.

Lowercase String List property now lowercase String Set property

The property type Property<List<String>> which had all its entries in lowercase has been changed to a Set: Property<Set<String>>. The reason is that Set has a more efficient #contains method, which is what this property type is typically used for.

Old:

import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseListProperty;

public static final Property<List<String>> FORBIDDEN_NAMES =
  newLowercaseListProperty("blacklist.names", "test", "potato");

New:

import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseStringSetProperty;

public static final Property<Set<String>> FORBIDDEN_NAMES =
  newLowercaseStringSetProperty("blacklist.names", "test", "potato");

Section comments

Annotation @SectionComments has been replaced with a method on SettingsHolder which can be extended to register comments.

Old:

@SectionComments
public static Map<String, String[]> buildSectionComments() {
  return ImmutableMap.of("Converter", new String[]{"Converter settings"});
}

New:

@Override
public void registerComments(CommentsConfiguration conf) {
  conf.setComment("Converter", "Converter settings");
}