diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java index af18519..3ef57ce 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java @@ -12,6 +12,7 @@ import lombok.Getter; import lombok.Singular; import java.lang.reflect.Field; +import java.math.BigInteger; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Function; @@ -99,7 +100,11 @@ public class AssetProperty { .setter((obj, value) -> { try { property.setAccessible(true); - property.set(obj, value); + if (Long.class.isAssignableFrom(property.getType()) && value instanceof BigInteger bigInteger) { + property.set(obj, bigInteger.longValue()); + } else { + property.set(obj, value); + } } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -209,7 +214,7 @@ public class AssetProperty { } else if (type == Type.INTEGER || type == Type.STRING) { return value.toString(); } else if (type == Type.CAPACITY) { - return String.format("%s bytes", value); + return convertCapacity(value).toString(); } else if (type.isEnum) { if (value instanceof AssetEnum assetEnum) { return assetEnum.getDisplayName(); @@ -220,6 +225,38 @@ public class AssetProperty { } } + public CapacityInfo convertCapacityBigInt(BigInteger value) { + if (type != Type.CAPACITY) { + throw new IllegalStateException("Property '" + name + "' is not a capacity type."); + } + return CapacityInfo.of(value, capacityAsIEC, capacityAsSI); + } + + public CapacityInfo convertCapacityLong(Long value) { + if (type != Type.CAPACITY) { + throw new IllegalStateException("Property '" + name + "' is not a capacity type."); + } + return CapacityInfo.of(BigInteger.valueOf(value), capacityAsIEC, capacityAsSI); + } + + public CapacityInfo convertCapacity(Object value) { + if (value == null) { + return null; + } else if (value instanceof BigInteger bi) { + return convertCapacityBigInt(bi); + } else { + return convertCapacityLong((Long) value); + } + } + + public CapacityInfo asCapacity(@Nullable Object object) { + var value = getValue(object); + if (value == null) { + return null; + } + return convertCapacity(value); + } + @Override public String toString() { var enumOptions = ""; diff --git a/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java b/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java new file mode 100644 index 0000000..11c4c1b --- /dev/null +++ b/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java @@ -0,0 +1,59 @@ +package be.seeseepuff.pcinv.meta; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Comparator; + +/** + * Represents a capacity in bytes, with various units for display. + * This class is used to encapsulate the capacity information and provide + * a way to represent it in different units. + */ +@Getter +@RequiredArgsConstructor +public class CapacityInfo { + private final BigInteger capacity; + private final CapacityUnit idealUnit; + + public static CapacityInfo of(BigInteger capacity, CapacityUnit idealUnit) { + return new CapacityInfo(capacity, idealUnit); + } + + public static CapacityInfo of(BigInteger capacity) { + return of(capacity, idealUnitForCapacity(capacity, CapacityUnit.values())); + } + + public static CapacityInfo ofSI(BigInteger capacity) { + return of(capacity, idealUnitForCapacity(capacity, CapacityUnit.SI_UNITS)); + } + + public static CapacityInfo ofIEC(BigInteger capacity) { + return of(capacity, idealUnitForCapacity(capacity, CapacityUnit.IEC_UNITS)); + } + + public static CapacityInfo of(BigInteger capacity, boolean iec, boolean si) { + if (iec && !si) { + return ofIEC(capacity); + } else if (si && !iec) { + return ofSI(capacity); + } else { + return of(capacity); + } + } + + public static CapacityUnit idealUnitForCapacity(BigInteger capacity, CapacityUnit[] units) { + return Arrays.stream(units) + .sorted(Comparator.comparing(CapacityUnit::getBytes).reversed()) + .filter(unit -> capacity.mod(unit.getBytes()).equals(BigInteger.ZERO)) + .findFirst() + .orElse(CapacityUnit.BYTES); + } + + @Override + public String toString() { + return String.format("%d %s", capacity.divide(idealUnit.getBytes()), idealUnit.getDisplayName()); + } +} diff --git a/src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java b/src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java new file mode 100644 index 0000000..9c7a426 --- /dev/null +++ b/src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java @@ -0,0 +1,32 @@ +package be.seeseepuff.pcinv.meta; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.math.BigInteger; + +/** + * Returns the capacity in bytes. + * + * @return the capacity in bytes + */ +@Getter +@RequiredArgsConstructor +public enum CapacityUnit { + BYTES("Bytes", BigInteger.valueOf(1)), + KIBIBYTES("KiB", BigInteger.valueOf(1024)), + MEBIBYTES("MiB", BigInteger.valueOf(1024 * 1024)), + GIBIBYTES("GiB", BigInteger.valueOf(1024 * 1024 * 1024)), + TEBIBYTES("TiB", BigInteger.valueOf(1024L * 1024 * 1024 * 1024)), + KILOBYTES("kB", BigInteger.valueOf(1000)), + MEGABYTES("MB", BigInteger.valueOf(1000 * 1000)), + GIGABYTES("GB", BigInteger.valueOf(1000 * 1000 * 1000)), + TERABYTES("TB", BigInteger.valueOf(1000L * 1000 * 1000 * 1000)), + ; + + public static final CapacityUnit[] SI_UNITS = {BYTES, KILOBYTES, MEGABYTES, GIGABYTES, TERABYTES}; + public static final CapacityUnit[] IEC_UNITS = {BYTES, KIBIBYTES, MEBIBYTES, GIBIBYTES, TEBIBYTES}; + + private final String displayName; + private final BigInteger bytes; +} diff --git a/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java b/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java index 9182b68..440f972 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java @@ -5,6 +5,8 @@ import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; +import java.math.BigInteger; + /** * Represents a RAM DIMM or similar memory asset in the inventory system. */ @@ -28,7 +30,7 @@ public class RamAsset implements Asset @Property("Capacity") @Capacity - private Long capacity; + private BigInteger capacity; @Description("The type of memory. E.g.: DDR2, SDRAM, ISA-8, etc...") @Property("Type") diff --git a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java index 022cf13..e037e52 100644 --- a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java +++ b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java @@ -15,6 +15,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.math.BigInteger; import java.time.ZonedDateTime; import java.util.*; @@ -207,7 +208,7 @@ public class AssetService { } try { - return Long.parseLong(value) * Long.parseLong(unit); + return new BigInteger(value).multiply(new BigInteger(unit)); } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid numeric value for property '" + property.getName() + "': " + value, e); } diff --git a/src/main/resources/templates/create_asset.html b/src/main/resources/templates/create_asset.html index 0ac1fce..667f3fc 100644 --- a/src/main/resources/templates/create_asset.html +++ b/src/main/resources/templates/create_asset.html @@ -31,17 +31,17 @@ - + Bad input type for diff --git a/src/main/resources/templates/view.html b/src/main/resources/templates/view.html index e2caeed..bcd3acd 100644 --- a/src/main/resources/templates/view.html +++ b/src/main/resources/templates/view.html @@ -15,6 +15,7 @@