Skip to content

Commit

Permalink
more unit refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus committed Oct 19, 2021
1 parent 6310dd1 commit ec6bac1
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 46 deletions.
24 changes: 24 additions & 0 deletions src/main/java/io/github/ititus/si/NotCommensurableException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.ititus.si;

public class NotCommensurableException extends RuntimeException {

public NotCommensurableException() {
super();
}

public NotCommensurableException(String message) {
super(message);
}

public NotCommensurableException(String message, Throwable cause) {
super(message, cause);
}

public NotCommensurableException(Throwable cause) {
super(cause);
}

protected NotCommensurableException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
22 changes: 6 additions & 16 deletions src/main/java/io/github/ititus/si/unit/AbstractUnit.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package io.github.ititus.si.unit;

import io.github.ititus.si.dimension.Dimension;
import io.github.ititus.si.quantity.type.QuantityType;

import java.util.Objects;

abstract class AbstractUnit<Q extends QuantityType<Q>> implements Unit<Q> {

private final Q type;
private final Dimension dimension;

protected AbstractUnit(Q type, Dimension dimension) {
protected AbstractUnit(Q type) {
this.type = type;
this.dimension = dimension;
}

@Override
public Q getType() {
return type;
}

@Override
public Dimension getDimension() {
return dimension;
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"type=" + type +
", dimension=" + dimension.getString() +
", dimension=" + getDimension().getString() +
", symbol=" + getSymbol() +
'}';
}
Expand All @@ -38,16 +28,16 @@ public String toString() {
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof AbstractUnit)) {
} else if (!(o instanceof Unit)) {
return false;
}

AbstractUnit<?> that = (AbstractUnit<?>) o;
return /*type.equals(that.type) &&*/ dimension.equals(that.dimension);
Unit<?> that = (Unit<?>) o;
return getDimension().equals(that.getDimension());
}

@Override
public int hashCode() {
return Objects.hash(/*type,*/ dimension);
return getDimension().hashCode();
}
}
7 changes: 5 additions & 2 deletions src/main/java/io/github/ititus/si/unit/AlternateUnit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.ititus.si.unit;

import io.github.ititus.si.NotCommensurableException;
import io.github.ititus.si.prefix.Prefix;
import io.github.ititus.si.quantity.type.QuantityType;
import io.github.ititus.si.quantity.value.QuantityValue;
Expand All @@ -13,7 +14,7 @@ final class AlternateUnit<Q extends QuantityType<Q>> extends AbstractUnit<Q> {
private final String alternateSymbol;

AlternateUnit(Unit<Q> baseUnit, String alternateSymbol) {
super(baseUnit.getType(), baseUnit.getDimension());
super(baseUnit.getType());
this.baseUnit = baseUnit;
this.alternateSymbol = alternateSymbol;
}
Expand All @@ -37,7 +38,9 @@ public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
@Override
@SuppressWarnings("unchecked")
public <T extends QuantityType<T>> Unit<T> as(T type) {
if (getType().equals(type)) {
if (!isCommensurableWith(type)) {
throw new NotCommensurableException();
} else if (getType().equals(type)) {
return (Unit<T>) this;
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/github/ititus/si/unit/BaseUnit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.ititus.si.unit;

import io.github.ititus.si.NotCommensurableException;
import io.github.ititus.si.prefix.Prefix;
import io.github.ititus.si.quantity.type.QuantityType;
import io.github.ititus.si.quantity.value.QuantityValue;
Expand All @@ -12,7 +13,7 @@ final class BaseUnit<Q extends QuantityType<Q>> extends AbstractUnit<Q> {
private final String symbol;

BaseUnit(Q type, String symbol) {
super(type, type.getDimension());
super(type);
this.symbol = symbol;
}

Expand All @@ -24,7 +25,7 @@ public String getSymbol() {
@Override
public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
if (!isCommensurableWith(unit.getType())) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (equals(unit)) {
return UnitConverter.IDENTITY;
}
Expand All @@ -36,7 +37,7 @@ public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
@SuppressWarnings("unchecked")
public <T extends QuantityType<T>> Unit<T> as(T type) {
if (!isCommensurableWith(type)) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (getType().equals(type)) {
return (Unit<T>) this;
}
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/io/github/ititus/si/unit/CompoundUnit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.ititus.si.unit;

import io.github.ititus.si.NotCommensurableException;
import io.github.ititus.si.dimension.Dimension;
import io.github.ititus.si.prefix.Prefix;
import io.github.ititus.si.quantity.type.QuantityType;
Expand All @@ -19,9 +20,11 @@
final class CompoundUnit<Q extends QuantityType<Q>> extends AbstractUnit<Q> {

private final Map<Unit<?>, Integer> units;
private final Dimension dimension;

private CompoundUnit(Q type, Dimension dimension, Map<Unit<?>, Integer> units) {
super(type, dimension);
super(type);
this.dimension = dimension;
this.units = units;
}

Expand All @@ -33,10 +36,8 @@ private static Unit<?> of(Dimension dimension, Map<Unit<?>, Integer> units) {

if (units.isEmpty()) {
return ONE;
}

if (units.size() == 1) {
Map.Entry<Unit<?>, Integer> entry = units.entrySet().stream().findAny().get();
} else if (units.size() == 1) {
Map.Entry<Unit<?>, Integer> entry = units.entrySet().stream().findAny().orElseThrow();
if (entry.getValue() == 1) {
return entry.getKey();
}
Expand Down Expand Up @@ -100,6 +101,11 @@ static Unit<?> ofRoot(Unit<?> u, int root) {
return of(u.getDimension().root(root), units);
}

@Override
public Dimension getDimension() {
return dimension;
}

@Override
public String getSymbol() {
StringBuilder b = new StringBuilder();
Expand All @@ -118,7 +124,7 @@ public String getSymbol() {
@Override
public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
if (!isCommensurableWith(unit.getType())) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (equals(unit)) {
return UnitConverter.IDENTITY;
}
Expand Down Expand Up @@ -154,12 +160,12 @@ public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
@SuppressWarnings("unchecked")
public <T extends QuantityType<T>> Unit<T> as(T type) {
if (!isCommensurableWith(type)) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (getType().equals(type)) {
return (Unit<T>) this;
}

return new CompoundUnit<>(type, getDimension(), units);
return new CompoundUnit<>(type, dimension, units);
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/github/ititus/si/unit/ConvertedUnit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.ititus.si.unit;

import io.github.ititus.si.NotCommensurableException;
import io.github.ititus.si.prefix.Prefix;
import io.github.ititus.si.quantity.type.QuantityType;
import io.github.ititus.si.quantity.value.QuantityValue;
Expand All @@ -13,7 +14,7 @@ final class ConvertedUnit<Q extends QuantityType<Q>> extends AbstractUnit<Q> {
private final UnitConverter converter;

private ConvertedUnit(Unit<Q> baseUnit, UnitConverter converter) {
super(baseUnit.getType(), baseUnit.getDimension());
super(baseUnit.getType());
this.baseUnit = baseUnit;
this.converter = converter;
}
Expand All @@ -34,7 +35,7 @@ public String getSymbol() {
@Override
public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
if (!isCommensurableWith(unit.getType())) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (equals(unit)) {
return UnitConverter.IDENTITY;
} else if (baseUnit.equals(unit)) {
Expand All @@ -48,7 +49,7 @@ public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
@SuppressWarnings("unchecked")
public <T extends QuantityType<T>> Unit<T> as(T type) {
if (!isCommensurableWith(type)) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (getType().equals(type)) {
return (Unit<T>) this;
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/io/github/ititus/si/unit/PrefixUnit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ititus.si.unit;

import io.github.ititus.math.number.BigRational;
import io.github.ititus.si.NotCommensurableException;
import io.github.ititus.si.prefix.Prefix;
import io.github.ititus.si.quantity.type.QuantityType;
import io.github.ititus.si.quantity.value.QuantityValue;
Expand All @@ -14,7 +15,7 @@ final class PrefixUnit<Q extends QuantityType<Q>> extends AbstractUnit<Q> {
private final Prefix prefix;

PrefixUnit(Unit<Q> baseUnit, Prefix prefix) {
super(baseUnit.getType(), baseUnit.getDimension());
super(baseUnit.getType());
this.baseUnit = baseUnit;
this.prefix = prefix;
}
Expand All @@ -27,7 +28,7 @@ public String getSymbol() {
@Override
public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
if (!isCommensurableWith(unit.getType())) {
throw new ClassCastException();
throw new NotCommensurableException();
} else if (equals(unit)) {
return UnitConverter.IDENTITY;
}
Expand All @@ -51,7 +52,9 @@ public <T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit) {
@Override
@SuppressWarnings("unchecked")
public <T extends QuantityType<T>> Unit<T> as(T type) {
if (getType().equals(type)) {
if (!isCommensurableWith(type)) {
throw new NotCommensurableException();
} else if (getType().equals(type)) {
return (Unit<T>) this;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/github/ititus/si/unit/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public interface Unit<Q extends QuantityType<Q>> {

Q getType();

Dimension getDimension();
default Dimension getDimension() {
return getType().getDimension();
}

<T extends QuantityType<T>> UnitConverter getConverterTo(Unit<T> unit);

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/github/ititus/si/unit/Units.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.ititus.si.unit;

import io.github.ititus.internal.Config;
import io.github.ititus.si.quantity.type.*;

import static io.github.ititus.math.number.BigRational.of;
Expand Down Expand Up @@ -69,6 +70,7 @@ public final class Units {
// Length
public static final Unit<Length> KILOMETRE = METRE.prefix(KILO);
public static final Unit<Length> DECIMETRE = METRE.prefix(DECI);
public static final Unit<Length> CENTIMETRE = METRE.prefix(CENTI);
public static final Unit<Length> MILLIMETRE = METRE.prefix(MILLI);
public static final Unit<Length> INCH = MILLIMETRE.multiply(ofExp(254, -1)).alternate("in");
public static final Unit<Length> FOOT = INCH.multiply(12).alternate("ft");
Expand Down Expand Up @@ -170,7 +172,7 @@ public final class Units {
public static final Unit<ElectricCapacitance> FARAD = COULOMB.divide(VOLT).alternate("F").as(ELECTRIC_CAPACITANCE);

// Electric Resistance
public static final Unit<ElectricResistance> OHM = VOLT.divide(AMPERE).alternate("\u03A9").as(ELECTRIC_RESISTANCE);
public static final Unit<ElectricResistance> OHM = VOLT.divide(AMPERE).alternate(Config.FORCE_ASCII ? "Ohm" : "\u03A9").as(ELECTRIC_RESISTANCE);

// Electric Conductance
public static final Unit<ElectricConductance> SIEMENS = OHM.inverse().alternate("S").as(ELECTRIC_CONDUCTANCE);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
exports io.github.ititus.math.vector;
exports io.github.ititus.parser;
exports io.github.ititus.precondition;
exports io.github.ititus.si;
exports io.github.ititus.si.dimension;
exports io.github.ititus.si.prefix;
exports io.github.ititus.si.quantity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ititus.si.quantity.type;
package io.github.ititus.si;

import io.github.ititus.si.quantity.Quantity;
import io.github.ititus.si.quantity.type.*;
import org.junit.jupiter.api.Test;

import static io.github.ititus.si.quantity.type.Force.FORCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ititus.si.quantity.type;
package io.github.ititus.si;

import io.github.ititus.si.quantity.Quantity;
import io.github.ititus.si.quantity.type.Length;
import org.junit.jupiter.api.Test;

import static io.github.ititus.assertions.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ititus.si.quantity.type;
package io.github.ititus.si;

import io.github.ititus.si.quantity.Quantity;
import io.github.ititus.si.quantity.type.Mass;
import org.junit.jupiter.api.Test;

import static io.github.ititus.assertions.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ititus.si.quantity.type;
package io.github.ititus.si;

import io.github.ititus.si.quantity.Quantity;
import io.github.ititus.si.quantity.type.Temperature;
import org.junit.jupiter.api.Test;

import static io.github.ititus.assertions.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.github.ititus.si.quantity.type;
package io.github.ititus.si;

import io.github.ititus.si.quantity.Quantity;
import io.github.ititus.si.quantity.type.Time;
import org.junit.jupiter.api.Test;

import static io.github.ititus.assertions.Assertions.assertThat;
import static io.github.ititus.math.number.BigRational.ofExp;
import static io.github.ititus.si.prefix.MetricPrefix.NANO;
import static io.github.ititus.si.quantity.value.QuantityValue.of;
import static io.github.ititus.si.unit.Units.*;
import static org.assertj.core.api.Assertions.assertThat;
import static io.github.ititus.si.unit.Units.MINUTE;
import static io.github.ititus.si.unit.Units.SECOND;
import static org.assertj.core.data.Percentage.withPercentage;

class TimeTest {
Expand Down

0 comments on commit ec6bac1

Please sign in to comment.