mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-21 07:59:26 +00:00
185 lines
5.4 KiB
Diff
185 lines
5.4 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Kevin Raneri <kevin.raneri@gmail.com>
|
|
Date: Tue, 9 Nov 2021 23:36:56 -0500
|
|
Subject: [PATCH] Pufferfish: Utils
|
|
|
|
Original license: GPL v3
|
|
Original project: https://github.com/pufferfish-gg/Pufferfish
|
|
|
|
diff --git a/src/main/java/gg/pufferfish/pufferfish/PufferfishLogger.java b/src/main/java/gg/pufferfish/pufferfish/PufferfishLogger.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..53aab67aea0a28c004c6106aa775443c30457141
|
|
--- /dev/null
|
|
+++ b/src/main/java/gg/pufferfish/pufferfish/PufferfishLogger.java
|
|
@@ -0,0 +1,17 @@
|
|
+package gg.pufferfish.pufferfish;
|
|
+
|
|
+import java.util.logging.Level;
|
|
+import java.util.logging.Logger;
|
|
+
|
|
+import org.bukkit.Bukkit;
|
|
+
|
|
+public class PufferfishLogger extends Logger {
|
|
+ public static final PufferfishLogger LOGGER = new PufferfishLogger();
|
|
+
|
|
+ private PufferfishLogger() {
|
|
+ super("Pufferfish", null);
|
|
+
|
|
+ setParent(Bukkit.getLogger());
|
|
+ setLevel(Level.ALL);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/gg/pufferfish/pufferfish/util/AsyncExecutor.java b/src/main/java/gg/pufferfish/pufferfish/util/AsyncExecutor.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..a62d22dc4ae2cc82cf6763e8b0ce6d4611782a57
|
|
--- /dev/null
|
|
+++ b/src/main/java/gg/pufferfish/pufferfish/util/AsyncExecutor.java
|
|
@@ -0,0 +1,73 @@
|
|
+package gg.pufferfish.pufferfish.util;
|
|
+
|
|
+import com.google.common.collect.Queues;
|
|
+import gg.pufferfish.pufferfish.PufferfishLogger;
|
|
+
|
|
+import java.util.Queue;
|
|
+import java.util.concurrent.locks.Condition;
|
|
+import java.util.concurrent.locks.Lock;
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
+import java.util.logging.Level;
|
|
+
|
|
+public class AsyncExecutor implements Runnable {
|
|
+
|
|
+ private final Queue<Runnable> jobs = Queues.newArrayDeque();
|
|
+ private final Lock mutex = new ReentrantLock();
|
|
+ private final Condition cond = mutex.newCondition();
|
|
+ private final Thread thread;
|
|
+ private volatile boolean killswitch = false;
|
|
+
|
|
+ public AsyncExecutor(String threadName) {
|
|
+ this.thread = new Thread(this, threadName);
|
|
+ }
|
|
+
|
|
+ public void start() {
|
|
+ thread.start();
|
|
+ }
|
|
+
|
|
+ public void kill() {
|
|
+ killswitch = true;
|
|
+ cond.signalAll();
|
|
+ }
|
|
+
|
|
+ public void submit(Runnable runnable) {
|
|
+ mutex.lock();
|
|
+ try {
|
|
+ jobs.offer(runnable);
|
|
+ cond.signalAll();
|
|
+ } finally {
|
|
+ mutex.unlock();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void run() {
|
|
+ while (!killswitch) {
|
|
+ try {
|
|
+ Runnable runnable = takeRunnable();
|
|
+ if (runnable != null) {
|
|
+ runnable.run();
|
|
+ }
|
|
+ } catch (InterruptedException e) {
|
|
+ Thread.currentThread().interrupt();
|
|
+ } catch (Exception e) {
|
|
+ PufferfishLogger.LOGGER.log(Level.SEVERE, e, () -> "Failed to execute async job for thread " + thread.getName());
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private Runnable takeRunnable() throws InterruptedException {
|
|
+ mutex.lock();
|
|
+ try {
|
|
+ while (jobs.isEmpty() && !killswitch) {
|
|
+ cond.await();
|
|
+ }
|
|
+
|
|
+ if (jobs.isEmpty()) return null; // We've set killswitch
|
|
+
|
|
+ return jobs.remove();
|
|
+ } finally {
|
|
+ mutex.unlock();
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/gg/pufferfish/pufferfish/util/IterableWrapper.java b/src/main/java/gg/pufferfish/pufferfish/util/IterableWrapper.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..8b4a51ae41e99d41a1095333c2036efcc9a38973
|
|
--- /dev/null
|
|
+++ b/src/main/java/gg/pufferfish/pufferfish/util/IterableWrapper.java
|
|
@@ -0,0 +1,20 @@
|
|
+package gg.pufferfish.pufferfish.util;
|
|
+
|
|
+import java.util.Iterator;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public class IterableWrapper<T> implements Iterable<T> {
|
|
+
|
|
+ private final Iterator<T> iterator;
|
|
+
|
|
+ public IterableWrapper(Iterator<T> iterator) {
|
|
+ this.iterator = iterator;
|
|
+ }
|
|
+
|
|
+ @NotNull
|
|
+ @Override
|
|
+ public Iterator<T> iterator() {
|
|
+ return iterator;
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/gg/pufferfish/pufferfish/util/Long2ObjectOpenHashMapWrapper.java b/src/main/java/gg/pufferfish/pufferfish/util/Long2ObjectOpenHashMapWrapper.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..5578acce073cea8a60619e634b3862624c8a1ae8
|
|
--- /dev/null
|
|
+++ b/src/main/java/gg/pufferfish/pufferfish/util/Long2ObjectOpenHashMapWrapper.java
|
|
@@ -0,0 +1,42 @@
|
|
+package gg.pufferfish.pufferfish.util;
|
|
+
|
|
+import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
|
+
|
|
+import java.util.Map;
|
|
+
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+
|
|
+public class Long2ObjectOpenHashMapWrapper<V> extends Long2ObjectOpenHashMap<V> {
|
|
+
|
|
+ private final Map<Long, V> backingMap;
|
|
+
|
|
+ public Long2ObjectOpenHashMapWrapper(Map<Long, V> map) {
|
|
+ backingMap = map;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public V put(Long key, V value) {
|
|
+ return backingMap.put(key, value);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public V get(Object key) {
|
|
+ return backingMap.get(key);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public V remove(Object key) {
|
|
+ return backingMap.remove(key);
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ @Override
|
|
+ public V putIfAbsent(Long key, V value) {
|
|
+ return backingMap.putIfAbsent(key, value);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int size() {
|
|
+ return backingMap.size();
|
|
+ }
|
|
+}
|