-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
smooth transition during profile updates
- Loading branch information
Showing
14 changed files
with
293 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.