Compare commits
2 commits
bda04a188b
...
f713e3941d
Author | SHA1 | Date | |
---|---|---|---|
|
f713e3941d | ||
|
ec7877fe22 |
4 changed files with 61 additions and 1 deletions
|
@ -5,6 +5,10 @@ import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import dev.peerat.framework.Locker.Key;
|
||||||
|
|
||||||
public class Locker<V>{
|
public class Locker<V>{
|
||||||
|
|
||||||
|
@ -55,5 +59,51 @@ public class Locker<V>{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void listen(Consumer<V> action){
|
||||||
|
Key key = new Key();
|
||||||
|
init(key);
|
||||||
|
try {
|
||||||
|
while(true){
|
||||||
|
lock(key);
|
||||||
|
V value = getValue(key);
|
||||||
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}catch(Exception e){}
|
||||||
|
remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void listen(Supplier<Boolean> condition,Consumer<V> action){
|
||||||
|
Key key = new Key();
|
||||||
|
init(key);
|
||||||
|
try {
|
||||||
|
while(condition.get()){
|
||||||
|
lock(key);
|
||||||
|
V value = getValue(key);
|
||||||
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}catch(Exception e){}
|
||||||
|
remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Thread listenAsync(Consumer<V> action){
|
||||||
|
Thread thread = new Thread(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
listen(action);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Thread listenAsync(Supplier<Boolean> condition,Consumer<V> action){
|
||||||
|
Thread thread = new Thread(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
listen(condition, action);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Key{ public Key(){} }
|
public static class Key{ public Key(){} }
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package dev.peerat.framework.utils.json;
|
||||||
|
|
||||||
public class Json{
|
public class Json{
|
||||||
|
|
||||||
public void buildValue(StringBuilder builder, Object value){
|
void buildValue(StringBuilder builder, Object value){
|
||||||
if(value == null) builder.append("null");
|
if(value == null) builder.append("null");
|
||||||
else if(value instanceof String){
|
else if(value instanceof String){
|
||||||
builder.append('"');
|
builder.append('"');
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package dev.peerat.framework.utils.json;
|
package dev.peerat.framework.utils.json;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -12,6 +13,10 @@ public class JsonArray extends Json{
|
||||||
this.list = new ArrayList<>();
|
this.list = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<Object> toList(){
|
||||||
|
return this.list;
|
||||||
|
}
|
||||||
|
|
||||||
public void add(Object value){
|
public void add(Object value){
|
||||||
this.list.add(value);
|
this.list.add(value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class JsonMap extends Json{
|
public class JsonMap extends Json{
|
||||||
|
|
||||||
|
@ -13,6 +14,10 @@ public class JsonMap extends Json{
|
||||||
this.map = new HashMap<>();
|
this.map = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<Entry<String, Object>> entries(){
|
||||||
|
return this.map.entrySet();
|
||||||
|
}
|
||||||
|
|
||||||
public void set(String key, Object value){
|
public void set(String key, Object value){
|
||||||
this.map.put(key, value);
|
this.map.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue