Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Configuration2



The Configuration interface make you have to handle the serializing an unserializing yourself, and also provide accessors for the properties. There is an extended interface called Configuration2 which handle all of this for you[1]
The Configuration2 interface extends Configuration, so a class which implements Configuration2 is a Configuration
.

Basically the Configuration2 interface provide a framework to define your properties and their types, and handle getting and setting these properties, and the serializing and unserializing of the configuration.

Implementing the interface

You only need to implement the following methods:

Supported property types

The supported property types are: It is possible to handle other types of properties by implementing the following methods:

Helper methods

Several getPropertyValueAsXXX(String) allow to get the value of a property in any of the default supported types.

Example with one int property

  public class MyConfiguration implements Configuration2 {
    public static final String MY_INT_PROPERTY = "MyIntProperty";
    private static final SortedMap<String, Class<?>> properties = new TreeMap<>();
    static {
      properties.put(MY_INT_PROPERTY, Integer.Type);
    }
    private int intPropValue = 0;

    public SortedMap<String, Class<?>> getPropertiesTypes() {
      return properties;
    }

    public void setPropertyValue(String key, Object value) {
      if (key.equals(MY_INT_PROPERTY) && value instanceof Integer) {
        intPropValue = (Integer)value;
      }
    }
    public Object getPropertyValue(String key) {
      if (key.equals(MY_INT_PROPERTY)) {
        return intPropValue;
      } else {
        return null;
      }
    }
  }

AbstractConfiguration2 class


The AbstractConfiguration2 class is a default implementation of the Configuration2 interface which stores the properties in a Map.

This class is abstract but it has no abstract methods.

Notes

  1. ^ The Configuration2 interface extends Configuration, so a class which implements Configuration2 is a Configuration

See also


Categories: conf | javafx | swing

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences