Skip to content

Commit

Permalink
smooth transition during profile updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zlainsama committed Mar 2, 2019
1 parent d7ae409 commit a589357
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 129 deletions.
21 changes: 21 additions & 0 deletions src/main/java/lain/lib/SharedPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package lain.lib;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;

public final class SharedPool
{

private static final ExecutorService thePool = Executors.newWorkStealingPool(ForkJoinPool.getCommonPoolParallelism() + 1);

public static void execute(Runnable command)
{
thePool.execute(command);
}

private SharedPool()
{
}

}
105 changes: 105 additions & 0 deletions src/main/java/lain/mods/skins/api/SkinBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package lain.mods.skins.api;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import lain.mods.skins.api.interfaces.ISkin;

/**
* A special ISkin object that will return first ready ISkin object in a collection. <br>
* It supports swapping it's collection reference at runtime.
*
*/
public class SkinBundle implements ISkin
{

protected final AtomicReference<Collection<ISkin>> ref = new AtomicReference<>(Collections.emptyList());
protected final Collection<Consumer<ISkin>> listeners = new CopyOnWriteArrayList<>();
protected final Collection<Function<ByteBuffer, ByteBuffer>> filters = new CopyOnWriteArrayList<>();

protected Optional<ISkin> find()
{
Collection<ISkin> skins;
if ((skins = ref.get()).isEmpty())
return Optional.empty();
return skins.stream().filter(ISkin::isDataReady).findFirst();
}

@Override
public ByteBuffer getData()
{
return find().orElse(SkinProviderAPI.DUMMY).getData();
}

@Override
public String getSkinType()
{
return find().orElse(SkinProviderAPI.DUMMY).getSkinType();
}

@Override
public boolean isDataReady()
{
return find().orElse(SkinProviderAPI.DUMMY).isDataReady();
}

@Override
public void onRemoval()
{
set(Collections.emptyList());
}

public SkinBundle set(Collection<ISkin> c)
{
Objects.requireNonNull(c);
if (!c.isEmpty())
{
for (ISkin e : c)
{
listeners.forEach(e::setRemovalListener);
filters.forEach(e::setSkinFilter);
}
}
Collection<ISkin> skins;
if (!(skins = ref.getAndSet(c)).isEmpty())
skins.forEach(ISkin::onRemoval);
return this;
}

@Override
public boolean setRemovalListener(Consumer<ISkin> listener)
{
if (listener == null || listeners.contains(listener))
return false;
if (listeners.add(listener))
{
Collection<ISkin> skins;
if (!(skins = ref.get()).isEmpty())
skins.forEach(e -> e.setRemovalListener(listener));
return true;
}
return false;
}

@Override
public boolean setSkinFilter(Function<ByteBuffer, ByteBuffer> filter)
{
if (filter == null || filters.contains(filter))
return false;
if (filters.add(filter))
{
Collection<ISkin> skins;
if (!(skins = ref.get()).isEmpty())
skins.forEach(e -> e.setSkinFilter(filter));
return true;
}
return false;
}

}
Loading

0 comments on commit a589357

Please sign in to comment.