Skip to content

Commit

Permalink
chore: handle disposable action outside of the constructor (#118)
Browse files Browse the repository at this point in the history
* chore: handle disposable action outside of the constructor
* make asset constructor protected
  • Loading branch information
pollend authored Jun 14, 2021
1 parent 0f6c4d9 commit 3a18fc6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public abstract class Asset<T extends AssetData> {
* @param urn The urn identifying the asset.
* @param assetType The asset type this asset belongs to.
*/
public Asset(ResourceUrn urn, AssetType<?, T> assetType) {
protected Asset(ResourceUrn urn, AssetType<?, T> assetType) {
Preconditions.checkNotNull(urn);
Preconditions.checkNotNull(assetType);
this.urn = urn;
Expand All @@ -71,21 +71,13 @@ public Asset(ResourceUrn urn, AssetType<?, T> assetType) {
}

/**
* The constructor for an asset. It is suggested that implementing classes provide a constructor taking both the urn, and an initial AssetData to load.
*
* @param urn The urn identifying the asset.
* @param assetType The asset type this asset belongs to.
* set a resource handler so the disposable hook can clean up resources not managed by the JVM
* @param resource A resource to close when disposing this class. The resource must not have a reference to this asset -
* this would prevent it being garbage collected. It must be a static inner class, or not contained in the asset class
* (or an anonymous class defined in a static context). A warning will be logged if this is not the case.
*/
public Asset(ResourceUrn urn, AssetType<?, T> assetType, DisposableResource resource) {
Preconditions.checkNotNull(urn);
Preconditions.checkNotNull(assetType);
this.urn = urn;
this.assetType = assetType;
disposalHook.setDisposableResource(resource);
assetType.registerAsset(this, disposalHook);
protected void setDisposableResource(DisposableResource resource) {
this.disposalHook.setDisposableResource(resource);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class Book extends Asset<BookData> {
private ImmutableList<String> lines = ImmutableList.of();

public Book(ResourceUrn urn, BookData data, AssetType<?, BookData> type) {
super(urn, type, new DisposalAction(urn));
super(urn, type);
setDisposableResource(new DisposalAction(urn));
reload(data);
}

Expand Down

0 comments on commit 3a18fc6

Please sign in to comment.