From 5d1c65a4c5c0174f9e188396983358defaf0e052 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Mon, 9 Jun 2025 15:03:16 +0200 Subject: [PATCH] Better capacity support --- .../seeseepuff/pcinv/meta/AssetProperty.java | 33 ++++--------------- .../seeseepuff/pcinv/meta/CapacityInfo.java | 23 +++++++------ .../seeseepuff/pcinv/meta/CapacityUnit.java | 26 +++++++-------- .../be/seeseepuff/pcinv/models/RamAsset.java | 6 ++-- .../pcinv/services/AssetService.java | 3 +- .../resources/templates/create_asset.html | 2 +- 6 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java index 3ef57ce..3ec9cff 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java @@ -12,7 +12,6 @@ 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; @@ -100,11 +99,7 @@ public class AssetProperty { .setter((obj, value) -> { try { property.setAccessible(true); - if (Long.class.isAssignableFrom(property.getType()) && value instanceof BigInteger bigInteger) { - property.set(obj, bigInteger.longValue()); - } else { - property.set(obj, value); - } + property.set(obj, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -214,7 +209,7 @@ public class AssetProperty { } else if (type == Type.INTEGER || type == Type.STRING) { return value.toString(); } else if (type == Type.CAPACITY) { - return convertCapacity(value).toString(); + return convertCapacity((Long) value).toString(); } else if (type.isEnum) { if (value instanceof AssetEnum assetEnum) { return assetEnum.getDisplayName(); @@ -225,36 +220,22 @@ public class AssetProperty { } } - public CapacityInfo convertCapacityBigInt(BigInteger value) { + public CapacityInfo convertCapacity(Long value) { + if (value == null) { + return null; + } 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); + return convertCapacity((Long) value); } @Override diff --git a/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java b/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java index 11c4c1b..71c9b09 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java @@ -3,7 +3,6 @@ package be.seeseepuff.pcinv.meta; import lombok.Getter; import lombok.RequiredArgsConstructor; -import java.math.BigInteger; import java.util.Arrays; import java.util.Comparator; @@ -15,26 +14,26 @@ import java.util.Comparator; @Getter @RequiredArgsConstructor public class CapacityInfo { - private final BigInteger capacity; + private final long capacity; private final CapacityUnit idealUnit; - public static CapacityInfo of(BigInteger capacity, CapacityUnit idealUnit) { + public static CapacityInfo of(long capacity, CapacityUnit idealUnit) { return new CapacityInfo(capacity, idealUnit); } - public static CapacityInfo of(BigInteger capacity) { + public static CapacityInfo of(long capacity) { return of(capacity, idealUnitForCapacity(capacity, CapacityUnit.values())); } - public static CapacityInfo ofSI(BigInteger capacity) { + public static CapacityInfo ofSI(long capacity) { return of(capacity, idealUnitForCapacity(capacity, CapacityUnit.SI_UNITS)); } - public static CapacityInfo ofIEC(BigInteger capacity) { + public static CapacityInfo ofIEC(long capacity) { return of(capacity, idealUnitForCapacity(capacity, CapacityUnit.IEC_UNITS)); } - public static CapacityInfo of(BigInteger capacity, boolean iec, boolean si) { + public static CapacityInfo of(long capacity, boolean iec, boolean si) { if (iec && !si) { return ofIEC(capacity); } else if (si && !iec) { @@ -44,16 +43,20 @@ public class CapacityInfo { } } - public static CapacityUnit idealUnitForCapacity(BigInteger capacity, CapacityUnit[] units) { + public static CapacityUnit idealUnitForCapacity(long capacity, CapacityUnit[] units) { return Arrays.stream(units) .sorted(Comparator.comparing(CapacityUnit::getBytes).reversed()) - .filter(unit -> capacity.mod(unit.getBytes()).equals(BigInteger.ZERO)) + .filter(unit -> capacity % unit.getBytes() == 0) .findFirst() .orElse(CapacityUnit.BYTES); } + public long getCapacityInUnit() { + return capacity / idealUnit.getBytes(); + } + @Override public String toString() { - return String.format("%d %s", capacity.divide(idealUnit.getBytes()), idealUnit.getDisplayName()); + return String.format("%d %s", capacity / 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 index 9c7a426..957d41a 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java @@ -3,30 +3,26 @@ 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 + * Represents a unit of capacity, either in binary (IEC) or decimal (SI) format. */ @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)), + BYTES("Bytes", 1), + KIBIBYTES("KiB", 1024), + MEBIBYTES("MiB", 1024 * 1024), + GIBIBYTES("GiB", 1024 * 1024 * 1024), + TEBIBYTES("TiB", 1024L * 1024 * 1024 * 1024), + KILOBYTES("kB", 1000), + MEGABYTES("MB", 1000 * 1000), + GIGABYTES("GB", 1000 * 1000 * 1000), + TERABYTES("TB", 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; + private final long bytes; } diff --git a/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java b/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java index 440f972..55b169c 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java @@ -5,8 +5,6 @@ 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. */ @@ -30,7 +28,7 @@ public class RamAsset implements Asset @Property("Capacity") @Capacity - private BigInteger capacity; + private Long capacity; @Description("The type of memory. E.g.: DDR2, SDRAM, ISA-8, etc...") @Property("Type") @@ -39,5 +37,5 @@ public class RamAsset implements Asset @Description("The speed of the memory in MHz.") @Property("Speed") - private Long speed; + private Integer speed; } diff --git a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java index e037e52..022cf13 100644 --- a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java +++ b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java @@ -15,7 +15,6 @@ 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.*; @@ -208,7 +207,7 @@ public class AssetService { } try { - return new BigInteger(value).multiply(new BigInteger(unit)); + return Long.parseLong(value) * Long.parseLong(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 667f3fc..907923b 100644 --- a/src/main/resources/templates/create_asset.html +++ b/src/main/resources/templates/create_asset.html @@ -31,7 +31,7 @@ - +