/**
* 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 .
*/
package sevenUnitsGUI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.Set;
import sevenUnits.unit.BaseDimension;
import sevenUnits.unit.Unit;
import sevenUnits.unit.UnitValue;
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 Set> 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 OptionalDouble inputValue;
/** The unit selected in the From selection */
private Optional fromSelection;
/** The unit selected in the To selection */
private Optional toSelection;
/** The currently selected dimension */
private Optional extends ObjectProduct> selectedDimension;
/** The units available in the From selection */
private Set extends Unit> fromUnits;
/** The units available in the To selection */
private Set extends Unit> toUnits;
/** Saved input values of all unit conversions */
private final List unitConversionInputValues;
/** Saved output values of all unit conversions */
private final List unitConversionOutputValues;
/** Saved outputs of all unit expressions */
private final List expressionConversionOutputs;
/**
* Creates a new {@code ViewBot} with a new presenter.
*
* @since 2022-01-29
*/
public ViewBot() {
this.presenter = new Presenter(this);
this.unitConversionInputValues = new ArrayList<>();
this.unitConversionOutputValues = new ArrayList<>();
this.expressionConversionOutputs = new ArrayList<>();
}
/**
* @return the available dimensions
* @since 2022-01-29
*/
@Override
public Set> getDimensions() {
return this.dimensions;
}
public List getExpressionConversionOutputs() {
return this.expressionConversionOutputs;
}
@Override
public String getFromExpression() {
return this.fromExpression;
}
@Override
public Optional getFromSelection() {
return this.fromSelection;
}
/**
* @return the units available for selection in From
* @since 2022-01-29
*/
public Set getFromUnits() {
return Collections.unmodifiableSet(this.fromUnits);
}
@Override
public OptionalDouble getInputValue() {
return this.inputValue;
}
/**
* @return the presenter associated with tihs view
* @since 2022-01-29
*/
public Presenter getPresenter() {
return this.presenter;
}
@Override
public Optional> getSelectedDimension() {
return this.selectedDimension.map(x -> x);
}
@Override
public String getToExpression() {
return this.toExpression;
}
@Override
public Optional getToSelection() {
return this.toSelection;
}
/**
* @return the units available for selection in To
* @since 2022-01-29
*/
public Set getToUnits() {
return Collections.unmodifiableSet(this.toUnits);
}
/**
* @return the unitConversionInputValues
* @since 2022-02-26
*/
public List getUnitConversionInputValues() {
return this.unitConversionInputValues;
}
/**
* @return the unitConversionOutputValues
* @since 2022-02-10
*/
public List getUnitConversionOutputValues() {
return this.unitConversionOutputValues;
}
@Override
public void setDimensions(
Set> 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 fromSelection) {
this.fromSelection = Objects.requireNonNull(fromSelection,
"fromSelection cannot be null");
}
/**
* @param fromSelection the fromSelection to set
* @since 2022-02-10
*/
public void setFromSelection(Unit fromSelection) {
this.setFromSelection(Optional.of(fromSelection));
}
@Override
public void setFromUnits(Set 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(OptionalDouble inputValue) {
this.inputValue = inputValue;
}
public void setSelectedDimension(
ObjectProduct selectedDimension) {
this.setSelectedDimension(Optional.of(selectedDimension));
}
/**
* @param selectedDimension the selectedDimension to set
* @since 2022-01-29
*/
public void setSelectedDimension(
Optional extends ObjectProduct> 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 toSelection) {
this.toSelection = Objects.requireNonNull(toSelection,
"toSelection cannot be null.");
}
public void setToSelection(Unit toSelection) {
this.setToSelection(Optional.of(toSelection));
}
@Override
public void setToUnits(Set 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) {
final String output = String.format("%s = %s %s", fromExpression, value,
toExpression);
this.expressionConversionOutputs.add(output);
System.out.println("Expression Conversion: " + output);
}
@Override
public void showUnitConversionOutput(UnitValue input, UnitValue output) {
this.unitConversionInputValues.add(input);
this.unitConversionOutputValues.add(output);
System.out.println("Unit conversion: " + input + " = " + output);
}
@Override
public String toString() {
return super.toString() + String.format("[presenter=%s]", this.presenter);
}
}