-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4761c71
commit 64ecbd6
Showing
25 changed files
with
417 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package flowtimer.parsing.config; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.text.ParseException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class Config { | ||
|
||
private ArrayList<ConfigObject> config; | ||
|
||
public Config() { | ||
config = new ArrayList<>(); | ||
} | ||
|
||
public void put(ConfigObject object) { | ||
config.add(object); | ||
} | ||
|
||
public void load(File file) throws Exception { | ||
HashMap<String, String> processedMap = new HashMap<>(); | ||
|
||
if(file.exists()) { | ||
BufferedReader br = new BufferedReader(new FileReader(file)); | ||
String line; | ||
int lineNumber = 0; | ||
while((line = br.readLine()) != null) { | ||
lineNumber++; | ||
if(line.isEmpty()) { | ||
continue; | ||
} | ||
|
||
char start = line.charAt(0); | ||
if(start == '[' || start == '#') { | ||
continue; | ||
} | ||
String[] tokens = line.split("="); | ||
if(tokens.length != 2) { | ||
br.close(); | ||
throw new ParseException("Line has too many '=' (line " + lineNumber + ")", lineNumber); | ||
} | ||
processedMap.put(tokens[0].trim(), tokens[1].trim()); | ||
} | ||
br.close(); | ||
} | ||
|
||
config.stream().forEach(entry -> entry.load(processedMap)); | ||
} | ||
|
||
public void save(File file) throws Exception { | ||
BufferedWriter bw = new BufferedWriter(new FileWriter(file)); | ||
for(ConfigObject object : config) { | ||
bw.write(object.getToWrite() + "\n"); | ||
} | ||
bw.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package flowtimer.parsing.config; | ||
|
||
import java.util.HashMap; | ||
|
||
public class ConfigComment implements ConfigObject { | ||
|
||
public static final char PREFIX = '#'; | ||
|
||
public String comment; | ||
|
||
public ConfigComment(String comment) { | ||
this.comment = comment; | ||
} | ||
|
||
public void load(HashMap<String, String> config) { | ||
} | ||
|
||
public String getToWrite() { | ||
return PREFIX + " " + comment; | ||
} | ||
|
||
public String getComment() { | ||
return comment; | ||
} | ||
|
||
public void setComment(String comment) { | ||
this.comment = comment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package flowtimer.parsing.config; | ||
|
||
import java.util.HashMap; | ||
|
||
public abstract class ConfigEntry<T> implements ConfigObject { | ||
|
||
private String name; | ||
private T defaultValue; | ||
private ConfigEntryLoadCallback<T> loadCallback; | ||
private ConfigEntrySaveCallback<T> saveCallback; | ||
private ConfigEntrySaveCondition saveCondition; | ||
|
||
public ConfigEntry(String name, T defaultValue, ConfigEntryLoadCallback<T> loadCallback, ConfigEntrySaveCallback<T> saveCallback, ConfigEntrySaveCondition saveCondition) { | ||
this.name = name; | ||
this.defaultValue = defaultValue; | ||
this.loadCallback = loadCallback; | ||
this.saveCallback = saveCallback; | ||
this.saveCondition = saveCondition; | ||
} | ||
|
||
public abstract T fromString(String value); | ||
|
||
public void load(HashMap<String, String> config) { | ||
loadCallback.onLoad(config.containsKey(name) ? fromString(config.get(name)) : defaultValue); | ||
} | ||
|
||
public String getToWrite() { | ||
return name + "=" + (saveCondition.get() ? String.valueOf(saveCallback.onSave()) : String.valueOf(defaultValue)); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public T getDefaultValue() { | ||
return defaultValue; | ||
} | ||
|
||
public void setDefaultValue(T defaultValue) { | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
public ConfigEntryLoadCallback<T> getLoadCallback() { | ||
return loadCallback; | ||
} | ||
|
||
public void setLoadCallback(ConfigEntryLoadCallback<T> loadCallback) { | ||
this.loadCallback = loadCallback; | ||
} | ||
|
||
public ConfigEntrySaveCallback<T> getSaveCallback() { | ||
return saveCallback; | ||
} | ||
|
||
public void setSaveCallback(ConfigEntrySaveCallback<T> saveCallback) { | ||
this.saveCallback = saveCallback; | ||
} | ||
|
||
public ConfigEntrySaveCondition getSaveCondition() { | ||
return saveCondition; | ||
} | ||
|
||
public void setSaveCondition(ConfigEntrySaveCondition saveCondition) { | ||
this.saveCondition = saveCondition; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package flowtimer.parsing.config; | ||
|
||
public class ConfigEntryBoolean extends ConfigEntry<Boolean> { | ||
|
||
public ConfigEntryBoolean(String name, boolean defaultValue, ConfigEntryLoadCallback<Boolean> loadCallback, ConfigEntrySaveCallback<Boolean> saveCallback) { | ||
this(name, defaultValue, loadCallback, saveCallback, () -> true); | ||
} | ||
|
||
public ConfigEntryBoolean(String name, boolean defaultValue, ConfigEntryLoadCallback<Boolean> loadCallback, ConfigEntrySaveCallback<Boolean> saveCallback, ConfigEntrySaveCondition saveCondition) { | ||
super(name, defaultValue, loadCallback, saveCallback, saveCondition); | ||
} | ||
|
||
public Boolean fromString(String value) { | ||
return Boolean.valueOf(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package flowtimer.parsing.config; | ||
|
||
public class ConfigEntryFloat extends ConfigEntry<Float> { | ||
|
||
public ConfigEntryFloat(String name, float defaultValue, ConfigEntryLoadCallback<Float> loadCallback, ConfigEntrySaveCallback<Float> saveCallback) { | ||
this(name, defaultValue, loadCallback, saveCallback, () -> true); | ||
} | ||
|
||
public ConfigEntryFloat(String name, float defaultValue, ConfigEntryLoadCallback<Float> loadCallback, ConfigEntrySaveCallback<Float> saveCallback, ConfigEntrySaveCondition saveCondition) { | ||
super(name, defaultValue, loadCallback, saveCallback, saveCondition); | ||
} | ||
|
||
public Float fromString(String value) { | ||
return Float.valueOf(value); | ||
} | ||
} |
Oops, something went wrong.