Skip to content

Commit

Permalink
Replace git submodule by maven dependency (#67)
Browse files Browse the repository at this point in the history
* Add unit tests for serializer

* Update to org.json:json 20230227

The maven build now uses maven to manage the dependency.

* Update maven build instructions
  • Loading branch information
jvierling authored Sep 16, 2024
1 parent 1cd95c1 commit 025501d
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "JSON-java"]
path = src/main/java/org/json
url = https://github.com/digama0/JSON-java.git
[submodule "lib/JSON-java"]
path = lib/JSON-java
url = https://github.com/stleary/JSON-java.git
12 changes: 2 additions & 10 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,8 @@ follow the Linux/Unix/Cygwin instructions above.

### Building mmj2 with maven

The mmj2 executable JAR file can be built from the sources with maven.
First run the following commands in the `mmj2/` directory to setup the
git submodules used by mmj2:

```
git submodule init
git submodule update
```

After that, build the executable JAR file using the following command:
The mmj2 executable JAR file can be built from the sources with maven
using the following command.

```
mvn package
Expand Down
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="classes" includeantruntime="false" source="${source}" target="${target}">
<src path="src/main/java/mmj/"/>
<src path="src/main/java/org/"/>
<src path="lib/JSON-java/src/main/java/"/>
<classpath refid="mmj2.classpath"/>
</javac>
</target>
Expand Down
1 change: 1 addition & 0 deletions lib/JSON-java
Submodule JSON-java added at 47fb49
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>nashorn-core</artifactId>
<version>15.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/mmj/pa/PaConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
import static mmj.pa.ErrorCode.of;

import java.awt.Color;
import java.util.List;
import java.util.Map;

import javax.swing.text.SimpleAttributeSet;
Expand Down Expand Up @@ -2941,7 +2942,7 @@ public static <T extends MMJException> T addStepContext(
.put(SessionStore.KEY_STORE, new JSONObject())
.put(SessionStore.KEY_OVERRIDE, new JSONObject())
.put(SessionStore.KEY_REMOVE, new JSONArray())
.put("-comments", new JSONArray(
.put("-comments", new JSONArray( List.of(
" ======================= The store.json File ========================== ",
" ",
" mmj2 uses a JSON file for saving and loading settings, called ",
Expand Down Expand Up @@ -2983,7 +2984,7 @@ public static <T extends MMJException> T addStepContext(
" this behavior (so that it instead reverts to some chosen value after ",
" every restart) you can either add a RunParm and a LoadSettings before ",
" that, or you can put the desired key-value pair in the 'manual' ",
" section. "));
" section. ")));

// ----------------------------------------------------------
// Messages from EraseWffsPreprocessRequest.java
Expand Down
41 changes: 22 additions & 19 deletions src/main/java/mmj/pa/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.*;
import java.util.stream.Collectors;

import mmj.util.StreamUtil;
import org.json.*;

import mmj.lang.*;
Expand Down Expand Up @@ -72,10 +73,10 @@ public Object serialize(final T value) {
*/
default Serializer<T[]> array(final IntFunction<T[]> generator) {
return Serializer.of(
o -> ((JSONArray)o).stream().map(this::deserialize)
o -> StreamUtil.stream((JSONArray)o).map(this::deserialize)
.toArray(generator),
v -> Arrays.stream(v).map(this::serialize)
.collect(Collectors.toCollection(JSONArray::new)));
v -> new JSONArray(Arrays.stream(v).map(this::serialize)
.collect(Collectors.toList())));
}

/**
Expand All @@ -86,10 +87,10 @@ default Serializer<T[]> array(final IntFunction<T[]> generator) {
*/
default Serializer<List<T>> list() {
return Serializer.of(
o -> ((JSONArray)o).stream().map(this::deserialize)
o -> StreamUtil.stream((JSONArray)o).map(this::deserialize)
.collect(Collectors.toList()),
v -> v.stream().map(this::serialize)
.collect(Collectors.toCollection(JSONArray::new)));
v -> new JSONArray(v.stream().map(this::serialize)
.collect(Collectors.toList())));
}

/**
Expand All @@ -100,10 +101,10 @@ default Serializer<List<T>> list() {
*/
default Serializer<Set<T>> set() {
return Serializer.of(
o -> ((JSONArray)o).stream().map(this::deserialize)
o -> StreamUtil.stream((JSONArray)o).map(this::deserialize)
.collect(Collectors.toSet()),
v -> v.stream().map(this::serialize)
.collect(Collectors.toCollection(JSONArray::new)));
v -> new JSONArray(v.stream().map(this::serialize)
.collect(Collectors.toList())));
}

/**
Expand All @@ -114,13 +115,13 @@ default Serializer<Set<T>> set() {
*/
default Serializer<Map<String, T>> map() {
return Serializer.of(
o -> ((JSONObject)o).entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> deserialize(e.getValue()))),
(final Map<String, T> v) -> v.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> serialize(e.getValue()), (a, b) -> a,
JSONObject::new)));
o -> ((JSONObject)o).keySet().stream()
.collect(Collectors.toMap(
k -> k,
k -> deserialize(((JSONObject) o).get(k)))),
(final Map<String, T> v) -> new JSONObject(v.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> serialize(e.getValue()), (a, b) -> a))));
}

/**
Expand Down Expand Up @@ -161,8 +162,10 @@ public static Serializer<File> getFileSerializer(
throw new IllegalArgumentException(e);
}
}, value -> {
final JSONArray a = new JSONArray(value.getRed(), value.getGreen(),
value.getBlue());
final JSONArray a = new JSONArray(List.of(
value.getRed(),
value.getGreen(),
value.getBlue()));
return value.getAlpha() == 255 ? a : a.put(value.getAlpha());
});

Expand All @@ -175,7 +178,7 @@ public static Serializer<File> getFileSerializer(
} catch (final JSONException e) {
throw new IllegalArgumentException(e);
}
}, r -> new JSONArray(r.x, r.y, r.width, r.height));
}, r -> new JSONArray(List.of(r.x, r.y, r.width, r.height)));

@SuppressWarnings("unchecked")
public static <T> Serializer<T[]> getArraySerializer(final Class<T> clazz) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/mmj/pa/SessionStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ public JSONObject load(final boolean intoSettings,
JSONArray remove = o.optJSONArray(KEY_REMOVE);
if (remove == null)
o.put(KEY_REMOVE, remove = new JSONArray());
for (final Object key : remove)
store.remove(key);
for (final Object key : remove) {
if (key instanceof String) {
store.remove((String) key);
}
}
remove.clear();
merge(intoSettings, store, errors, settingsToLoad);
merge(true, o.optJSONObject(KEY_OVERRIDE), errors, settingsToLoad);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/mmj/tmff/TMFFAlignColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import mmj.pa.ErrorCode;
import mmj.tmff.TMFFConstants.AlignType;

import java.util.List;

/**
* TMFFAlignColumn aligns portions of a sub-expression into a single column when
* splitting the sub-expression across multiple lines.
Expand Down Expand Up @@ -219,9 +221,9 @@ public TMFFAlignColumn(final String maxDepthString,

@Override
public JSONArray asArray() {
return new JSONArray(TMFFConstants.TMFF_METHOD_USER_NAME_ALIGN_COLUMN,
return new JSONArray(List.of(TMFFConstants.TMFF_METHOD_USER_NAME_ALIGN_COLUMN,
maxDepth, alignByValue.toString(), alignAtNbr,
alignAtValue.toString());
alignAtValue.toString()));
}

/**
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/mmj/tmff/TMFFPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,24 @@ public TMFFPreferences(final SessionStore store) {

// The ~ is so that it sorts at the end with other big keys
store.addSerializable("~" + PFX + "formatArray",
(final JSONObject o) -> o.entrySet().parallelStream()
(final JSONObject o) -> o.toMap().entrySet().parallelStream()
.map(e -> new String[]{e.getKey().toString(),
(String)e.getValue()})
.forEach(read),
() -> Arrays.stream(tmffFormatArray).filter(f -> f != null)
() -> new JSONObject(Arrays.stream(tmffFormatArray).filter(f -> f != null)
.collect(Collectors.toMap(f -> f.getFormatNbr() + "",
f -> f.getFormatScheme().getSchemeName(), (a, b) -> a,
JSONObject::new)));
f -> f.getFormatScheme().getSchemeName(), (a, b) -> a))));

store.addSerializable("~" + PFX + "schemeMap", (final JSONObject o) -> {
for (final Entry<String, Object> e : o.entrySet()) {
final List<Object> a = new ArrayList<>(
(JSONArray)e.getValue());
for (final Entry<String, Object> e : o.toMap().entrySet()) {
final List<Object> a = new ArrayList<>(((JSONArray)e.getValue()).toList());
a.add(0, e.getKey());
putToSchemeMap(new TMFFScheme(
a.stream().map(Object::toString).toArray(String[]::new)));
}
} , () -> tmffSchemeMap.values().parallelStream()
} , () -> new JSONObject(tmffSchemeMap.values().parallelStream()
.collect(Collectors.toMap(s -> s.getSchemeName(),
s -> s.getTMFFMethod().asArray(), (a, b) -> a,
JSONObject::new)));
s -> s.getTMFFMethod().asArray(), (a, b) -> a))));

}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/mmj/tmff/TMFFTwoColumnAlignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import mmj.lang.*;

import java.util.List;

/**
* TMFFTwoColumnAlignment aligns portions of a sub-expression into a two columns
* when splitting the sub-expression across multiple lines: the left column is
Expand Down Expand Up @@ -104,8 +106,8 @@ public TMFFTwoColumnAlignment(final String maxDepthString) {

@Override
public JSONArray asArray() {
return new JSONArray(
TMFFConstants.TMFF_METHOD_USER_NAME_TWO_COLUMN_ALIGNMENT, maxDepth);
return new JSONArray(List.of(
TMFFConstants.TMFF_METHOD_USER_NAME_TWO_COLUMN_ALIGNMENT, maxDepth));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/mmj/util/StreamUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mmj.util;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class StreamUtil {
public static <T> Stream<T> stream(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}
}
4 changes: 2 additions & 2 deletions src/main/java/mmj/verify/LRParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private void initialize() {
// state where the only valid move is to shift '}'.
boolean bad = false;
for (final String head : badTokens)
bad |= rows.get(fwd).transitions.containsKey(head);
bad |= rows.get(fwd).transitions.toMap().containsKey(head);
if (bad) {
// (Let's pretend that the above optimization did not take
// place so we can continue the example.)
Expand Down Expand Up @@ -406,7 +406,7 @@ private void developState(final Integer index) {
else
reduce = e.rule;
}
else if (!row.transitions.containsKey(head.getId())) {
else if (!row.transitions.toMap().containsKey(head.getId())) {
final ParseSet goal = new ParseSet();
set.forEach(state -> {
if (head.equals(state.head()))
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/json
Submodule json deleted from edfb0c
Loading

0 comments on commit 025501d

Please sign in to comment.