Skip to content

Commit

Permalink
Merge pull request #553 from raynigon/feature/add-binary-units
Browse files Browse the repository at this point in the history
Implement binary units
  • Loading branch information
raynigon authored Aug 13, 2024
2 parents b1f2c0e + c850500 commit 9bc3e9b
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

import com.raynigon.unit.api.core.quantities.Torque;
import com.raynigon.unit.api.core.units.si.area.SquareMetre;
import com.raynigon.unit.api.core.units.si.binary.Bit;
import com.raynigon.unit.api.core.units.si.binary.Byte;
import com.raynigon.unit.api.core.units.si.binary.Gibibyte;
import com.raynigon.unit.api.core.units.si.binary.Gigabyte;
import com.raynigon.unit.api.core.units.si.binary.Kibibyte;
import com.raynigon.unit.api.core.units.si.binary.Kilobyte;
import com.raynigon.unit.api.core.units.si.binary.Mebibyte;
import com.raynigon.unit.api.core.units.si.binary.Megabyte;
import com.raynigon.unit.api.core.units.si.binary.Tebibyte;
import com.raynigon.unit.api.core.units.si.binary.Terabyte;
import com.raynigon.unit.api.core.units.si.mass.Kilogram;
import com.raynigon.unit.api.core.units.si.acceleration.MetrePerSquaredSecond;
import com.raynigon.unit.api.core.units.si.dimensionless.One;
Expand Down Expand Up @@ -63,19 +73,27 @@ public class SISystemUnitsConstants {

public static final Ampere Ampere = new Ampere();
public static final AmpereHour AmpereHour = new AmpereHour();
public static final Bit Bit = new Bit();
public static final Byte Byte = new Byte();
public static final Celsius Celsius = new Celsius();
public static final Centimetre Centimetre = new Centimetre();
public static final Coulomb Coulomb = new Coulomb();
public static final CubicMetre CubicMetre = new CubicMetre();
public static final Gibibyte Gibibyte = new Gibibyte();
public static final Gigabyte Gigabyte = new Gigabyte();
public static final Hertz Hertz = new Hertz();
public static final Hour Hour = new Hour();
public static final Joule Joule = new Joule();
public static final Kelvin Kelvin = new Kelvin();
public static final Kibibyte Kibibyte = new Kibibyte();
public static final KiloWattHour KiloWattHour = new KiloWattHour();
public static final Kilobyte Kilobyte = new Kilobyte();
public static final Kilogram Kilogram = new Kilogram();
public static final Kilometre Kilometre = new Kilometre();
public static final KilometrePerHour KilometrePerHour = new KilometrePerHour();
public static final Litre Litre = new Litre();
public static final Mebibyte Mebibyte = new Mebibyte();
public static final Megabyte Megabyte = new Megabyte();
public static final Metre Metre = new Metre();
public static final MetrePerSecond MetrePerSecond = new MetrePerSecond();
public static final MetrePerSquaredSecond MetrePerSquaredSecond = new MetrePerSquaredSecond();
Expand All @@ -94,6 +112,8 @@ public class SISystemUnitsConstants {
public static final Percent Percent = new Percent();
public static final Second Second = new Second();
public static final SquareMetre SquareMetre = new SquareMetre();
public static final Tebibyte Tebibyte = new Tebibyte();
public static final Terabyte Terabyte = new Terabyte();
public static final Volt Volt = new Volt();
public static final Watt Watt = new Watt();
public static final WattHour WattHour = new WattHour();
Expand Down Expand Up @@ -423,4 +443,94 @@ public static Quantity<Acceleration> MetrePerSquaredSecond(Number value) {
public static @NotNull Quantity<Volume> Litres(@NotNull Number value) {
return quantity(value, Litre);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Byte(@NotNull Number value) {
return quantity(value, Byte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Bytes(@NotNull Number value) {
return quantity(value, Byte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Kilobyte(@NotNull Number value) {
return quantity(value, Kilobyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Kilobytes(@NotNull Number value) {
return quantity(value, Kilobyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Megabyte(@NotNull Number value) {
return quantity(value, Megabyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Megabytes(@NotNull Number value) {
return quantity(value, Megabyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Gigabyte(@NotNull Number value) {
return quantity(value, Gigabyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Gigabytes(@NotNull Number value) {
return quantity(value, Gigabyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Terabyte(@NotNull Number value) {
return quantity(value, Terabyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Terabytes(@NotNull Number value) {
return quantity(value, Terabyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Kibibyte(@NotNull Number value) {
return quantity(value, Kibibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Kibibytes(@NotNull Number value) {
return quantity(value, Kibibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Mebibyte(@NotNull Number value) {
return quantity(value, Mebibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Mebibytes(@NotNull Number value) {
return quantity(value, Mebibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Gibibyte(@NotNull Number value) {
return quantity(value, Gibibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Gibibytes(@NotNull Number value) {
return quantity(value, Gibibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Tebibyte(@NotNull Number value) {
return quantity(value, Tebibyte);
}

@SuppressWarnings("PMD.MethodNamingConventions")
public static @NotNull Quantity<Dimensionless> Tebibytes(@NotNull Number value) {
return quantity(value, Tebibyte);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.function.unitconverter.MultiplyConverter;
import com.raynigon.unit.api.core.units.general.TransformedUnit;

import javax.measure.quantity.Dimensionless;

public class Bit extends TransformedUnit<Dimensionless> {
public Bit() {
super("bit", "Bit", new Byte(), MultiplyConverter.ofRational(1, 8));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.BaseUnit;
import com.raynigon.unit.api.core.units.general.UnitDimension;
import com.raynigon.unit.api.core.units.si.SISystem;

import javax.measure.quantity.Dimensionless;

public class Byte extends BaseUnit<Dimensionless> {
public Byte() {
super(SISystem.ID, "B", "Byte", Dimensionless.class, UnitDimension.NONE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.BinaryPrefix;
import javax.measure.quantity.Dimensionless;

public class Gibibyte extends ScaledUnit<Dimensionless> {
public Gibibyte() {
super(BinaryPrefix.GIBI, new Byte(), "Gibibyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.MetricPrefix;
import javax.measure.quantity.Dimensionless;

public class Gigabyte extends ScaledUnit<Dimensionless> {
public Gigabyte() {
super(MetricPrefix.GIGA, new Byte(), "Gigabyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.BinaryPrefix;
import javax.measure.quantity.Dimensionless;

public class Kibibyte extends ScaledUnit<Dimensionless> {
public Kibibyte() {
super(BinaryPrefix.KIBI, new Byte(), "Kibibyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.MetricPrefix;
import javax.measure.quantity.Dimensionless;

public class Kilobyte extends ScaledUnit<Dimensionless> {
public Kilobyte() {
super(MetricPrefix.KILO, new Byte(), "Kilobyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.BinaryPrefix;
import javax.measure.quantity.Dimensionless;

public class Mebibyte extends ScaledUnit<Dimensionless> {
public Mebibyte() {
super(BinaryPrefix.MEBI, new Byte(), "Mebibyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.MetricPrefix;
import javax.measure.quantity.Dimensionless;

public class Megabyte extends ScaledUnit<Dimensionless> {
public Megabyte() {
super(MetricPrefix.MEGA, new Byte(), "Megabyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.BinaryPrefix;
import javax.measure.quantity.Dimensionless;

public class Tebibyte extends ScaledUnit<Dimensionless> {
public Tebibyte() {
super(BinaryPrefix.TEBI, new Byte(), "Tebibyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.raynigon.unit.api.core.units.si.binary;

import com.raynigon.unit.api.core.units.general.ScaledUnit;

import javax.measure.MetricPrefix;
import javax.measure.quantity.Dimensionless;

public class Terabyte extends ScaledUnit<Dimensionless> {
public Terabyte() {
super(MetricPrefix.TERA, new Byte(), "Terabyte");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.raynigon.unit.api.core.units.si.binary

import com.raynigon.unit.api.core.units.si.SISystemUnitsConstants
import spock.lang.Specification

class ByteSpec extends Specification {
def "Bytes are 8 Bits"() {
given:
def bytes = SISystemUnitsConstants.Bytes(2)

when:
def bits = bytes.to(SISystemUnitsConstants.Bit)

then:
bytes.value * 8 == bits.value
}

def "Kilobytes and Kibibytes are different"() {
when:
def kilo = SISystemUnitsConstants.Kilobytes(2)
def kibi = SISystemUnitsConstants.Kibibytes(2)

then:
kilo < kibi
}
}

0 comments on commit 9bc3e9b

Please sign in to comment.