summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/ioutils/format/CLParameters.java
blob: 5bdcbbff6b85a9cfe650cba06447dd71f71a4db6 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package bjc.utils.ioutils.format;

import java.util.ArrayList;
import java.util.List;

import bjc.utils.esodata.Tape;

/**
 * Represents a set of parameters to a CL format directive.
 *
 * @author Benjamin Culkin
 */
public class CLParameters {
	private String[] params;

	/**
	 * Create a new set of CL format parameters.
	 * 
	 * @param params
	 *        The CL format parameters to use.
	 */
	public CLParameters(String[] params) {
		this.params = params;
	}

	/**
	 * Get the length of the parameter list.
	 * 
	 * @return The length of the parameters.
	 */
	public int length() {
		return params.length;
	}

	/**
	 * Creates a set of parameters from an array of parameters.
	 *
	 * Mostly, this just fills in V and # parameters.
	 *
	 * @param params
	 *        The parameters of the directive.
	 * @param dirParams
	 *        The parameters of the format string.
	 *
	 * @return A set of CL parameters.
	 */
	public static CLParameters fromDirective(String[] params, Tape<Object> dirParams) {
		List<String> parameters = new ArrayList<>();

		for(String param : params) {
			if(param.equalsIgnoreCase("V")) {
				Object par = dirParams.item();
				boolean succ = dirParams.right();

				if(!succ) {
					throw new IllegalStateException("Couldn't advance tape for parameter");
				}

				if(par == null) {
					throw new IllegalArgumentException(
							"Expected a format parameter for V inline parameter");
				}

				if(par instanceof Number) {
					int val = ((Number) par).intValue();

					parameters.add(Integer.toString(val));
				} else if(par instanceof Character) {
					char ch = ((Character) par);

					parameters.add(Character.toString(ch));
				} else {
					throw new IllegalArgumentException(
							"Incorrect type of parameter for V inline parameter");
				}
			} else if(param.equals("#")) {
				parameters.add(Integer.toString(dirParams.position()));
			} else {
				parameters.add(param);
			}
		}

		return new CLParameters(parameters.toArray(new String[0]));
	}

	/**
	 * Get an optional character parameter with a default value.
	 * 
	 * @param idx
	 *        The index the parameter is at.
	 * @param paramName
	 *        The name of the parameter.
	 * @param directive
	 *        The directive this parameter belongs to.
	 * @param def
	 *        The default value for the parameter.
	 * @return The value of the parameter if it exists, or the default
	 *         otherwise.
	 */
	public char getCharDefault(int idx, String paramName, char directive, char def) {
		if(!params[idx].equals("")) {
			return getChar(idx, paramName, directive);
		}

		return def;
	}

	/**
	 * Get a mandatory character parameter.
	 * 
	 * @param idx
	 *        The index the parameter is at.
	 * @param paramName
	 *        The name of the parameter.
	 * @param directive
	 *        The directive this parameter belongs to.
	 * @return The value for the parameter.
	 */
	public char getChar(int idx, String paramName, char directive) {
		String param = params[idx];

		if(!param.startsWith("'")) {
			throw new IllegalArgumentException(
					String.format("Invalid %s %s to %c directive", paramName, param, directive));
		}

		return param.charAt(1);
	}
	
	/**
	 * Get an optional integer parameter with a default value.
	 * 
	 * @param idx
	 *        The index the parameter is at.
	 * @param paramName
	 *        The name of the parameter.
	 * @param directive
	 *        The directive this parameter belongs to.
	 * @param def
	 *        The default value for the parameter.
	 * @return The value of the parameter if it exists, or the default
	 *         otherwise.
	 */
	public int getIntDefault(int idx, String paramName, char directive, int def) {
		if(!params[idx].equals("")) {
			return getInt(idx, paramName, directive);
		}

		return def;
	}
	
	/**
	 * Get a mandatory integer parameter.
	 * 
	 * @param idx
	 *        The index the parameter is at.
	 * @param paramName
	 *        The name of the parameter.
	 * @param directive
	 *        The directive this parameter belongs to.
	 * @return The value for the parameter.
	 */
	public int getInt(int idx, String paramName, char directive) {
		String param = params[idx];

		try {
			return Integer.parseInt(param);
		} catch(NumberFormatException nfex) {
			String msg = String.format("Invalid %s %s to %c directive", paramName, param, directive);

			IllegalArgumentException iaex = new IllegalArgumentException(msg);
			iaex.initCause(nfex);

			throw iaex;
		}
	}
}