Enhance asset handling with type-specific retrieval and null safety improvements
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 <T> 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> T getAsset(Class<T> 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);
|
||||
}
|
||||
|
||||
@@ -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 <T> The type of asset to return, must extend Asset.
|
||||
*/
|
||||
public <T extends Asset> T getAsset(Class<T> assetType) {
|
||||
@Override
|
||||
public <T> T getAsset(Class<T> assetType) {
|
||||
if (assetType.equals(GenericAsset.class)) {
|
||||
//noinspection unchecked
|
||||
return (T) getAsset();
|
||||
}
|
||||
if (assetType.equals(Composite.class)) {
|
||||
return assetType.cast(this);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user