Skip to content

Commit 2ccc6e7

Browse files
committed
Initial commit
0 parents  commit 2ccc6e7

File tree

9 files changed

+636
-0
lines changed

9 files changed

+636
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.iml
3+
target

pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.javawebstack</groupId>
8+
<artifactId>Graph</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.google.code.gson</groupId>
14+
<artifactId>gson</artifactId>
15+
<version>2.8.6</version>
16+
</dependency>
17+
</dependencies>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>8</source>
26+
<target>8</target>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
32+
33+
</project>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.javawebstack.graph;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
6+
import java.util.ArrayList;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.stream.Stream;
11+
12+
public class GraphArray implements GraphElement, Iterable<GraphElement> {
13+
14+
private final List<GraphElement> elements = new ArrayList<>();
15+
16+
public boolean isArray() {
17+
return true;
18+
}
19+
20+
public GraphArray array() {
21+
return this;
22+
}
23+
24+
public GraphArray add(GraphElement element) {
25+
if (element == null)
26+
element = GraphNull.INSTANCE;
27+
elements.add(element);
28+
return this;
29+
}
30+
31+
public GraphArray addNull() {
32+
return add(GraphNull.INSTANCE);
33+
}
34+
35+
public GraphArray add(Number value) {
36+
if (value == null)
37+
return addNull();
38+
return add(new GraphPrimitive(value));
39+
}
40+
41+
public GraphArray add(Boolean value) {
42+
if (value == null)
43+
return addNull();
44+
return add(new GraphPrimitive(value));
45+
}
46+
47+
public GraphArray add(String value) {
48+
if (value == null)
49+
return addNull();
50+
return add(new GraphPrimitive(value));
51+
}
52+
53+
public GraphArray setNull(int i){
54+
elements.set(i, GraphNull.INSTANCE);
55+
return this;
56+
}
57+
58+
public GraphArray set(int i, GraphElement element){
59+
elements.set(i, element);
60+
return this;
61+
}
62+
63+
public GraphArray set(int i, Number value){
64+
if(value == null)
65+
return setNull(i);
66+
return set(i, new GraphPrimitive(value));
67+
}
68+
69+
public GraphArray set(int i, Boolean value){
70+
if(value == null)
71+
return setNull(i);
72+
return set(i, new GraphPrimitive(value));
73+
}
74+
75+
public GraphArray set(int i, String value){
76+
if(value == null)
77+
return setNull(i);
78+
return set(i, new GraphPrimitive(value));
79+
}
80+
81+
public GraphArray remove(int i){
82+
elements.remove(i);
83+
return this;
84+
}
85+
86+
public GraphElement get(int i){
87+
return elements.get(i);
88+
}
89+
90+
public Stream<GraphElement> stream(){
91+
return elements.stream();
92+
}
93+
94+
public int size() {
95+
return elements.size();
96+
}
97+
98+
public GraphArray clear() {
99+
elements.clear();
100+
return this;
101+
}
102+
103+
public Iterator<GraphElement> iterator() {
104+
return elements.iterator();
105+
}
106+
107+
public JsonElement toJson() {
108+
JsonArray array = new JsonArray();
109+
elements.forEach(e -> array.add(e.toJson()));
110+
return array;
111+
}
112+
113+
public static GraphArray fromJson(JsonArray array){
114+
GraphArray a = new GraphArray();
115+
array.forEach(e -> a.add(GraphElement.fromJson(e)));
116+
return a;
117+
}
118+
119+
public Type getType() {
120+
return Type.ARRAY;
121+
}
122+
123+
public GraphObject object(){
124+
GraphObject o = new GraphObject();
125+
for(int i=0; i<size(); i++)
126+
o.set(String.valueOf(i), get(i));
127+
return o;
128+
}
129+
130+
public Map<String[], Object> toTree(){
131+
return object().toTree();
132+
}
133+
134+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package org.javawebstack.graph;
2+
3+
import com.google.gson.JsonElement;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public interface GraphElement {
9+
Type getType();
10+
default boolean isPrimitive(){
11+
return false;
12+
}
13+
default boolean isObject(){
14+
return false;
15+
}
16+
default boolean isArray(){
17+
return false;
18+
}
19+
default boolean isNull(){
20+
return false;
21+
}
22+
default boolean isNumber(){
23+
return false;
24+
}
25+
default boolean isBoolean(){
26+
return false;
27+
}
28+
default boolean isString(){
29+
return false;
30+
}
31+
default GraphPrimitive primitive(){
32+
return null;
33+
}
34+
default GraphArray array(){
35+
return null;
36+
}
37+
default GraphObject object(){
38+
return null;
39+
}
40+
default String string(){
41+
if(!isString())
42+
return null;
43+
return primitive().string();
44+
}
45+
default Boolean bool(){
46+
if(!isBoolean())
47+
return null;
48+
return primitive().bool();
49+
}
50+
default Number number(){
51+
if(!isNumber())
52+
return null;
53+
return primitive().number();
54+
}
55+
JsonElement toJson();
56+
57+
default Map<String, String> toFormData(){
58+
Map<String[], Object> tree = toTree();
59+
Map<String, String> data = new HashMap<>();
60+
for(String[] key : tree.keySet()){
61+
if(key == null || key.length == 0)
62+
continue;
63+
StringBuilder sb = new StringBuilder(key[0]);
64+
for(int i=1; i<key.length; i++)
65+
sb.append('[').append(key[i]).append(']');
66+
Object value = tree.get(key);
67+
data.put(sb.toString(), value == null ? "" : value.toString());
68+
}
69+
return data;
70+
}
71+
72+
Map<String[], Object> toTree();
73+
74+
static GraphElement fromJson(JsonElement element){
75+
if(element == null)
76+
return null;
77+
if(element.isJsonArray())
78+
return GraphArray.fromJson(element.getAsJsonArray());
79+
if(element.isJsonObject())
80+
return GraphObject.fromJson(element.getAsJsonObject());
81+
if(element.isJsonPrimitive())
82+
return GraphPrimitive.fromJson(element.getAsJsonPrimitive());
83+
return GraphNull.INSTANCE;
84+
}
85+
86+
static GraphElement fromTree(Map<String[], Object> tree){
87+
GraphObject object = new GraphObject();
88+
for(String[] key : tree.keySet()){
89+
GraphObject current = object;
90+
Object value = tree.get(key);
91+
int offset = 0;
92+
while (key.length - offset > 1){
93+
if(current.has(key[offset])){
94+
current = current.get(key[offset]).object();
95+
}else{
96+
GraphObject n = new GraphObject();
97+
current.set(key[offset], n);
98+
current = n;
99+
}
100+
offset++;
101+
}
102+
current.set(key[offset], GraphPrimitive.from(value));
103+
}
104+
return object;
105+
}
106+
107+
static GraphElement fromFormData(Map<String, String> formData){
108+
Map<String[], Object> tree = new HashMap<>();
109+
for(String k : formData.keySet()){
110+
String[] key = k.split("\\[");
111+
for(int i=1; i<key.length; i++)
112+
key[i] = key[i].substring(0, key[i].length()-1);
113+
tree.put(key, formData.get(k));
114+
}
115+
return fromTree(tree);
116+
}
117+
118+
enum Type {
119+
NULL,
120+
STRING,
121+
NUMBER,
122+
BOOLEAN,
123+
OBJECT,
124+
ARRAY;
125+
public boolean isPrimitive(){
126+
return this == NUMBER || this == BOOLEAN || this == STRING;
127+
}
128+
}
129+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.javawebstack.graph;
2+
3+
import com.google.gson.*;
4+
import com.google.gson.annotations.Expose;
5+
6+
public class GraphMapper {
7+
8+
private Gson gson;
9+
private NamingPolicy namingPolicy = NamingPolicy.NONE;
10+
private String dateFormat = "yyyy-MM-dd HH:mm:ss";
11+
private boolean exposeRequired = false;
12+
13+
public GraphMapper setNamingPolicy(NamingPolicy namingPolicy){
14+
this.namingPolicy = namingPolicy;
15+
gson = null;
16+
return this;
17+
}
18+
19+
public NamingPolicy getNamingPolicy() {
20+
return namingPolicy;
21+
}
22+
23+
public GraphMapper setExposeRequired(boolean exposeRequired) {
24+
this.exposeRequired = exposeRequired;
25+
gson = null;
26+
return this;
27+
}
28+
29+
public boolean isExposeRequired() {
30+
return exposeRequired;
31+
}
32+
33+
public GraphMapper setDateFormat(String dateFormat) {
34+
this.dateFormat = dateFormat;
35+
gson = null;
36+
return this;
37+
}
38+
39+
public String getDateFormat() {
40+
return dateFormat;
41+
}
42+
43+
private Gson gson(){
44+
if(gson != null)
45+
return gson;
46+
GsonBuilder builder = new GsonBuilder()
47+
.setFieldNamingPolicy(namingPolicy.getGsonPolicy());
48+
if(dateFormat != null)
49+
builder.setDateFormat(dateFormat);
50+
if(exposeRequired){
51+
builder.excludeFieldsWithoutExposeAnnotation();
52+
}else{
53+
builder.setExclusionStrategies(new ExclusionStrategy() {
54+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
55+
return fieldAttributes.getAnnotation(Expose.class) != null && !fieldAttributes.getAnnotation(Expose.class).serialize();
56+
}
57+
public boolean shouldSkipClass(Class<?> aClass) { return false; }
58+
});
59+
}
60+
return builder.create();
61+
}
62+
63+
public GraphElement toGraph(Object object){
64+
return GraphElement.fromJson(gson().toJsonTree(object));
65+
}
66+
67+
public <T> T fromGraph(GraphElement element, Class<T> type){
68+
return gson().fromJson(element.toJson(), type);
69+
}
70+
71+
72+
73+
}

0 commit comments

Comments
 (0)