Skip to content

Commit

Permalink
Capsule
Browse files Browse the repository at this point in the history
  • Loading branch information
slowcheet4h committed Jan 14, 2022
1 parent ce7d119 commit 354c726
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 deletions.
1 change: 1 addition & 0 deletions pisi/unitedmeows/yystal/YYStal.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static void setCurrentPool(ITaskPool taskPool) {
currentPool.register();
}


public static void pop(YEx ex) {
YExManager.pop(ex);
}
Expand Down
50 changes: 50 additions & 0 deletions pisi/unitedmeows/yystal/utils/Capsule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package pisi.unitedmeows.yystal.utils;

import pisi.unitedmeows.yystal.clazz.function;
import pisi.unitedmeows.yystal.parallel.IFunction;

import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class Capsule {

protected final HashMap<String, Object> parameters;

public static Capsule of(Pair<String, ?>... params) {
final Capsule capsule = new Capsule(params.length);
for (Pair<String, ?> param : params) {
capsule.parameters.put(param.item1(), param.item2());
}
return capsule;
}

protected Capsule(int paramCount) {
parameters = new HashMap<>(paramCount);
}

public <X> X get(String paramName) {
return (X) parameters.getOrDefault(paramName, null);
}

public <X> X getOrDefault(String paramName, X defaultVal) {
return (X) parameters.getOrDefault(paramName, defaultVal);
}

public boolean contains(String paramName) {
return parameters.containsKey(paramName);
}

public boolean exists() {
return !parameters.isEmpty();
}

public <X> X ifExists(String paramName, Consumer<X> func) {
X value = get(paramName);
if (value != null) {
func.accept(value);
}
return value;
}

}
38 changes: 0 additions & 38 deletions test/yystal/Start.java

This file was deleted.

26 changes: 26 additions & 0 deletions test/yystal/YTestStart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package test.yystal;

import pisi.unitedmeows.yystal.utils.Capsule;
import pisi.unitedmeows.yystal.utils.kThread;


import static pisi.unitedmeows.yystal.parallel.Async.*;
import static pisi.unitedmeows.yystal.YYStal.*;

public enum YTestStart {
gaming; /* :D */

public static void main(final String[] args) {

Capsule capsule = Capsule.of(pair("hello", 5), pair("var2", ":DDD"), pair("token", 12));

System.out.println((String)capsule.get("hello"));
capsule.ifExists("var2", o -> {
System.out.println("var2 exists");
});

kThread.sleep(1099900);
}


}

0 comments on commit 354c726

Please sign in to comment.