Skip to content

Commit

Permalink
chore: remove custom linkedlist
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Jun 14, 2021
1 parent 4caf510 commit 44c56ac
Showing 1 changed file with 19 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.terasology.gestalt.assets;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,6 +27,7 @@
import java.security.PrivilegedActionException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
Expand Down Expand Up @@ -56,88 +58,44 @@
public abstract class Asset<T extends AssetData> {
private static final Logger logger = LoggerFactory.getLogger(Asset.class);

private AssetNode<T> next;
private final List<WeakReference<Asset<T>>> instances;
private Asset<T> parent;

private final ResourceUrn urn;
private final AssetType<?, T> assetType;
private final DisposalHook disposalHook = new DisposalHook();
private volatile boolean disposed;

private static class AssetNode<T extends AssetData> {
WeakReference<Asset<T>> reference;
AssetNode<T> next;

public AssetNode(Asset<T> root, Asset<T> instance) {
this.reference = new WeakReference<>(instance);
instance.parent = root;
}

protected boolean hasValidAsset() {
Asset<T> instance = reference == null ? null: reference.get();
if(instance == null) {
return false;
}
return !instance.isDisposed();
}
}


private void pushAsset(Asset<T> asset) {
if (next == null) {
private void appendInstance(Asset<T> asset) {
if (parent == null) {
Preconditions.checkArgument(!getUrn().isInstance());
next = new AssetNode<>(this, asset);
asset.parent = this;
instances.add(new WeakReference<>(asset));
} else {
AssetNode node = new AssetNode<>(parent, asset);
node.next = next;
next = node;
asset.parent = parent;
parent.instances.add(new WeakReference<>(asset));
}
}

protected Iterable<WeakReference<Asset<T>>> instances() {
if (this.parent != null) {
return parent.instances();
}
AssetNode<T> rootNode = this.next;
if(rootNode == null) {
return Collections::emptyIterator;
}
return () -> new Iterator<WeakReference<Asset<T>>>() {
AssetNode<T> node = null;

@Override
public boolean hasNext() {
if (node == null) {
return true;
}
return node.next != null;
}

@Override
public WeakReference<Asset<T>> next() {
if (node == null) {
node = rootNode.next;
return rootNode.reference;
}
node = node.next;
return node.reference;
}
};
return instances;
}

protected void cleanup() {
if(this.parent != null) {
parent.cleanup();
return;
}
AssetNode<T> node = next;
while (node != null) {
AssetNode<T> nextAsset = node.next;
while (nextAsset != null && !nextAsset.hasValidAsset()) {
nextAsset = nextAsset.next;
Iterator<WeakReference<Asset<T>>> iter = instances.iterator();
while(iter.hasNext()) {
WeakReference<Asset<T>> reference = iter.next();
Asset<T> inst = reference.get();
if(inst == null || inst.isDisposed()) {
iter.remove();
}
node.next = nextAsset;
node = nextAsset;
}
}

Expand All @@ -150,6 +108,7 @@ protected void cleanup() {
public Asset(ResourceUrn urn, AssetType<?, T> assetType) {
Preconditions.checkNotNull(urn);
Preconditions.checkNotNull(assetType);
instances = urn.isInstance() ? Collections.emptyList(): Lists.newArrayList();
this.urn = urn;
this.assetType = assetType;
assetType.registerAsset(this, disposalHook);
Expand All @@ -167,6 +126,7 @@ public Asset(ResourceUrn urn, AssetType<?, T> assetType) {
public Asset(ResourceUrn urn, AssetType<?, T> assetType, DisposableResource resource) {
Preconditions.checkNotNull(urn);
Preconditions.checkNotNull(assetType);
instances = urn.isInstance() ? Collections.emptyList(): Lists.newArrayList();
this.urn = urn;
this.assetType = assetType;
disposalHook.setDisposableResource(resource);
Expand Down Expand Up @@ -221,7 +181,7 @@ public final <U extends Asset<T>> Optional<U> createInstance() {
assetCopy = Optional.ofNullable(assetType.loadAsset(instanceUrn, assetData.get()));
}
}
assetCopy.ifPresent(this::pushAsset);
assetCopy.ifPresent(this::appendInstance);
return (Optional<U>) assetCopy;
}

Expand Down

0 comments on commit 44c56ac

Please sign in to comment.