diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java index 72d8275..ad48b46 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java @@ -26,6 +26,9 @@ public class AssetDescriptor { /// The type of property, e.g.: ram, asset, etc... private final String type; + /// The Java class of the type. + private final Class assetClass; + /// The displayable name of the property, e.g.: "Random Access Memory" private final String displayName; @@ -53,6 +56,7 @@ public class AssetDescriptor { Objects.requireNonNull(assetInfo, "Asset class must be annotated with @AssetInfo"); var builder = AssetDescriptor.builder() .type(assetInfo.type()) + .assetClass(assetType) .displayName(assetInfo.displayName()) .pluralName(assetInfo.pluralName()) .visible(assetInfo.isVisible()) diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java index f657340..aae076f 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java @@ -70,7 +70,11 @@ public class AssetProperty { .setter((obj, value) -> { try { property.setAccessible(true); - property.set(obj, Converters.convert(value, property.getType())); + if (obj instanceof Asset asset) { + property.set(asset.getAsset(property.getDeclaringClass()), Converters.convert(value, property.getType())); + } else { + property.set(obj, Converters.convert(value, property.getType())); + } } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -81,7 +85,12 @@ public class AssetProperty { obj = asset.getAsset(); } property.setAccessible(true); - return property.get(obj); + + if (obj instanceof Asset asset) { + return property.get(asset.getAsset(property.getDeclaringClass())); + } else { + return property.get(obj); + } } catch (IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/src/main/java/be/seeseepuff/pcinv/models/Asset.java b/src/main/java/be/seeseepuff/pcinv/models/Asset.java index 0d97e25..d900c40 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/Asset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/Asset.java @@ -9,5 +9,24 @@ public interface Asset { return getAsset().getQr(); } + + /** + * Returns the asset as a specific type. + * @param assetType The type of asset to return, e.g., CpuAsset.class. + * @return The asset cast to the specified type. + * @param The type of asset to return, must extend Asset. + * @throws IllegalArgumentException if the requested assetType is not compatible with this asset. + */ + @SuppressWarnings("unchecked") + default T getAsset(Class assetType) { + if (assetType.equals(GenericAsset.class)) { + return (T) getAsset(); + } + if (assetType.equals(this.getClass())) { + return (T) this; + } + throw new IllegalArgumentException("No asset of type " + assetType.getSimpleName() + " found in composite."); + } + void setAsset(GenericAsset asset); } diff --git a/src/main/java/be/seeseepuff/pcinv/models/Composite.java b/src/main/java/be/seeseepuff/pcinv/models/Composite.java index 6f7a003..da18430 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/Composite.java +++ b/src/main/java/be/seeseepuff/pcinv/models/Composite.java @@ -30,13 +30,12 @@ public class Composite implements Asset { addAsset(asset); } - /** - * Returns the composite asset as a specific type. - * @param assetType The type of asset to return, e.g., CpuAsset.class. - * @return The asset cast to the specified type. - * @param The type of asset to return, must extend Asset. - */ - public T getAsset(Class assetType) { + @Override + public T getAsset(Class assetType) { + if (assetType.equals(GenericAsset.class)) { + //noinspection unchecked + return (T) getAsset(); + } if (assetType.equals(Composite.class)) { return assetType.cast(this); } diff --git a/src/main/java/be/seeseepuff/pcinv/models/PowerSupplyAsset.java b/src/main/java/be/seeseepuff/pcinv/models/PowerSupplyAsset.java index fc8be11..852811e 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/PowerSupplyAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/PowerSupplyAsset.java @@ -28,7 +28,7 @@ public class PowerSupplyAsset implements Asset @Description("The wattage rating of the power supply in watts.") @Property("Wattage") - private int wattage; + private Integer wattage; @Description("The efficiency rating of the power supply, e.g., 80 Plus Bronze, Silver, Gold, Platinum, Titanium.") @Property("Efficiency Rating") @@ -42,25 +42,25 @@ public class PowerSupplyAsset implements Asset @Description("The number number of Molex connectors.") @Property("4-pin Molex Connectors") - private int molexConnectors; + private Integer molexConnectors; @Description("The number of 4-pin floppy connectors.") @Property("4-pin Floppy Connectors") - private int floppyConnectors; + private Integer floppyConnectors; @Description("The number of SATA power connectors.") @Property("15-pin SATA Connectors") - private int sataConnectors; + private Integer sataConnectors; @Description("The number of 6-pin PCIe connectors.") @Property("6-pin PCIe Connectors") - private int pcie6Connectors; + private Integer pcie6Connectors; @Description("The number of 8-pin PCIe connectors.") @Property("8-pin PCIe Connectors") - private int pcie8Connectors; + private Integer pcie8Connectors; @Description("The number of 4-pin ATX 12V connectors.") @Property("4-pin ATX 12V Connectors") - private int atx12vConnectors; + private Integer atx12vConnectors; }