-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support nested objects in auto-sort (#115)
- Loading branch information
Showing
16 changed files
with
667 additions
and
132 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/main/java/com/askimed/nf/test/lang/channels/ChannelItemComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.askimed.nf.test.lang.channels; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
|
||
import com.askimed.nf.test.util.AnsiColors; | ||
|
||
public class ChannelItemComparator implements Comparator<Object> { | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public int compare(Object o1, Object o2) { | ||
|
||
if (o1 instanceof ArrayList) { | ||
ArrayList<Object> tuple1 = (ArrayList<Object>) o1; | ||
ArrayList<Object> tuple2 = (ArrayList<Object>) o2; | ||
for (int i = 0; i < tuple1.size(); i++) { | ||
Object a = tuple1.get(i); | ||
Object b = tuple2.get(i); | ||
|
||
int result = compareObjects(a, b); | ||
|
||
if (result != 0) { | ||
return result; | ||
} | ||
|
||
} | ||
} else { | ||
return compareObjects(o1, o2); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public int compareObjects(Object a, Object b) { | ||
|
||
if (a.getClass() != b.getClass()) { | ||
System.err.println(AnsiColors | ||
.yellow("\nWarning: Cannot sort channel, order not deterministic. Objects are different types: " | ||
+ a.getClass() + " vs. " + b.getClass())); | ||
return 1; | ||
} | ||
|
||
if (a instanceof String) { | ||
if (isPath(a)) { | ||
return comparePaths(a.toString(), b.toString()); | ||
} else { | ||
return compareStrings(a.toString(), b.toString()); | ||
} | ||
} else if (isNumber(a)) { | ||
return compareNumbers((Comparable) a, (Comparable) b); | ||
} else if (a instanceof Map) { | ||
return compareMaps((Map) a, (Map) b); | ||
} else { | ||
System.err.println(AnsiColors | ||
.yellow("\nWarning: Cannot sort channel, order not deterministic. Unsupported objects types: " | ||
+ a.getClass() + " vs. " + b.getClass())); | ||
return 1; | ||
|
||
} | ||
|
||
} | ||
|
||
private boolean isNumber(Object a) { | ||
return a instanceof Integer || a instanceof Double || a instanceof Float; | ||
} | ||
|
||
private boolean isPath(Object a) { | ||
return a.toString().startsWith("/"); | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes" }) | ||
private int compareMaps(Map a, Map b) { | ||
// since we converted all nested maps to treemaps, toString returns keys sorted in deterministic order. | ||
return compareStrings(a.toString(), b.toString()); | ||
} | ||
|
||
public int comparePaths(String a, String b) { | ||
String name1 = new File(a).getName(); | ||
String name2 = new File(b).getName(); | ||
return name1.compareTo(name2); | ||
} | ||
|
||
public int compareStrings(String a, String b) { | ||
return a.compareTo(b); | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public int compareNumbers(Comparable a, Comparable b) { | ||
return a.compareTo(b); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/askimed/nf/test/lang/channels/Channels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.askimed.nf.test.lang.channels; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import com.askimed.nf.test.util.AnsiText; | ||
import com.askimed.nf.test.util.MapUtil; | ||
|
||
import groovy.json.JsonOutput; | ||
import groovy.json.JsonSlurper; | ||
|
||
public class Channels extends TreeMap<Object, Object> { | ||
|
||
private static final String OUTPUT_CHANNEL_PREFIX = "output_"; | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public void loadFromFolder(File folder, boolean autoSort) { | ||
for (File file : folder.listFiles()) { | ||
if (file.getName().startsWith(OUTPUT_CHANNEL_PREFIX)) { | ||
loadFromFile(file, autoSort); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void loadFromFile(File file, boolean autoSort) { | ||
JsonSlurper jsonSlurper = new JsonSlurper(); | ||
Map<String, Object> map = (Map<String, Object>) jsonSlurper.parse(file); | ||
//convert map to treemap to sort keys of nested objects | ||
map = MapUtil.convertToTreeMap(map); | ||
if (autoSort) { | ||
for (Object key : map.keySet()) { | ||
Object value = map.get(key); | ||
List<Object> channel = (List<Object>) value; | ||
sortChannel(channel); | ||
} | ||
} | ||
putAll(map); | ||
} | ||
|
||
public void view() { | ||
System.out.println(AnsiText.padding(JsonOutput.prettyPrint(JsonOutput.toJson(this)), 4)); | ||
} | ||
|
||
public void sortChannel(List<Object> channel) { | ||
channel.sort(new ChannelItemComparator()); | ||
} | ||
|
||
@Override | ||
public Object get(Object key) { | ||
return super.get(key.toString()); | ||
} | ||
} |
127 changes: 0 additions & 127 deletions
127
src/main/java/com/askimed/nf/test/lang/process/ChannelsOutput.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.askimed.nf.test.util; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.Vector; | ||
|
||
public class MapUtil { | ||
|
||
public static TreeMap<String, Object> convertToTreeMap(Map<String, Object> hashMap) { | ||
TreeMap<String, Object> treeMap = new TreeMap<>(); | ||
convertToTreeMap(hashMap, treeMap); | ||
return treeMap; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static void convertToTreeMap(Map<String, Object> sourceMap, TreeMap<String, Object> targetMap) { | ||
for (Map.Entry<String, Object> entry : sourceMap.entrySet()) { | ||
String currentKey = entry.getKey(); | ||
|
||
if (entry.getValue() instanceof Map) { | ||
// nested map | ||
TreeMap<String, Object> nestedTreeMap = new TreeMap<>(); | ||
targetMap.put(currentKey, nestedTreeMap); | ||
convertToTreeMap((Map<String, Object>) entry.getValue(), nestedTreeMap); | ||
} else if (entry.getValue() instanceof List) { | ||
// and array | ||
List<Object> targetList = new Vector<Object>(); | ||
for (Object item : (List<Object>) entry.getValue()) { | ||
// nested map in array | ||
if (item instanceof Map) { | ||
TreeMap<String, Object> treeMap = new TreeMap<>(); | ||
convertToTreeMap((Map<String, Object>) item, treeMap); | ||
targetList.add(treeMap); | ||
} else { | ||
targetList.add(item); | ||
} | ||
} | ||
targetMap.put(currentKey, targetList); | ||
} else { | ||
targetMap.put(currentKey, entry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.