From 28c861db52484eefa37bd0ef795b9329aa8b0290 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 25 Jan 2019 12:30:52 -0500 Subject: Initial commit --- src/unitConverter/unit/Unit.java | 103 +++++++++++++++++++++++++++++++ src/unitConverter/unit/UnitSystem.java | 31 ++++++++++ src/unitConverter/unit/package-info.java | 23 +++++++ 3 files changed, 157 insertions(+) create mode 100755 src/unitConverter/unit/Unit.java create mode 100755 src/unitConverter/unit/UnitSystem.java create mode 100644 src/unitConverter/unit/package-info.java (limited to 'src/unitConverter/unit') diff --git a/src/unitConverter/unit/Unit.java b/src/unitConverter/unit/Unit.java new file mode 100755 index 0000000..3e7f9da --- /dev/null +++ b/src/unitConverter/unit/Unit.java @@ -0,0 +1,103 @@ +/** + * Copyright (C) 2018 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 unitConverter.unit; + +import java.util.Objects; + +import unitConverter.dimension.UnitDimension; + +/** + * A unit that has an associated base unit, and can convert a value expressed in it to and from that base. + * + * @author Adrien Hopkins + * @since 2018-12-22 + */ +public interface Unit { + /** + * Checks if a value expressed in this unit can be converted to a value expressed in {@code other} + * + * @param other + * unit to test with + * @return true if the units are compatible + * @since 2019-01-13 + */ + default boolean canConvertTo(final Unit other) { + return Objects.equals(this.getBase(), other.getBase()); + } + + /** + * Converts from a value expressed in this unit's base unit to a value expressed in this unit. + *

+ * This must be the inverse of {@code convertToBase}, so {@code convertFromBase(convertToBase(value))} must be equal + * to {@code value} for any value, ignoring precision loss by roundoff error. + *

+ *

+ * If this unit is a base unit, this method should return {@code value}. + *

+ * + * @param value + * value expressed in base unit + * @return value expressed in this unit + * @since 2018-12-22 + */ + double convertFromBase(double value); + + /** + * Converts from a value expressed in this unit to a value expressed in this unit's base unit. + *

+ * This must be the inverse of {@code convertFromBase}, so {@code convertToBase(convertFromBase(value))} must be + * equal to {@code value} for any value, ignoring precision loss by roundoff error. + *

+ *

+ * If this unit is a base unit, this method should return {@code value}. + *

+ * + * @param value + * value expressed in this unit + * @return value expressed in base unit + * @since 2018-12-22 + */ + double convertToBase(double value); + + /** + *

+ * Returns the base unit associated with this unit. + *

+ *

+ * The dimension of this unit must be equal to the dimension of the returned unit. + *

+ *

+ * If this unit is a base unit, this method should return this unit.\ + *

+ * + * @return base unit associated with this unit + * @since 2018-12-22 + */ + Unit getBase(); + + /** + * @return dimension measured by this unit + * @since 2018-12-22 + */ + UnitDimension getDimension(); + + /** + * @return system that this unit is a part of + * @since 2018-12-23 + */ + UnitSystem getSystem(); +} diff --git a/src/unitConverter/unit/UnitSystem.java b/src/unitConverter/unit/UnitSystem.java new file mode 100755 index 0000000..2e3a5d8 --- /dev/null +++ b/src/unitConverter/unit/UnitSystem.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2018 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 unitConverter.unit; + +/** + * A system of units. Each unit should be aware of its system. + * + * @author Adrien Hopkins + * @since 2018-12-23 + */ +public interface UnitSystem { + /** + * @return name of system + * @since 2019-01-25 + */ + String getName(); +} diff --git a/src/unitConverter/unit/package-info.java b/src/unitConverter/unit/package-info.java new file mode 100644 index 0000000..b5deb0c --- /dev/null +++ b/src/unitConverter/unit/package-info.java @@ -0,0 +1,23 @@ +/** + * Copyright (C) 2019 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 . + */ +/** + * All of the classes that correspond to the units being converted. + * + * @author Adrien Hopkins + * @since 2019-01-25 + */ +package unitConverter.unit; \ No newline at end of file -- cgit v1.2.3