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...
|
/// The type of property, e.g.: ram, asset, etc...
|
||||||
private final String type;
|
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"
|
/// The displayable name of the property, e.g.: "Random Access Memory"
|
||||||
private final String displayName;
|
private final String displayName;
|
||||||
|
|
||||||
@@ -53,6 +56,7 @@ public class AssetDescriptor {
|
|||||||
Objects.requireNonNull(assetInfo, "Asset class must be annotated with @AssetInfo");
|
Objects.requireNonNull(assetInfo, "Asset class must be annotated with @AssetInfo");
|
||||||
var builder = AssetDescriptor.builder()
|
var builder = AssetDescriptor.builder()
|
||||||
.type(assetInfo.type())
|
.type(assetInfo.type())
|
||||||
|
.assetClass(assetType)
|
||||||
.displayName(assetInfo.displayName())
|
.displayName(assetInfo.displayName())
|
||||||
.pluralName(assetInfo.pluralName())
|
.pluralName(assetInfo.pluralName())
|
||||||
.visible(assetInfo.isVisible())
|
.visible(assetInfo.isVisible())
|
||||||
|
|||||||
@@ -70,7 +70,11 @@ public class AssetProperty {
|
|||||||
.setter((obj, value) -> {
|
.setter((obj, value) -> {
|
||||||
try {
|
try {
|
||||||
property.setAccessible(true);
|
property.setAccessible(true);
|
||||||
|
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()));
|
property.set(obj, Converters.convert(value, property.getType()));
|
||||||
|
}
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -81,7 +85,12 @@ public class AssetProperty {
|
|||||||
obj = asset.getAsset();
|
obj = asset.getAsset();
|
||||||
}
|
}
|
||||||
property.setAccessible(true);
|
property.setAccessible(true);
|
||||||
|
|
||||||
|
if (obj instanceof Asset asset) {
|
||||||
|
return property.get(asset.getAsset(property.getDeclaringClass()));
|
||||||
|
} else {
|
||||||
return property.get(obj);
|
return property.get(obj);
|
||||||
|
}
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,24 @@ public interface Asset {
|
|||||||
return getAsset().getQr();
|
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);
|
void setAsset(GenericAsset asset);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,13 +30,12 @@ public class Composite implements Asset {
|
|||||||
addAsset(asset);
|
addAsset(asset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Returns the composite asset as a specific type.
|
public <T> T getAsset(Class<T> assetType) {
|
||||||
* @param assetType The type of asset to return, e.g., CpuAsset.class.
|
if (assetType.equals(GenericAsset.class)) {
|
||||||
* @return The asset cast to the specified type.
|
//noinspection unchecked
|
||||||
* @param <T> The type of asset to return, must extend Asset.
|
return (T) getAsset();
|
||||||
*/
|
}
|
||||||
public <T extends Asset> T getAsset(Class<T> assetType) {
|
|
||||||
if (assetType.equals(Composite.class)) {
|
if (assetType.equals(Composite.class)) {
|
||||||
return assetType.cast(this);
|
return assetType.cast(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class PowerSupplyAsset implements Asset
|
|||||||
|
|
||||||
@Description("The wattage rating of the power supply in watts.")
|
@Description("The wattage rating of the power supply in watts.")
|
||||||
@Property("Wattage")
|
@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.")
|
@Description("The efficiency rating of the power supply, e.g., 80 Plus Bronze, Silver, Gold, Platinum, Titanium.")
|
||||||
@Property("Efficiency Rating")
|
@Property("Efficiency Rating")
|
||||||
@@ -42,25 +42,25 @@ public class PowerSupplyAsset implements Asset
|
|||||||
|
|
||||||
@Description("The number number of Molex connectors.")
|
@Description("The number number of Molex connectors.")
|
||||||
@Property("4-pin Molex Connectors")
|
@Property("4-pin Molex Connectors")
|
||||||
private int molexConnectors;
|
private Integer molexConnectors;
|
||||||
|
|
||||||
@Description("The number of 4-pin floppy connectors.")
|
@Description("The number of 4-pin floppy connectors.")
|
||||||
@Property("4-pin Floppy Connectors")
|
@Property("4-pin Floppy Connectors")
|
||||||
private int floppyConnectors;
|
private Integer floppyConnectors;
|
||||||
|
|
||||||
@Description("The number of SATA power connectors.")
|
@Description("The number of SATA power connectors.")
|
||||||
@Property("15-pin SATA Connectors")
|
@Property("15-pin SATA Connectors")
|
||||||
private int sataConnectors;
|
private Integer sataConnectors;
|
||||||
|
|
||||||
@Description("The number of 6-pin PCIe connectors.")
|
@Description("The number of 6-pin PCIe connectors.")
|
||||||
@Property("6-pin PCIe Connectors")
|
@Property("6-pin PCIe Connectors")
|
||||||
private int pcie6Connectors;
|
private Integer pcie6Connectors;
|
||||||
|
|
||||||
@Description("The number of 8-pin PCIe connectors.")
|
@Description("The number of 8-pin PCIe connectors.")
|
||||||
@Property("8-pin PCIe Connectors")
|
@Property("8-pin PCIe Connectors")
|
||||||
private int pcie8Connectors;
|
private Integer pcie8Connectors;
|
||||||
|
|
||||||
@Description("The number of 4-pin ATX 12V connectors.")
|
@Description("The number of 4-pin ATX 12V connectors.")
|
||||||
@Property("4-pin ATX 12V Connectors")
|
@Property("4-pin ATX 12V Connectors")
|
||||||
private int atx12vConnectors;
|
private Integer atx12vConnectors;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user