DIFF入口,用于比较两个Json字符串 @Author JingWei @create 2022/2/25
| 26 | * @create 2022/2/25 |
| 27 | */ |
| 28 | public class Diff { |
| 29 | /** |
| 30 | * 算法模型。根据传入的算法模型使用该算法模型进行diff比较。 |
| 31 | */ |
| 32 | private AlgorithmEnum algorithmEnum; |
| 33 | /** |
| 34 | * 特殊路径集合。当前路径符合特殊路径且特殊路径下比较结果相同,会在返回结果中做额外标识标识。 |
| 35 | */ |
| 36 | private List<String> specialPath; |
| 37 | /** |
| 38 | * 噪音字段集合。如果当前路径符合噪音字段路径,则不会比较。 |
| 39 | */ |
| 40 | private List<String> noisePahList; |
| 41 | /** |
| 42 | * 以下为5种可以自定义的比较器,条件是algorithmEnum为空(如果algorithmEnum有值,直接使用algorithmEnum对应算法模型,比较器不生效) |
| 43 | */ |
| 44 | private ObjectComparator objectComparator; |
| 45 | private ArrayComparator arrayComparator; |
| 46 | private PrimitiveComparator primitiveComparator; |
| 47 | private NullComparator nullComparator; |
| 48 | private OtherComparator otherComparator; |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * @param a 要比较的第一个JsonElement |
| 53 | * @param b 要比较的第二个JsonElement |
| 54 | * @return 用来展示的diff结果 |
| 55 | */ |
| 56 | public List<Result> diffElement(JsonElement a, JsonElement b) { |
| 57 | DiffContext diffContext; |
| 58 | //用噪音路径和自定义路径构造路径模型 |
| 59 | PathModule pathModule = new PathModule(noisePahList, specialPath); |
| 60 | //如果有算法模型直接使用算法模型 |
| 61 | if(algorithmEnum != null){ |
| 62 | diffContext = algorithmEnum.getAlgorithmModule().diffElement(a, b, pathModule); |
| 63 | }//如果也没有比较器,直接用默认算法模型 |
| 64 | else if(objectComparator == null && arrayComparator == null && primitiveComparator == null && nullComparator == null && otherComparator == null){ |
| 65 | diffContext = AlgorithmEnum.DEFAULT.getAlgorithmModule().diffElement(a, b, pathModule); |
| 66 | }//如果有比较器,为空的比较器用默认替换,然后构造算法模型, |
| 67 | else { |
| 68 | constrcutDefaultComparator(); |
| 69 | diffContext = new AlgorithmModule(objectComparator, arrayComparator, primitiveComparator, nullComparator, otherComparator).diffElement(a, b, pathModule); |
| 70 | } |
| 71 | return ResultConvertUtil.constructResult(diffContext); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param strA 要比较的第一个字符串 |
| 76 | * @param strB 要比较的第二个字符串 |
| 77 | * @return 用来展示的diff结果 |
| 78 | */ |
| 79 | public List<Result> diff(String strA, String strB) { |
| 80 | return diffElement(new JsonParser().parse(strA), new JsonParser().parse(strB)); |
| 81 | } |
| 82 | |
| 83 | public Diff() { |
| 84 | } |
| 85 |
nothing calls this directly
no outgoing calls
no test coverage detected