From a38d182d82cc8334896b3a0fd09d7442a2dedcca Mon Sep 17 00:00:00 2001 From: slowcheet4h <47327665+slowcheet4h@users.noreply.github.com> Date: Sat, 22 May 2021 13:22:46 +0300 Subject: [PATCH] async_when | async_while | async_loop_times | async_loop_till | async_loop_times_w | async_loop_till_w | removed some redundant code --- pisi/unitedmeows/meowlib/async/Async.java | 104 ++++++++++++++-- .../meowlib/async/BasicTaskPool.java | 1 - pisi/unitedmeows/meowlib/async/Future.java | 20 +++- .../unitedmeows/meowlib/async/TaskWorker.java | 1 - .../meowlib/async/states/IState.java | 7 ++ pisi/unitedmeows/meowlib/clazz/IProperty.java | 5 +- pisi/unitedmeows/meowlib/clazz/onion.java | 37 +++++- pisi/unitedmeows/meowlib/clazz/prop.java | 22 +++- pisi/unitedmeows/meowlib/clazz/type.java | 40 +++++++ .../meowlib/encryption/memory/MString.java | 33 ++++- pisi/unitedmeows/meowlib/etc/IFilter.java | 4 +- pisi/unitedmeows/meowlib/graphics/Color.java | 43 +++++++ pisi/unitedmeows/meowlib/lists/list.java | 11 ++ pisi/unitedmeows/meowlib/memory/MMemory.java | 28 +++++ .../meowlib/predefined/STRING.java | 12 +- .../meowlib/reflection/Reflect.java | 113 ++++++++++++++++++ pisi/unitedmeows/meowlib/signal/poc.txt | 11 ++ test/Start.java | 38 ++++-- 18 files changed, 494 insertions(+), 36 deletions(-) create mode 100644 pisi/unitedmeows/meowlib/async/states/IState.java create mode 100644 pisi/unitedmeows/meowlib/graphics/Color.java diff --git a/pisi/unitedmeows/meowlib/async/Async.java b/pisi/unitedmeows/meowlib/async/Async.java index 02c0f0a..bb7dae7 100644 --- a/pisi/unitedmeows/meowlib/async/Async.java +++ b/pisi/unitedmeows/meowlib/async/Async.java @@ -1,13 +1,13 @@ package pisi.unitedmeows.meowlib.async; import pisi.unitedmeows.meowlib.MeowLib; +import pisi.unitedmeows.meowlib.clazz.prop; import pisi.unitedmeows.meowlib.etc.CoID; -import pisi.unitedmeows.meowlib.etc.IAction; +import pisi.unitedmeows.meowlib.async.states.IState; import pisi.unitedmeows.meowlib.etc.MLibSettings; import pisi.unitedmeows.meowlib.thread.kThread; import java.util.HashMap; -import java.util.UUID; public class Async { @@ -25,6 +25,27 @@ public static void _return(CoID uuid, Object result) { task(uuid)._return(result); } + public static Promise async_when(IState when, IAsyncAction asyncAction, long checkInterval) { + prop promise = new prop(); + promise.set(async_wloop(u -> { + if (when.state()) { + async_f(asyncAction); + promise.get().stop(); + } + }, checkInterval)); + return promise.get(); + } + + public static Promise async_while(IState state, IAsyncAction asyncAction, long loopInterval) { + prop promise = new prop(); + promise.set(async_wloop(u -> { + if (!state.state()) { + promise.get().stop(); + } + async_f(asyncAction); + }, loopInterval)); + return promise.get(); + } public static Future async_f(IAsyncAction action) { // change this uuid alternative @@ -44,7 +65,7 @@ public void run() { } public static Future async(IAsyncAction action) { - // change this uuid alternative + final CoID pointer = newPointer(); Future future = new Future<>(pointer); @@ -121,11 +142,6 @@ public static Promise async_loop(IAsyncAction action, final long repeatDelay, fi return promise; } - public static Promise async_loop_w(IAsyncAction action, final long repeatDelay, final long lifeTime) { - final Promise promise = async_loop_w(action, repeatDelay); - async_w((u) -> promise.stop(), lifeTime + repeatDelay); - return promise; - } public static Promise async_loop(IAsyncAction action, final long repeatDelay) { final Promise promise = new Promise(); @@ -144,8 +160,78 @@ public void run() { return promise; } + public static Promise async_loop_times(IAsyncAction loop, IAsyncAction finalAction, final long repeatDelay, final int repeatTime) { + final Promise promise = async_loop(loop, repeatDelay); + + async_w((u)->{ + promise.stop(); + async_f(finalAction); + }, repeatDelay * repeatTime); + return promise; + } + + public static Promise async_loop_times(IAsyncAction loop, final long repeatDelay, final int repeatTime) { + final Promise promise = async_loop(loop, repeatDelay); + async_w((u)->{ + promise.stop(); + }, repeatDelay * repeatTime); + return promise; + } + + public static Promise async_loop_times_w(IAsyncAction loop, final long repeatDelay, final int repeatTime) { + final Promise promise = async_loop_m(loop, repeatDelay); + async_w(u -> { + promise.start(); + }, repeatDelay); + async_w((u)->{ + promise.stop(); + }, repeatDelay * repeatTime); + return promise; + } + + + public static Promise async_loop_till(IAsyncAction loop, final long repeatDelay, final int till) { + final Promise promise = async_loop(loop, repeatDelay); + async_w((u) -> { + promise.stop(); + }, till); + return promise; + } + + public static Promise async_loop_till(IAsyncAction loop, IAsyncAction finalAction, final long repeatDelay, final int till) { + final Promise promise = async_loop(loop, repeatDelay); + + async_w((u) -> { + promise.stop(); + async_f(finalAction); + }, till); + return promise; + } + public static Promise async_loop_till_w(IAsyncAction loop, final long repeatDelay, final int till) { + final Promise promise = async_loop_m(loop, repeatDelay); + async_w(u -> { + promise.start(); + }, repeatDelay); + async_w((u) -> { + promise.stop(); + }, till); + return promise; + } + + public static Promise async_loop_till_w(IAsyncAction loop, IAsyncAction finalAction, final long repeatDelay, final int till) { + final Promise promise = async_loop_m(loop, repeatDelay); + async_w(u -> { + promise.start(); + }, repeatDelay); + async_w((u) -> { + async_f(finalAction); + promise.stop(); + }, till); + return promise; + } + /* same as async_loop but starts manually */ - public static Promise async_loop_w(IAsyncAction action, final long repeatDelay) { + public static Promise async_loop_m(IAsyncAction action, final long repeatDelay) { final Promise promise = new Promise(); Task task = new Task(action) { @Override diff --git a/pisi/unitedmeows/meowlib/async/BasicTaskPool.java b/pisi/unitedmeows/meowlib/async/BasicTaskPool.java index 1ab582d..89033ff 100644 --- a/pisi/unitedmeows/meowlib/async/BasicTaskPool.java +++ b/pisi/unitedmeows/meowlib/async/BasicTaskPool.java @@ -1,6 +1,5 @@ package pisi.unitedmeows.meowlib.async; -import com.sun.jmx.remote.internal.ArrayQueue; import pisi.unitedmeows.meowlib.MeowLib; import pisi.unitedmeows.meowlib.etc.MLibSettings; import pisi.unitedmeows.meowlib.thread.kThread; diff --git a/pisi/unitedmeows/meowlib/async/Future.java b/pisi/unitedmeows/meowlib/async/Future.java index 04e3553..d8677af 100644 --- a/pisi/unitedmeows/meowlib/async/Future.java +++ b/pisi/unitedmeows/meowlib/async/Future.java @@ -1,6 +1,7 @@ package pisi.unitedmeows.meowlib.async; import pisi.unitedmeows.meowlib.etc.CoID; +import pisi.unitedmeows.meowlib.etc.Tuple; import java.util.ArrayList; import java.util.List; @@ -10,7 +11,7 @@ public class Future { private X value; private CoID pointer; private Task task; - private transient List afterTasks; + private transient List> afterTasks; public Future(CoID pointer) { this.pointer = pointer; @@ -26,8 +27,12 @@ public Task task() { public void post() { if (afterTasks != null) { - for (IAsyncAction afterTask : afterTasks) { - Async.async(afterTask); + for (Tuple afterTask : afterTasks) { + if (afterTask.getFirst() == 0) { + Async.async_f(afterTask.getSecond()); + } else { + Async.async_w(afterTask.getSecond(), afterTask.getFirst()); + } } afterTasks.clear(); } @@ -39,7 +44,14 @@ public void after(IAsyncAction task) { if (afterTasks == null) { afterTasks = new ArrayList<>(); } - afterTasks.add(task); + afterTasks.add(new Tuple(0L, task)); + } + + public void after_w(IAsyncAction task, long time) { + if (afterTasks == null) { + afterTasks = new ArrayList<>(); + } + afterTasks.add(new Tuple(time, task)); } diff --git a/pisi/unitedmeows/meowlib/async/TaskWorker.java b/pisi/unitedmeows/meowlib/async/TaskWorker.java index 6c57e33..9d53910 100644 --- a/pisi/unitedmeows/meowlib/async/TaskWorker.java +++ b/pisi/unitedmeows/meowlib/async/TaskWorker.java @@ -1,6 +1,5 @@ package pisi.unitedmeows.meowlib.async; -import com.sun.istack.internal.NotNull; import pisi.unitedmeows.meowlib.MeowLib; import pisi.unitedmeows.meowlib.etc.MLibSettings; import pisi.unitedmeows.meowlib.thread.kThread; diff --git a/pisi/unitedmeows/meowlib/async/states/IState.java b/pisi/unitedmeows/meowlib/async/states/IState.java new file mode 100644 index 0000000..797f340 --- /dev/null +++ b/pisi/unitedmeows/meowlib/async/states/IState.java @@ -0,0 +1,7 @@ +package pisi.unitedmeows.meowlib.async.states; + +@FunctionalInterface +public interface IState { + + boolean state(); +} diff --git a/pisi/unitedmeows/meowlib/clazz/IProperty.java b/pisi/unitedmeows/meowlib/clazz/IProperty.java index 5f1cb3c..38e296e 100644 --- a/pisi/unitedmeows/meowlib/clazz/IProperty.java +++ b/pisi/unitedmeows/meowlib/clazz/IProperty.java @@ -1,4 +1,7 @@ package pisi.unitedmeows.meowlib.clazz; -public class IProperty { +public interface IProperty { + + void set(X value); + X get(); } diff --git a/pisi/unitedmeows/meowlib/clazz/onion.java b/pisi/unitedmeows/meowlib/clazz/onion.java index 69ec495..2c5515f 100644 --- a/pisi/unitedmeows/meowlib/clazz/onion.java +++ b/pisi/unitedmeows/meowlib/clazz/onion.java @@ -1,4 +1,39 @@ package pisi.unitedmeows.meowlib.clazz; -public class onion { +import pisi.unitedmeows.meowlib.memory.MMemory; + +public class onion { + + private int pointer; + + public onion() { + pointer = -1; + } + + public onion(X value) { + set(value); + } + + public void set(X value) { + if (pointer == -1) { + pointer = MMemory.write(value); + } else { + MMemory.write(pointer, value); + } + } + + public X get() { + if (pointer == -1) { + return null; + } + + return MMemory.read(pointer); + } + + public void remove() { + if (pointer != -1) { + MMemory.remove(pointer); + pointer = -1; + } + } } diff --git a/pisi/unitedmeows/meowlib/clazz/prop.java b/pisi/unitedmeows/meowlib/clazz/prop.java index b2dd687..4304857 100644 --- a/pisi/unitedmeows/meowlib/clazz/prop.java +++ b/pisi/unitedmeows/meowlib/clazz/prop.java @@ -1,4 +1,24 @@ package pisi.unitedmeows.meowlib.clazz; -public class prop { +import pisi.unitedmeows.meowlib.async.Promise; + +public class prop implements IProperty { + + protected X value; /* change this to some optional variable */ + + public prop() { } + + public prop(X _startObject) { + value = _startObject; + } + + @Override + public void set(X value) { + this.value = value; + } + + @Override + public X get() { + return value; + } } diff --git a/pisi/unitedmeows/meowlib/clazz/type.java b/pisi/unitedmeows/meowlib/clazz/type.java index 215086a..92ec81d 100644 --- a/pisi/unitedmeows/meowlib/clazz/type.java +++ b/pisi/unitedmeows/meowlib/clazz/type.java @@ -1,4 +1,44 @@ package pisi.unitedmeows.meowlib.clazz; public class type { + + public static boolean is_type(Object object, Class ... types) { + for (Class type : types) { + if (!type.isInstance(object)) { + return false; + } + } + return true; + } + + public static X cast(Object object) { + return (X) object; + } + + public static boolean same_type(Object object, Object... objects) { + Class type = typeof(object); + for (Object obj : objects) { + if (typeof(obj) != type) { + return false; + } + } + return true; + } + + public static Class[] type_array(Object... objects) { + Class types[] = new Class[objects.length]; + int i = 0; + for (Object parameter : objects) { + types[i++] = type.typeof(parameter); + } + return types; + } + + public static Class typeof(Object object) { + return object.getClass(); + } + + public static String toString(Object object) { + return object.toString(); + } } diff --git a/pisi/unitedmeows/meowlib/encryption/memory/MString.java b/pisi/unitedmeows/meowlib/encryption/memory/MString.java index ee04c33..bb07aa2 100644 --- a/pisi/unitedmeows/meowlib/encryption/memory/MString.java +++ b/pisi/unitedmeows/meowlib/encryption/memory/MString.java @@ -1,4 +1,35 @@ package pisi.unitedmeows.meowlib.encryption.memory; -public class MString { +import java.util.Arrays; + +/** + * memory safe strings + */ +public class MString implements Comparable { + + private char[] value; + + public MString(String input) { + + final String encrypted = input; + + value = new char[encrypted.length()]; + int i = 0; + for (char c : encrypted.toCharArray()) { + value[i] = c; + i++; + } + } + + + @Override + public String toString() { + // decrypt + return value.toString(); + } + + @Override + public int compareTo(MString o) { + return 0; + } } diff --git a/pisi/unitedmeows/meowlib/etc/IFilter.java b/pisi/unitedmeows/meowlib/etc/IFilter.java index 2e54d91..c331794 100644 --- a/pisi/unitedmeows/meowlib/etc/IFilter.java +++ b/pisi/unitedmeows/meowlib/etc/IFilter.java @@ -1,4 +1,6 @@ package pisi.unitedmeows.meowlib.etc; -public class Filtere { +public interface IFilter { + + boolean check(X object); } diff --git a/pisi/unitedmeows/meowlib/graphics/Color.java b/pisi/unitedmeows/meowlib/graphics/Color.java new file mode 100644 index 0000000..170a8ca --- /dev/null +++ b/pisi/unitedmeows/meowlib/graphics/Color.java @@ -0,0 +1,43 @@ +package pisi.unitedmeows.meowlib.graphics; + +import pisi.unitedmeows.meowlib.etc.Tuple; +import pisi.unitedmeows.meowlib.etc.Tuple3; +import pisi.unitedmeows.meowlib.etc.Tuple4; + +public class Color { + + + private int red, green, blue, alpha; + + public Color(int red, int green, int blue) { + this.red = red; + this.green = green; + this.blue = blue; + } + + public Color(int red, int green, int blue, int alpha) { + this(red, green, blue); + this.alpha = alpha; + } + + + public static Color color(String input) { + return null; + } + + public int getRed() { + return red; + } + + public int getBlue() { + return blue; + } + + public int getAlpha() { + return alpha; + } + + public int getGreen() { + return green; + } +} \ No newline at end of file diff --git a/pisi/unitedmeows/meowlib/lists/list.java b/pisi/unitedmeows/meowlib/lists/list.java index ce4045f..5b5ce3a 100644 --- a/pisi/unitedmeows/meowlib/lists/list.java +++ b/pisi/unitedmeows/meowlib/lists/list.java @@ -1,4 +1,15 @@ package pisi.unitedmeows.meowlib.lists; +import pisi.unitedmeows.meowlib.etc.IFilter; + +import java.util.ArrayList; +import java.util.List; + public class list { + + + public static ArrayList filter_list(List _list, IFilter filter) { + _list.removeIf(x-> !filter.check(x)); + return (ArrayList) _list; + } } diff --git a/pisi/unitedmeows/meowlib/memory/MMemory.java b/pisi/unitedmeows/meowlib/memory/MMemory.java index f259107..2d778ab 100644 --- a/pisi/unitedmeows/meowlib/memory/MMemory.java +++ b/pisi/unitedmeows/meowlib/memory/MMemory.java @@ -1,4 +1,32 @@ package pisi.unitedmeows.meowlib.memory; +import java.util.HashMap; +import java.util.Random; + public class MMemory { + + private static final HashMap variables = new HashMap<>(); + + + public static int write(Object data) { + int pointer = new Random().nextInt(10000); /*todo: change this */ + variables.put(pointer, data); + return pointer; + } + + public static void write(int pointer, Object data) { + variables.put(pointer, data); + } + + public void clear() { + variables.clear(); + } + + public static void remove(int pointer) { + variables.remove(pointer); + } + + public static X read(int pointer) { + return (X) variables.getOrDefault(pointer, null); + } } diff --git a/pisi/unitedmeows/meowlib/predefined/STRING.java b/pisi/unitedmeows/meowlib/predefined/STRING.java index f5ee130..722c5ee 100644 --- a/pisi/unitedmeows/meowlib/predefined/STRING.java +++ b/pisi/unitedmeows/meowlib/predefined/STRING.java @@ -4,7 +4,13 @@ public class STRING { - public static String EMPTY = ""; - public static String DOT = "."; - public static String L_SEPARATOR = System.lineSeparator(); + public static final String EMPTY = ""; + public static final String DOT = "."; + public static final String L_SEPARATOR = System.lineSeparator(); + + /* add your statics methods related to string here */ + + public static String string_reverse(String input) { + return new StringBuilder(input).reverse().toString(); + } } diff --git a/pisi/unitedmeows/meowlib/reflection/Reflect.java b/pisi/unitedmeows/meowlib/reflection/Reflect.java index 207d1ae..a21db71 100644 --- a/pisi/unitedmeows/meowlib/reflection/Reflect.java +++ b/pisi/unitedmeows/meowlib/reflection/Reflect.java @@ -1,4 +1,117 @@ package pisi.unitedmeows.meowlib.reflection; +import pisi.unitedmeows.meowlib.clazz.type; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + public class Reflect { + + public static Object instance(Class clazz) { + try { + return clazz.newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + public static X instance_c(Class clazz) { + try { + return (X) clazz.newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + public static Field field(Class clazz, String fieldName) { + try { + final Field field = clazz.getClass().getDeclaredField(fieldName); + if (!field.isAccessible()) { + field.setAccessible(true); + } + return field; + } catch (NoSuchFieldException e) { + + } + return null; + } + + public static X field(Object object, String fieldName) { + try { + final Field field = object.getClass().getDeclaredField(fieldName); + if (!field.isAccessible()) { + field.setAccessible(true); + } + return (X) field.get(object); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + public static Method method(Class owner, String methodName) { + try { + final Method method = owner.getDeclaredMethod(methodName); + if (!method.isAccessible()) { + method.setAccessible(true); + } + + return method; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } + } + + public static Method method(Class owner, String methodName, Object... parameters) { + try { + final Method method = owner.getDeclaredMethod(methodName, type.type_array(parameters)); + if (!method.isAccessible()) { + method.setAccessible(true); + } + + return method; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } + } + + public static X call_method(Object instance, String method) { + return call_method(instance, method, null); + } + + public static X call_method(Object instance, String method, Object... parameters) { + try { + Method theMethod; + if (parameters.length == 0) { + theMethod = instance.getClass().getDeclaredMethod(method); + } else { + theMethod = instance.getClass().getDeclaredMethod(method, type.type_array(parameters)); + } + if (!theMethod.isAccessible()) { + theMethod.setAccessible(true); + } + if (parameters == null) { + return (X) theMethod.invoke(instance); + } + + return (X)theMethod.invoke(instance, parameters); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return null; + } + + + + } diff --git a/pisi/unitedmeows/meowlib/signal/poc.txt b/pisi/unitedmeows/meowlib/signal/poc.txt index e69de29..1191538 100644 --- a/pisi/unitedmeows/meowlib/signal/poc.txt +++ b/pisi/unitedmeows/meowlib/signal/poc.txt @@ -0,0 +1,11 @@ + + + + + +signal.listen(APP_KEY); + +signal.send(int dataTypeId, byte[] data, app_key) +signal.send(int dataTypeId, string data, app_key) + +signal.discover(); /* discover all apps running in this machine */ diff --git a/test/Start.java b/test/Start.java index a838513..f4d5d67 100644 --- a/test/Start.java +++ b/test/Start.java @@ -1,37 +1,49 @@ package test; import static pisi.unitedmeows.meowlib.async.Async.*; -import static pisi.unitedmeows.meowlib.MeowLib.*; +import java.io.Serializable; import java.util.ArrayList; -import java.util.List; import java.util.Random; -import java.util.UUID; import pisi.unitedmeows.meowlib.async.Future; import pisi.unitedmeows.meowlib.async.Promise; import pisi.unitedmeows.meowlib.async.Task; + +import pisi.unitedmeows.meowlib.clazz.onion; +import pisi.unitedmeows.meowlib.clazz.prop; +import pisi.unitedmeows.meowlib.clazz.type; import pisi.unitedmeows.meowlib.etc.CoID; -import pisi.unitedmeows.meowlib.lists.MeowList; + +import static pisi.unitedmeows.meowlib.lists.list.*; +import pisi.unitedmeows.meowlib.math.MeowMath; +import static pisi.unitedmeows.meowlib.predefined.STRING.*; import pisi.unitedmeows.meowlib.thread.kThread; -import pisi.unitedmeows.meowlib.variables.ubyte; -import pisi.unitedmeows.meowlib.variables.uint; public class Start { private static Future future; - public static void main(String[] args) { + private static prop number = new prop(0) { + @Override + public void set(Integer value) { + /* do something */ + this.value = value; + } + @Override + public Integer get() { + value += 1; + return this.value; + } + }; - async((u)-> System.out.println("Usage 1")); - async((u)-> System.out.println("Usage 2")).after((u)-> System.out.println("test")); + public static onion text = new onion<>(); - async_w((u)-> System.out.println("Usage 3 (this will run after 5000ms)"), 5000); + static int i; + public static void main(String[] args) { - /* defining a variable is not required if you'll not start/stop the loop */ - final Promise promise = async_loop((u)-> System.out.println("Hello World"), 500, 3000); - } + } public static void asyncTest(CoID id) {