Skip to content
This repository has been archived by the owner on May 29, 2022. It is now read-only.

Commit

Permalink
async_when | async_while | async_loop_times | async_loop_till | async…
Browse files Browse the repository at this point in the history
…_loop_times_w | async_loop_till_w | removed some redundant code
  • Loading branch information
slowcheet4h committed May 22, 2021
1 parent 6e16e3c commit a38d182
Show file tree
Hide file tree
Showing 18 changed files with 494 additions and 36 deletions.
104 changes: 95 additions & 9 deletions pisi/unitedmeows/meowlib/async/Async.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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> promise = new prop<Promise>();
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> promise = new prop<Promise>();
promise.set(async_wloop(u -> {
if (!state.state()) {
promise.get().stop();
}
async_f(asyncAction);
}, loopInterval));
return promise.get();
}

public static <X> Future<X> async_f(IAsyncAction action) {
// change this uuid alternative
Expand All @@ -44,7 +65,7 @@ public void run() {
}

public static <X> Future<X> async(IAsyncAction action) {
// change this uuid alternative

final CoID pointer = newPointer();

Future<X> future = new Future<>(pointer);
Expand Down Expand Up @@ -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();
Expand All @@ -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<Object>(action) {
@Override
Expand Down
1 change: 0 additions & 1 deletion pisi/unitedmeows/meowlib/async/BasicTaskPool.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
20 changes: 16 additions & 4 deletions pisi/unitedmeows/meowlib/async/Future.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,7 +11,7 @@ public class Future<X> {
private X value;
private CoID pointer;
private Task task;
private transient List<IAsyncAction> afterTasks;
private transient List<Tuple<Long, IAsyncAction>> afterTasks;

public Future(CoID pointer) {
this.pointer = pointer;
Expand All @@ -26,8 +27,12 @@ public Task<X> task() {

public void post() {
if (afterTasks != null) {
for (IAsyncAction afterTask : afterTasks) {
Async.async(afterTask);
for (Tuple<Long, IAsyncAction> afterTask : afterTasks) {
if (afterTask.getFirst() == 0) {
Async.async_f(afterTask.getSecond());
} else {
Async.async_w(afterTask.getSecond(), afterTask.getFirst());
}
}
afterTasks.clear();
}
Expand All @@ -39,7 +44,14 @@ public void after(IAsyncAction task) {
if (afterTasks == null) {
afterTasks = new ArrayList<>();
}
afterTasks.add(task);
afterTasks.add(new Tuple<Long, IAsyncAction>(0L, task));
}

public void after_w(IAsyncAction task, long time) {
if (afterTasks == null) {
afterTasks = new ArrayList<>();
}
afterTasks.add(new Tuple<Long, IAsyncAction>(time, task));
}


Expand Down
1 change: 0 additions & 1 deletion pisi/unitedmeows/meowlib/async/TaskWorker.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions pisi/unitedmeows/meowlib/async/states/IState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pisi.unitedmeows.meowlib.async.states;

@FunctionalInterface
public interface IState {

boolean state();
}
5 changes: 4 additions & 1 deletion pisi/unitedmeows/meowlib/clazz/IProperty.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package pisi.unitedmeows.meowlib.clazz;

public class IProperty {
public interface IProperty<X> {

void set(X value);
X get();
}
37 changes: 36 additions & 1 deletion pisi/unitedmeows/meowlib/clazz/onion.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
package pisi.unitedmeows.meowlib.clazz;

public class onion {
import pisi.unitedmeows.meowlib.memory.MMemory;

public class onion<X> {

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;
}
}
}
22 changes: 21 additions & 1 deletion pisi/unitedmeows/meowlib/clazz/prop.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
package pisi.unitedmeows.meowlib.clazz;

public class prop {
import pisi.unitedmeows.meowlib.async.Promise;

public class prop<X> implements IProperty<X> {

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;
}
}
40 changes: 40 additions & 0 deletions pisi/unitedmeows/meowlib/clazz/type.java
Original file line number Diff line number Diff line change
@@ -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> 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();
}
}
33 changes: 32 additions & 1 deletion pisi/unitedmeows/meowlib/encryption/memory/MString.java
Original file line number Diff line number Diff line change
@@ -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<MString> {

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;
}
}
4 changes: 3 additions & 1 deletion pisi/unitedmeows/meowlib/etc/IFilter.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package pisi.unitedmeows.meowlib.etc;

public class Filtere {
public interface IFilter<X> {

boolean check(X object);
}
Loading

0 comments on commit a38d182

Please sign in to comment.