summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI/ViewBot.java
blob: bc4103cdb1d325893c067e093aa35f3422afc9cf (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 * Copyright (C) 2022 Adrien Hopkins
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package sevenUnitsGUI;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import sevenUnits.unit.BaseDimension;
import sevenUnits.unit.Unit;
import sevenUnits.utils.NamedObjectProduct;
import sevenUnits.utils.ObjectProduct;

/**
 * A class that simulates a View (supports both unit and expression conversion)
 * for testing. Getters and setters work as expected.
 * 
 * @author Adrien Hopkins
 * @since 2022-01-29
 */
final class ViewBot implements UnitConversionView, ExpressionConversionView {
	/** The presenter that works with this ViewBot */
	private final Presenter presenter;
	
	/** The dimensions available to select from */
	private List<NamedObjectProduct<BaseDimension>> dimensions;
	/** The expression in the From field */
	private String fromExpression;
	/** The expression in the To field */
	private String toExpression;
	/**
	 * The user-provided string representing the value in {@code fromSelection}
	 */
	private String inputValue;
	/** The unit selected in the From selection */
	private Optional<Unit> fromSelection;
	/** The unit selected in the To selection */
	private Optional<Unit> toSelection;
	/** The currently selected dimension */
	private Optional<? extends ObjectProduct<BaseDimension>> selectedDimension;
	/** The units available in the From selection */
	private List<? extends Unit> fromUnits;
	/** The units available in the To selection */
	private List<? extends Unit> toUnits;
	
	/**
	 * Creates a new {@code ViewBot} with a new presenter.
	 *
	 * @since 2022-01-29
	 */
	public ViewBot() {
		this.presenter = new Presenter(this);
	}
	
	/**
	 * @return the available dimensions
	 * @since 2022-01-29
	 */
	@Override
	public List<NamedObjectProduct<BaseDimension>> getDimensions() {
		return this.dimensions;
	}
	
	@Override
	public String getFromExpression() {
		return this.fromExpression;
	}
	
	@Override
	public Optional<Unit> getFromSelection() {
		return this.fromSelection;
	}
	
	/**
	 * @return the units available for selection in From
	 * @since 2022-01-29
	 */
	public List<? extends Unit> getFromUnits() {
		return this.fromUnits;
	}
	
	@Override
	public String getInputValue() {
		return this.inputValue;
	}
	
	/**
	 * @return the presenter associated with tihs view
	 * @since 2022-01-29
	 */
	public Presenter getPresenter() {
		return this.presenter;
	}
	
	@Override
	public Optional<? extends ObjectProduct<BaseDimension>> getSelectedDimension() {
		return this.selectedDimension;
	}
	
	@Override
	public String getToExpression() {
		return this.toExpression;
	}
	
	@Override
	public Optional<Unit> getToSelection() {
		return this.toSelection;
	}
	
	/**
	 * @return the units available for selection in To
	 * @since 2022-01-29
	 */
	public List<? extends Unit> getToUnits() {
		return this.toUnits;
	}
	
	@Override
	public void setDimensions(
			List<NamedObjectProduct<BaseDimension>> dimensions) {
		this.dimensions = Objects.requireNonNull(dimensions,
				"dimensions may not be null");
	}
	
	/**
	 * Sets the From expression (as in {@link #getFromExpression}).
	 *
	 * @param fromExpression the expression to convert from
	 * @throws NullPointerException if {@code fromExpression} is null
	 * @since 2022-01-29
	 */
	public void setFromExpression(String fromExpression) {
		this.fromExpression = Objects.requireNonNull(fromExpression,
				"fromExpression cannot be null.");
	}
	
	/**
	 * @param fromSelection the fromSelection to set
	 * @since 2022-01-29
	 */
	public void setFromSelection(Optional<Unit> fromSelection) {
		this.fromSelection = Objects.requireNonNull(fromSelection,
				"fromSelection cannot be null");
	}
	
	@Override
	public void setFromUnits(List<? extends Unit> units) {
		this.fromUnits = Objects.requireNonNull(units, "units may not be null");
	}
	
	/**
	 * @param inputValue the inputValue to set
	 * @since 2022-01-29
	 */
	public void setInputValue(String inputValue) {
		this.inputValue = inputValue;
	}
	
	/**
	 * @param selectedDimension the selectedDimension to set
	 * @since 2022-01-29
	 */
	public void setSelectedDimension(
			Optional<? extends ObjectProduct<BaseDimension>> selectedDimension) {
		this.selectedDimension = selectedDimension;
	}
	
	/**
	 * Sets the To expression (as in {@link #getToExpression}).
	 *
	 * @param toExpression the expression to convert to
	 * @throws NullPointerException if {@code toExpression} is null
	 * @since 2022-01-29
	 */
	public void setToExpression(String toExpression) {
		this.toExpression = Objects.requireNonNull(toExpression,
				"toExpression cannot be null.");
	}
	
	/**
	 * @param toSelection the toSelection to set
	 * @since 2022-01-29
	 */
	public void setToSelection(Optional<Unit> toSelection) {
		this.toSelection = Objects.requireNonNull(toSelection,
				"toSelection cannot be null.");
	}
	
	@Override
	public void setToUnits(List<? extends Unit> units) {
		this.toUnits = Objects.requireNonNull(units, "units may not be null");
	}
	
	@Override
	public void showErrorMessage(String title, String message) {
		System.err.printf("%s: %s%n", title, message);
	}
	
	@Override
	public void showExpressionConversionOutput(String fromExpression,
			String toExpression, double value) {
		System.out.printf("Expression Conversion: %s = %d * (%s)%n",
				fromExpression, value, toExpression);
	}
	
	@Override
	public void showUnitConversionOutput(String outputString) {
		System.out.println("Unit conversion: " + outputString);
	}
	
	@Override
	public String toString() {
		return super.toString() + String.format("[presenter=%s]", this.presenter);
	}
	
}