Abacus/core/src/main/java/org/nwapw/abacus/config/Configuration.java

160 lines
4.5 KiB
Java
Raw Normal View History

2017-07-28 20:03:50 -07:00
package org.nwapw.abacus.config;
import com.moandjiezana.toml.Toml;
import com.moandjiezana.toml.TomlWriter;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
2017-07-28 20:03:50 -07:00
/**
* The configuration object that stores
* options that the user can change.
2017-07-28 20:03:50 -07:00
*/
public class Configuration {
/**
2017-08-02 21:57:53 -07:00
* The defaults TOML string.
*/
2017-08-02 21:57:53 -07:00
private static final String DEFAULT_CONFIG =
"numberImplementation = \"naive\"\n" +
2017-08-04 13:20:57 -07:00
"disabledPlugins = []";
2017-08-02 21:57:53 -07:00
/**
* The defaults TOML object, parsed from the string.
*/
private static final Toml DEFAULT_TOML = new Toml().read(DEFAULT_CONFIG);
/**
2017-08-02 21:57:53 -07:00
* The TOML writer used to write this configuration to a file.
2017-07-28 20:03:50 -07:00
*/
2017-08-02 21:57:53 -07:00
private static final TomlWriter TOML_WRITER = new TomlWriter();
2017-07-28 20:03:50 -07:00
2017-08-07 09:22:11 -07:00
/**
* The computation delay for which the thread can run without interruption.
*/
private double computationDelay = 0;
/**
* The implementation of the number that should be used.
*/
2017-08-02 21:57:53 -07:00
private String numberImplementation = "<default>";
/**
* The list of disabled plugins in this Configuration.
*/
private Set<String> disabledPlugins = new HashSet<>();
2017-08-05 17:06:22 -07:00
/**
* Creates a new configuration form the given configuration.
*
* @param copyFrom the configuration to copy.
*/
2017-08-14 19:03:52 -07:00
public Configuration(Configuration copyFrom) {
2017-08-05 17:06:22 -07:00
copyFrom(copyFrom);
}
/**
* Creates a new configuration with the given values.
2017-08-04 13:20:57 -07:00
*
2017-08-07 09:22:11 -07:00
* @param computationDelay the delay before the computation gets killed.
* @param numberImplementation the number implementation, like "naive" or "precise"
2017-08-04 13:20:57 -07:00
* @param disabledPlugins the list of disabled plugins.
*/
2017-08-07 09:22:11 -07:00
public Configuration(double computationDelay, String numberImplementation, String[] disabledPlugins) {
this.computationDelay = computationDelay;
this.numberImplementation = numberImplementation;
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
}
/**
* Loads a configuration from a given file, keeping non-specified fields default.
2017-08-04 13:20:57 -07:00
*
* @param fromFile the file to load from.
*/
2017-08-04 13:20:57 -07:00
public Configuration(File fromFile) {
if (!fromFile.exists()) return;
2017-08-02 21:57:53 -07:00
copyFrom(new Toml(DEFAULT_TOML).read(fromFile).to(Configuration.class));
}
/**
* Copies the values from the given configuration into this one.
2017-08-04 13:20:57 -07:00
*
* @param otherConfiguration the configuration to copy from.
*/
2017-08-04 13:20:57 -07:00
public void copyFrom(Configuration otherConfiguration) {
2017-08-07 09:22:11 -07:00
this.computationDelay = otherConfiguration.computationDelay;
this.numberImplementation = otherConfiguration.numberImplementation;
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
}
/**
* Saves this configuration to the given file, creating
* any directories that do not exist.
2017-08-04 13:20:57 -07:00
*
* @param file the file to save to.
*/
2017-08-04 13:20:57 -07:00
public void saveTo(File file) {
if (file.getParentFile() != null) file.getParentFile().mkdirs();
try {
TOML_WRITER.write(this, file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Gets the value of this configuration as a string.
*
* @return the string that represents this configuration.
*/
2017-08-14 19:03:52 -07:00
public String asTomlString() {
return TOML_WRITER.write(this);
}
/**
* Gets the number implementation from this configuration.
2017-08-04 13:20:57 -07:00
*
* @return the number implementation.
*/
public String getNumberImplementation() {
return numberImplementation;
}
/**
* Sets the number implementation for the configuration
2017-08-04 13:20:57 -07:00
*
* @param numberImplementation the number implementation.
*/
public void setNumberImplementation(String numberImplementation) {
this.numberImplementation = numberImplementation;
}
/**
* Gets the list of disabled plugins.
2017-08-04 13:20:57 -07:00
*
* @return the list of disabled plugins.
*/
public Set<String> getDisabledPlugins() {
return disabledPlugins;
}
2017-08-07 09:22:11 -07:00
/**
* Gets the computation delay specified in the configuration.
*
* @return the computaton delay.
*/
public double getComputationDelay() {
return computationDelay;
}
/**
* Sets the computation delay.
*
* @param computationDelay the new computation delay.
*/
public void setComputationDelay(double computationDelay) {
this.computationDelay = computationDelay;
}
2017-07-28 20:03:50 -07:00
}