Search in sources :

Example 6 with PropertyModule

use of org.opt4j.core.config.PropertyModule in project opt4j by felixreimann.

the class Opt4JModule method configure.

/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.inject.AbstractModule#configure()
	 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
    /**
     * Configure injected constants.
     */
    PropertyModule module = new PropertyModule(this);
    for (Property property : module.getProperties()) {
        for (Annotation annotation : property.getAnnotations()) {
            if (annotation.annotationType().getAnnotation(BindingAnnotation.class) != null) {
                Class<?> type = property.getType();
                Object value = property.getValue();
                ConstantBindingBuilder builder = bindConstant(annotation);
                if (type.equals(Integer.TYPE)) {
                    builder.to((Integer) value);
                } else if (type.equals(Long.TYPE)) {
                    builder.to((Long) value);
                } else if (type.equals(Double.TYPE)) {
                    builder.to((Double) value);
                } else if (type.equals(Float.TYPE)) {
                    builder.to((Float) value);
                } else if (type.equals(Byte.TYPE)) {
                    builder.to((Byte) value);
                } else if (type.equals(Short.TYPE)) {
                    builder.to((Short) value);
                } else if (type.equals(Boolean.TYPE)) {
                    builder.to((Boolean) value);
                } else if (type.equals(Character.TYPE)) {
                    builder.to((Character) value);
                } else if (type.equals(String.class)) {
                    builder.to((String) value);
                } else if (type.equals(Class.class)) {
                    builder.to((Class<?>) value);
                } else if (value instanceof Enum<?>) {
                    builder.to((Enum) value);
                } else {
                    String message = "Constant type not bindable: " + type + " of field " + property.getName() + " in module " + this.getClass().getName();
                    throw new ConfigurationException(Arrays.asList(new Message(message)));
                }
            }
        }
    }
    multi(OptimizerStateListener.class);
    multi(OptimizerIterationListener.class);
    multi(IndividualStateListener.class);
    config();
}
Also used : Message(com.google.inject.spi.Message) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) ConstantBindingBuilder(com.google.inject.binder.ConstantBindingBuilder) ConfigurationException(com.google.inject.ConfigurationException) BindingAnnotation(com.google.inject.BindingAnnotation) PropertyModule(org.opt4j.core.config.PropertyModule) Property(org.opt4j.core.config.Property)

Example 7 with PropertyModule

use of org.opt4j.core.config.PropertyModule in project opt4j by felixreimann.

the class FileOperations method save.

/**
 * Saves the current selected modules to the specified file.
 *
 * @param file
 *            the configuration file
 */
public void save(File file) {
    setFile(file);
    ModuleSaver saver = new ModuleSaver();
    Collection<PropertyModule> modules = selectedModules;
    System.out.println(modules);
    saver.save(file, modules);
    System.out.println("Saved configuration to " + file);
}
Also used : ModuleSaver(org.opt4j.core.config.ModuleSaver) PropertyModule(org.opt4j.core.config.PropertyModule)

Aggregations

PropertyModule (org.opt4j.core.config.PropertyModule)7 Module (com.google.inject.Module)3 BindingAnnotation (com.google.inject.BindingAnnotation)1 ConfigurationException (com.google.inject.ConfigurationException)1 ConstantBindingBuilder (com.google.inject.binder.ConstantBindingBuilder)1 Message (com.google.inject.spi.Message)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 JMenuItem (javax.swing.JMenuItem)1 ModuleLoader (org.opt4j.core.config.ModuleLoader)1 ModuleSaver (org.opt4j.core.config.ModuleSaver)1 Property (org.opt4j.core.config.Property)1 Multi (org.opt4j.core.config.annotations.Multi)1