Add BigInteger support for capacity handling and enhance UI for capacity selection
This commit is contained in:
@@ -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);
|
||||
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 = "";
|
||||
|
||||
59
src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java
Normal file
59
src/main/java/be/seeseepuff/pcinv/meta/CapacityInfo.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
32
src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java
Normal file
32
src/main/java/be/seeseepuff/pcinv/meta/CapacityUnit.java
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -31,17 +31,17 @@
|
||||
<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}"/>
|
||||
<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()}"/>
|
||||
<select th:id="${d.asString(p)}+'-unit'" th:name="${d.asString(p)}+'-unit'">
|
||||
<option value="1">Bytes</option>
|
||||
<option th:if="${p.capacityAsSI}">kB</option>
|
||||
<option th:if="${p.capacityAsIEC}">KiB</option>
|
||||
<option th:if="${p.capacityAsSI}">MB</option>
|
||||
<option th:if="${p.capacityAsIEC}">MiB</option>
|
||||
<option th:if="${p.capacityAsSI}">GB</option>
|
||||
<option th:if="${p.capacityAsIEC}">GiB</option>
|
||||
<option th:if="${p.capacityAsSI}">TB</option>
|
||||
<option th:if="${p.capacityAsIEC}">TiB</option>
|
||||
<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>
|
||||
<option th:value="${1024}" th:if="${p.capacityAsIEC}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'KIBIBYTES'}">KiB</option>
|
||||
<option th:value="${1000*1000}" th:if="${p.capacityAsSI}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'MEGABYTES'}">MB</option>
|
||||
<option th:value="${1024*1024}" th:if="${p.capacityAsIEC}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'MEBIBYTES'}">MiB</option>
|
||||
<option th:value="${1000*1000*1000}" th:if="${p.capacityAsSI}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'GIGABYTES'}">GB</option>
|
||||
<option th:value="${1024*1024*1024}" th:if="${p.capacityAsIEC}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'GIBIBYTES'}">GiB</option>
|
||||
<option value="1000000000000" th:if="${p.capacityAsSI}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'TERABYTES'}">TB</option>
|
||||
<option value="1099511627776" th:if="${p.capacityAsIEC}" th:selected="${p.asCapacity(asset).getIdealUnit().name() == 'TEBIBYTES'}">TiB</option>
|
||||
</select>
|
||||
</span>
|
||||
<b th:case="*">Bad input type for <span th:text="${d.type}+'-'+${p.type}"></span></b>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<ul th:if="${action == 'view'}">
|
||||
<li><a th:href="'/edit/'+${asset.getQr()}">Edit</a></li>
|
||||
<li><a th:href="'/delete/'+${asset.getQr()}">Delete</a></li>
|
||||
<li><a th:href="'/create/'+${asset.getAsset().type}">Create another <span th:text="${descriptor.displayName}">Hard Drive</span> </a></li>
|
||||
<li><a th:href="'/browse/'+${descriptor.type}">Browse all <span th:text="${descriptor.pluralName}">Hard Drives</span></a></li>
|
||||
</ul>
|
||||
<ul th:if="${action == 'delete'}">
|
||||
|
||||
Reference in New Issue
Block a user