summaryrefslogtreecommitdiff
path: root/src/net/tuschhcm/routercontrol/Preset.java
blob: 7b64333521956a2869f1dcf6597ede50dc449b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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 file The file to load.
     * @return the Preset object
     * @throws IllegalArgumentException if the preset can't be loaded.
     */
    public static Preset loadPresetFile(File file) throws IllegalArgumentException {
        try {
            Preset preset = new Preset();

            BufferedReader in = new BufferedReader(new FileReader(file));
            int lineno = 1;

            String line = in.readLine();
            if (line == null) {
                throw new IllegalArgumentException("Empty preset file: " + file);
            }

            // 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.",
                                file, output, lineno));
                }

                line = in.readLine();
                lineno++;
            }

            in.close();

            return preset;

        } catch (final IOException e) {
            throw new IllegalArgumentException("Could not read " + file, 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() {
        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) {
        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;
    }
}