Better capacity support
All checks were successful
Build / build (push) Successful in 2m36s
Deploy / build (push) Successful in 2m40s

This commit is contained in:
2025-06-09 15:03:16 +02:00
parent 85de1c0c9f
commit 5d1c65a4c5
6 changed files with 35 additions and 58 deletions

View File

@@ -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

View File

@@ -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());
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -31,7 +31,7 @@
<option th:each="o : ${p.options}" th:value="${o.value}" th:text="${o.displayName}" th:selected="${asset != null ? (p.getValue(asset) == o.enumConstant) : o.defaultValue}">Good</option>
</select>
<span th:case="CAPACITY">
<input type="number" th:id="${d.asString(p)+'-value'}" th:name="${d.asString(p)+'-value'}" th:required="${p.required}" th:value="${p.asCapacity(asset).getCapacity()}"/>
<input type="number" th:id="${d.asString(p)+'-value'}" th:name="${d.asString(p)+'-value'}" th:required="${p.required}" th:value="${p.asCapacity(asset).getCapacityInUnit()}"/>
<select th:id="${d.asString(p)}+'-unit'" th:name="${d.asString(p)}+'-unit'">
<option value="1" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'BYTES'}">Bytes</option>
<option th:value="${1000}" th:if="${p.capacityAsSI}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'KILOBYTES'}">kB</option>