summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/net/tuschhcm/routercontrol/Preset.java119
1 files changed, 107 insertions, 12 deletions
diff --git a/src/net/tuschhcm/routercontrol/Preset.java b/src/net/tuschhcm/routercontrol/Preset.java
index 35ba8ab..ef2ba49 100644
--- a/src/net/tuschhcm/routercontrol/Preset.java
+++ b/src/net/tuschhcm/routercontrol/Preset.java
@@ -1,39 +1,134 @@
package net.tuschhcm.routercontrol;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+
/**
* Class to represent a router preset.
*/
public class Preset {
+ private static final String UNNAMED = "<Unnamed Preset>";
+
+ /**
+ * Name of the preset.
+ */
+ private String mName;
+
+ /**
+ * List of inputs for outputs.
+ * List index corresponds with the output.
+ */
+ private final ArrayList<Integer> mInputs;
/**
* Load a preset from the given file.
- *
+ *
* @param filename The file to load.
* @return the Preset object
+ * @throws IllegalArgumentException if the preset can't be loaded.
*/
- public static Preset loadPresetFile(String filename) {
- // TODO: Implement
- return null;
+ public static Preset loadPresetFile(String filename) throws IllegalArgumentException {
+ try {
+ Preset preset = new Preset();
+
+ BufferedReader in = new BufferedReader(new FileReader(filename));
+ int lineno = 1;
+
+ String line = in.readLine();
+ if (line == null) {
+ throw new IllegalArgumentException("Empty preset file: " + filename);
+ }
+
+ // The name is optional and starts with a #
+ if (line.charAt(0) == '#') {
+ preset.mName = line.substring(1).trim();
+ line = in.readLine();
+ lineno++;
+ }
+
+ // Read the inputs.
+ int output = 1;
+ while (line != null && !line.equals("")) {
+ try {
+ final Integer input = Integer.valueOf(line.trim());
+ preset.mInputs.add(input);
+ output++;
+
+ } catch (final NumberFormatException nfe) {
+ throw new IllegalArgumentException(
+ String.format("%s: Input set for output %d on line %d is not a number.",
+ filename, output, lineno));
+ }
+
+ line = in.readLine();
+ lineno++;
+ }
+
+ in.close();
+
+ return preset;
+
+ } catch (final IOException e) {
+ throw new IllegalArgumentException("Could not read " + filename, e);
+ }
}
-
+
+ /**
+ * Constructor for Preset.
+ */
+ public Preset() {
+ mName = UNNAMED;
+ mInputs = new ArrayList<Integer>();
+ }
+
/**
* Get the name of the preset.
- *
+ *
* @return The preset name
*/
public String getName() {
- // TODO: Implement
- return null;
+ return mName;
}
-
+
/**
* Get an input for the given output.
- *
+ *
* @param output
* @return Input number, or 0 if no change.
+ * @throws IllegalArgumentException if the output isn't defined.
*/
public int getInputForOutput(final int output) {
- // TODO: Implement
- return 0;
+ final int index = output - 1;
+ if (index < 0 || index > mInputs.size()) {
+ throw new IllegalArgumentException("Output out of range.");
+ }
+
+ return mInputs.get(output);
+ }
+
+ /**
+ * @return The number of outputs defined in this preset.
+ */
+ public int getNumberOfOutputs() {
+ return mInputs.size();
+ }
+
+ /**
+ * Return a string representation of this preset.
+ */
+ public String toString() {
+ String str = "";
+ if (!mName.equals(UNNAMED)) {
+ str = "# " + mName + "\n";
+ }
+
+ for (Integer in : mInputs) {
+ str += in + "\n";
+ }
+
+ return str;
}
}