refactored sorters

This commit is contained in:
Brian Long 2022-10-10 22:16:34 -04:00
parent 99d76df708
commit cc973b0a28
6 changed files with 126 additions and 57 deletions

View File

@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inteligr8.maven.aps.modeling.util.ApsModelArrayNodeSorter;
import com.inteligr8.maven.aps.modeling.util.ArrayNodeObjectSorter;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;
/**
@ -66,20 +66,27 @@ public class ApsAppJsonNormalizer implements ApsFileNormalizer {
// remove system-level and non-functional fields from models
for (JsonNode _jsonModel : jsonModels) {
ObjectNode jsonModel = (ObjectNode)_jsonModel;
int fields = jsonModel.size();
jsonModel.remove(Arrays.asList("lastUpdated"));
// jsonModel.remove(Arrays.asList("createdBy", "createdByFullName", "lastUpdatedBy", "lastUpdatedByFullName", "lastUpdated"));
if (jsonModel.size() < fields)
changed = true;
changed = this.transformModel(jsonModel) || changed;
}
// sort the models for better 'diff' support
if (this.enableSorting)
changed = ApsModelArrayNodeSorter.getInstance().sort(jsonModels, "name") || changed;
if (this.enableSorting) {
boolean sorted = ArrayNodeObjectSorter.getInstance().sort(jsonModels, "name");
this.logger.trace("Sorted App models: {}: {}", modelName, sorted);
changed = sorted || changed;
}
if (changed)
ModelUtil.getInstance().writeJson(descriptor, file, this.enableSorting);
}
private boolean transformModel(ObjectNode jsonModel) {
this.logger.trace("Removing excess App model meta-data: {}", jsonModel.get("name"));
int fields = jsonModel.size();
jsonModel.remove(Arrays.asList("lastUpdated"));
// jsonModel.remove(Arrays.asList("createdBy", "createdByFullName", "lastUpdatedBy", "lastUpdatedByFullName", "lastUpdated"));
return jsonModel.size() < fields;
}
}

View File

@ -23,7 +23,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inteligr8.maven.aps.modeling.util.ApsModelArrayNodeSorter;
import com.inteligr8.maven.aps.modeling.util.ArrayNodeObjectSorter;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;
/**

View File

@ -1,22 +0,0 @@
package com.inteligr8.maven.aps.modeling.util;
import com.fasterxml.jackson.databind.node.ArrayNode;
public class ApsModelArrayNodeSorter extends ArrayNodeObjectSorter {
private static ApsModelArrayNodeSorter INSTANCE = new ApsModelArrayNodeSorter();
public static ApsModelArrayNodeSorter getInstance() {
return INSTANCE;
}
protected ApsModelArrayNodeSorter() {
}
public boolean sort(ArrayNode arrayNode, String jsonObjectFieldName) {
return this.sort(arrayNode, new ObjectNodeComparator(jsonObjectFieldName));
}
}

View File

@ -1,23 +0,0 @@
package com.inteligr8.maven.aps.modeling.util;
import java.util.List;
import java.util.Map;
public class ApsModelListMapSorter extends ListMapSorter<String, Object> {
private static ApsModelListMapSorter INSTANCE = new ApsModelListMapSorter();
public static ApsModelListMapSorter getInstance() {
return INSTANCE;
}
protected ApsModelListMapSorter() {
}
public boolean sort(List<Map<String, Object>> jsonArrayAsMap, String jsonObjectFieldName) {
return this.sort(jsonArrayAsMap, new MapComparator<String, Object>(jsonObjectFieldName));
}
}

View File

@ -1,3 +1,17 @@
/*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.inteligr8.maven.aps.modeling.util;
import java.util.Comparator;
@ -5,10 +19,18 @@ import java.util.Comparator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
/**
* A singleton class that provides JSON array sorting methods.
*
* @author brian@inteligr8.com
*/
public class ArrayNodeObjectSorter {
private static ArrayNodeObjectSorter INSTANCE = new ArrayNodeObjectSorter();
/**
* @return A singleton instance of this class.
*/
public static ArrayNodeObjectSorter getInstance() {
return INSTANCE;
}
@ -17,7 +39,32 @@ public class ArrayNodeObjectSorter {
protected ArrayNodeObjectSorter() {
}
/**
* This method sorts the JSON object elements of the JSON array by the
* value of the specified field in the JSON objects.
*
* If a JSON object has the specified field, its value is subject to the
* standard comparison. If it doesn't have a value, then it will sort to
* the end of the array.
*
* @param arrayNode A JSON array of JSON objects.
* @param jsonObjectFieldName A field in the JSON object elements.
* @return true if any elements were moved/sorted; false if unchanged.
* @see com.inteligr8.maven.aps.modeling.util.ObjectNodeComparator
*/
public boolean sort(ArrayNode arrayNode, String jsonObjectFieldName) {
return this.sort(arrayNode, new ObjectNodeComparator(jsonObjectFieldName));
}
/**
* This method sorts the JSON node elements of the JSON array using the
* specified comparator.
*
* @param arrayNode A JSON array of JSON objects.
* @param comparator A JSON node comparator.
* @return true if any elements were moved/sorted; false if unchanged.
*/
public boolean sort(ArrayNode arrayNode, Comparator<JsonNode> comparator) {
if (arrayNode.isEmpty())
return false;

View File

@ -1,12 +1,72 @@
/*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.inteligr8.maven.aps.modeling.util;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
public class ListMapSorter<K, V> {
/**
* A singleton class that provides list of maps sorting methods.
*
* @author brian@inteligr8.com
*/
public class ListMapSorter {
public boolean sort(List<Map<K, V>> listOfMaps, Comparator<Map<K, V>> comparator) {
private static ListMapSorter INSTANCE = new ListMapSorter();
/**
* @return A singleton instance of this class.
*/
public static ListMapSorter getInstance() {
return INSTANCE;
}
protected ListMapSorter() {
}
/**
* This method sorts the maps in the list by the value of the specified key
* in the maps.
*
* If a map has the specified key, its value is subject to the standard
* comparison. If it doesn't have a value, then it will sort to the end of
* the array.
*
* @param <V> The type of value in the maps.
* @param listOfMaps A list of maps.
* @param mapKey A key in the maps.
* @return true if any maps were moved/sorted; false if unchanged.
* @see com.inteligr8.maven.aps.modeling.util.MapComparator
*/
public <V> boolean sort(List<Map<String, V>> listOfMaps, String mapKey) {
return this.sort(listOfMaps, new MapComparator<String, V>(mapKey));
}
/**
* This method sorts the maps of the list using the specified comparator.
*
* @param <K> The type of key in the maps.
* @param <V> The type of value in the maps.
* @param listOfMaps A list of maps.
* @param comparator A map comparator.
* @return true if any maps were moved/sorted; false if unchanged.
*/
public <K, V> boolean sort(List<Map<K, V>> listOfMaps, Comparator<Map<K, V>> comparator) {
if (listOfMaps.isEmpty())
return false;