Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-Nauwynck committed Mar 9, 2023
1 parent 30b5b39 commit d6fff81
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test-visitor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>fr.an.tests</groupId>
<artifactId>test-visitor</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
104 changes: 104 additions & 0 deletions test-visitor/src/fr/iut/tp2/ArrayJsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package fr.iut.tp2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

public class ArrayJsonNode extends JsonNode {

private final List<JsonNode> child = new ArrayList<>();

// ------------------------------------------------------------------------

public ArrayJsonNode() {
}

public ArrayJsonNode(JsonNode... elements) {
child.addAll(Arrays.asList(elements));
}

public ArrayJsonNode(List<JsonNode> elements) {
child.addAll(elements);
}

// ------------------------------------------------------------------------

public void accept(JsonNodeVisitor v) {
v.caseArray(this);
}

public <T> T accept(JsonNodeVisitor2<T> v) {
return v.caseArray(this);
}

public List<JsonNode> getValue() {
return child;
}

public Iterator<JsonNode> iterator() {
return child.iterator();
}



// ------------------------------------------------------------------------

public static class Builder {

private final List<JsonNode> child = new ArrayList<>();

public ArrayJsonNode build() {
return new ArrayJsonNode(child);
}

public Builder addNull() {
child.add(new NullJsonNode());
return this;
}

public Builder add(JsonNode value) {
child.add(value);
return this;
}

public Builder add(boolean b) {
child.add(new BooleanJsonNode(true));
return this;
}

public Builder add(double value) {
child.add(new NumericJsonNode(value));
return this;
}

public Builder add(String value) {
child.add(new TextJsonNode(value));
return this;
}

public Builder add(JsonNode... elements) {
child.add(new ArrayJsonNode(elements));
return this;
}

public Builder addArray(Consumer<ArrayJsonNode.Builder> cb) {
ArrayJsonNode.Builder childBuilder = new ArrayJsonNode.Builder();
cb.accept(childBuilder);
ArrayJsonNode value = childBuilder.build();
child.add(value);
return this;
}

public Builder addObj(Consumer<ObjectJsonNode.Builder> cb) {
ObjectJsonNode.Builder childBuilder = new ObjectJsonNode.Builder();
cb.accept(childBuilder);
ObjectJsonNode value = childBuilder.build();
child.add(value);
return this;
}
}


}
31 changes: 31 additions & 0 deletions test-visitor/src/fr/iut/tp2/BooleanJsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.iut.tp2;

public class BooleanJsonNode extends JsonNode {

private boolean value;

public BooleanJsonNode() {
}

public BooleanJsonNode(boolean p) {
this.value = p;
}


public void accept(JsonNodeVisitor v) {
v.caseBoolean(this);
}

public <T> T accept(JsonNodeVisitor2<T> v) {
return v.caseBoolean(this);
}

public boolean isValue() {
return value;
}

public void setValue(boolean value) {
this.value = value;
}

}
15 changes: 15 additions & 0 deletions test-visitor/src/fr/iut/tp2/JsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.iut.tp2;

public abstract class JsonNode {

public abstract void accept(JsonNodeVisitor v);

public abstract <T> T accept(JsonNodeVisitor2<T> v);


public String toJson() {
PrintJsonVisitor v = new PrintJsonVisitor();
return this.accept(v);
}

}
12 changes: 12 additions & 0 deletions test-visitor/src/fr/iut/tp2/JsonNodeVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.iut.tp2;

public interface JsonNodeVisitor {

public abstract void caseNull(NullJsonNode p);
public abstract void caseBoolean(BooleanJsonNode p);
public abstract void caseNumeric(NumericJsonNode p);
public abstract void caseText(TextJsonNode p);
public abstract void caseArray(ArrayJsonNode p);
public abstract void caseObject(ObjectJsonNode p);

}
12 changes: 12 additions & 0 deletions test-visitor/src/fr/iut/tp2/JsonNodeVisitor2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.iut.tp2;

public interface JsonNodeVisitor2<T> {

public abstract T caseNull(NullJsonNode p);
public abstract T caseBoolean(BooleanJsonNode p);
public abstract T caseNumeric(NumericJsonNode p);
public abstract T caseText(TextJsonNode p);
public abstract T caseArray(ArrayJsonNode p);
public abstract T caseObject(ObjectJsonNode p);

}
14 changes: 14 additions & 0 deletions test-visitor/src/fr/iut/tp2/NullJsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.iut.tp2;

public class NullJsonNode extends JsonNode {

public void accept(JsonNodeVisitor v) {
v.caseNull(this);
}

public <T> T accept(JsonNodeVisitor2<T> v) {
return v.caseNull(this);
}


}
31 changes: 31 additions & 0 deletions test-visitor/src/fr/iut/tp2/NumericJsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.iut.tp2;

public class NumericJsonNode extends JsonNode {

private Number value;


public NumericJsonNode() {
}

public NumericJsonNode(Number value) {
this.value = value;
}

public void accept(JsonNodeVisitor v) {
v.caseNumeric(this);
}

public <T> T accept(JsonNodeVisitor2<T> v) {
return v.caseNumeric(this);
}

public Number getValue() {
return value;
}

public void setValue(Number value) {
this.value = value;
}

}
100 changes: 100 additions & 0 deletions test-visitor/src/fr/iut/tp2/ObjectJsonNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package fr.iut.tp2;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;

public class ObjectJsonNode extends JsonNode {

private final Map<String,JsonNode> child = new LinkedHashMap<>();

public ObjectJsonNode() {
}

public ObjectJsonNode(Map<String, JsonNode> src) {
child.putAll(src);
}

public void accept(JsonNodeVisitor v) {
v.caseObject(this);
}

public <T> T accept(JsonNodeVisitor2<T> v) {
return v.caseObject(this);
}


public Map<String,JsonNode> getValue() {
return child;
}

public JsonNode get(String key) {
return child.get(key);
}

public JsonNode put(String key, JsonNode value) {
return child.put(key, value);
}

public JsonNode remove(String key) {
return child.remove(key);
}

// ------------------------------------------------------------------------

public static class Builder {

private final Map<String,JsonNode> child = new LinkedHashMap<>();

public ObjectJsonNode build() {
return new ObjectJsonNode(child);
}

public Builder putNull(String key) {
child.put(key, new NullJsonNode());
return this;
}

public Builder put(String key, JsonNode value) {
child.put(key, value);
return this;
}

public Builder put(String key, boolean b) {
child.put(key, new BooleanJsonNode(true));
return this;
}

public Builder put(String key, double value) {
child.put(key, new NumericJsonNode(value));
return this;
}

public Builder put(String key, String value) {
child.put(key, new TextJsonNode(value));
return this;
}

public Builder putArray(String key, JsonNode... elements) {
child.put(key, new ArrayJsonNode(elements));
return this;
}

public Builder putArray(String key, Consumer<ArrayJsonNode.Builder> cb) {
ArrayJsonNode.Builder childBuilder = new ArrayJsonNode.Builder();
cb.accept(childBuilder);
ArrayJsonNode value = childBuilder.build();
child.put(key, value);
return this;
}

public Builder putObj(String key, Consumer<Builder> cb) {
Builder childBuilder = new Builder();
cb.accept(childBuilder);
ObjectJsonNode value = childBuilder.build();
child.put(key, value);
return this;
}
}

}
Loading

0 comments on commit d6fff81

Please sign in to comment.