Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70d3bb0

Browse files
Add files via upload
1 parent 81a454c commit 70d3bb0

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package head;
2+
3+
import java.io.File;
4+
import java.lang.reflect.Field;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Random;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
11+
import ugp.org.SerialX.Scope;
12+
import ugp.org.SerialX.Serializer;
13+
import ugp.org.SerialX.protocols.SerializationProtocol;
14+
15+
/**
16+
* This example is overview of general SerialX API functionalities!
17+
* We will look at how to serialize and deserialize objects in to file. We will also create protocols for our objects as well as for already existing ones!
18+
*
19+
* @author PETO
20+
*
21+
* @since 1.0.0
22+
*/
23+
public class SimpleExampleOfUse
24+
{
25+
public static void main(String[] args) throws Exception
26+
{
27+
//Protocol registration
28+
SerializationProtocol.REGISTRY.addAll(new Bar.BarProtocol(), new Foo.FooProtocol(), new SerializationProtocol<Random>() //Sample custom protocol to serialized Random.
29+
{ //Random will be serialized also without protocol via classic Java Base64 because it implements java.io.Serializable!
30+
@Override
31+
public Object[] serialize(Random object)
32+
{
33+
try
34+
{
35+
Field f = Random.class.getDeclaredField("seed");
36+
f.setAccessible(true);
37+
return new Object[] {((AtomicLong) f.get(object)).get()};
38+
}
39+
catch (Exception e)
40+
{
41+
e.printStackTrace();
42+
return new Object[] {-1};
43+
}
44+
}
45+
46+
@Override
47+
public Random unserialize(Class<? extends Random> objectClass, Object... args)
48+
{
49+
return new Random((long) args[0]);
50+
}
51+
52+
@Override
53+
public Class<? extends Random> applicableFor()
54+
{
55+
return Random.class;
56+
}
57+
});
58+
59+
File f = new File("./test.juss"); //File to write and read from!
60+
61+
//Sample objects
62+
Random r = new Random();
63+
List<Object> list = new ArrayList<>();
64+
for (int i = 0; i < 10; i++)
65+
list.add(r.nextBoolean() ? r.nextInt(i+1) : r.nextBoolean());
66+
67+
HashMap<String, Object> vars = new HashMap<>(); //Variables to serialize
68+
vars.put("yourMom", "is heavier than sun...");
69+
vars.put("num", 6);
70+
71+
int[][] ints = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}};
72+
73+
Serializer.generateComments = true; //Enabling comment generation
74+
75+
Serializer.globalVariables.put("parent", "father"); //Setting global variables
76+
77+
double t0 = System.nanoTime(); //Invokation of static members of this class (calling method "println" and obtaining "hello" field as argument!
78+
Serializer.SerializeTo(f, vars, "145asaa4144akhdgj31hahaXDDLol", r, list, Serializer.Comment("Size of array"), Serializer.Var("arrSize", list.size()), new Bar(), 1, 2.2, 3, 'A', true, false, null, ints, Serializer.Code("$num"), new Scope(), Serializer.StaticMember(SimpleExampleOfUse.class, "println", Serializer.StaticMember(SimpleExampleOfUse.class, "hello"))); //Saving to file (serializing)
79+
double t = System.nanoTime(); //This will insert an comment Another way to add variable except Map<String, Object> $ is used to obtain value from variable
80+
System.out.println("Write: " + (t-t0)/1000000);
81+
82+
SerializationProtocol.REGISTRY.setActivityForAll(true); //Enabling all protocols, just in case
83+
t0 = System.nanoTime();
84+
Scope scope = Serializer.LoadFrom(f); //Loading scope with variables and values from file!
85+
t = System.nanoTime();
86+
System.out.println("Read: " + (t-t0)/1000000);
87+
88+
scope = scope.filter(obj -> obj != null); //This will filter away every null value and variable!
89+
90+
//Printing values and variables of scope!!
91+
System.out.println(scope.toVarMap());
92+
System.out.println(scope.toValList());
93+
}
94+
95+
//We can invoke static things in JUSS!
96+
public static String hello = "Hello world!";
97+
98+
public static void println(String str)
99+
{
100+
System.out.println(str);
101+
}
102+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package head;
2+
3+
import java.util.Scanner;
4+
5+
import ugp.org.SerialX.Registry;
6+
import ugp.org.SerialX.converters.DataParser;
7+
import ugp.org.SerialX.converters.NumberConverter;
8+
import ugp.org.SerialX.converters.OperationGroups;
9+
import ugp.org.SerialX.converters.operators.ArithmeticOperators;
10+
11+
/**
12+
* This example will show you simple implementation of SerialX latest feature the recursive data parser!
13+
* In this example we will be creating simple evaluator of mathematical expressions!
14+
*
15+
* @since 1.3.0
16+
*/
17+
public class SimpleImplementation
18+
{
19+
static Scanner scIn = new Scanner(System.in);
20+
21+
public static void main(String[] args)
22+
{
23+
/*
24+
* We could easily just use DataParser.REGISTRY but there is tone of stuff we do not need and it will just slow it down!
25+
*/
26+
Registry<DataParser> parsersRequiredToEvaluateMath = new Registry<>(new OperationGroups(), new ArithmeticOperators(), new NumberConverter());
27+
28+
/*
29+
* This is an example of simple custom parser this one will allow us to reuse answers of out previous evaluations!
30+
* We will access this old answer using 'ans' word!
31+
* Old ans must be provided as first one of args!
32+
*/
33+
DataParser ansParser = new DataParser()
34+
{
35+
@Override
36+
public Object parse(Registry<DataParser> myHomeRegistry, String str, Object... args)
37+
{
38+
if (str.equalsIgnoreCase("ans"))
39+
{
40+
if (args.length > 0)
41+
return args[0];
42+
return null;
43+
}
44+
return CONTINUE;
45+
}
46+
};
47+
parsersRequiredToEvaluateMath.add(ansParser);
48+
49+
Object oldAns = null;
50+
while (true)
51+
{
52+
System.out.println("Please insert your math problem!"); //Ask for input!
53+
String input = scIn.nextLine() ;//Read console input
54+
if (!(input = input.trim()).isEmpty()) //Avoiding empty input!
55+
System.out.println(input + " = " + (oldAns = DataParser.parseObj(parsersRequiredToEvaluateMath, input, oldAns))+"\n"); //Parsing input!
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)