GitDiffTest2.java:


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class GitDiffTest2 {

    // private static final String folder = "\\xxx\\";
    private static final String folder = "\\xxx\\";

    private static final List<String> lines_v1 = loadTxtFile2List(folder + "DemoClass1.java" );
    private static final List<String> lines_v2 = loadTxtFile2List(folder + "DemoClass2.java");

    // 1 2 34567 89
    //      ||
    //      ||
    //      ||
    //    \\||//
    //     \\//
    //      \/
    // 1   34567 ab c

    public static void main(String[] args) {
        int size_v1 = lines_v1.size();
        int size_v2 = lines_v2.size();
        int[][] dp = new int[size_v1 + 1][size_v2 + 1];
        for (int i = 1; i <= size_v1; i++) {
            for (int j = 1; j <= size_v2; j++) {
                if (lines_v1.get(i - 1).equals(lines_v2.get(j - 1))) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        List<String> result = new ArrayList<>();
        int i = lines_v1.size();
        int j = lines_v2.size();
        //            |
        // 1 2 34567 89
        // 1   34567 ab c
        //              |
        while (i > 0 && j > 0) {
            if (lines_v1.get(i - 1).equals(lines_v2.get(j - 1))) {
                result.add( "  " + lines_v1.get( i - 1 ) );
                i--;
                j--;
            } else if (dp[i - 1][j] < dp[i][j - 1]) {
                result.add("+ " + lines_v2.get(j - 1));
                j--;
            } else {
                result.add("- " + lines_v1.get(i - 1));
                i--;
            }
        }
        while (j > 0) {
            result.add("+ " + lines_v2.get(j - 1));
            j--;
        }
        while (i > 0) {
            result.add("- " + lines_v1.get(i - 1));
            i--;
        }
        for (int k = result.size() - 1; k >= 0; k--) {
            System.out.println(result.get(k));
        }
    }

    private static List<String> loadTxtFile2List(String filePath) {
        BufferedReader reader = null;
        List<String> lines = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(filePath));
            String line = reader.readLine();
            while (line != null) {
                lines.add( line );
                line = reader.readLine();
            }
            return lines;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
DemoClass1.java:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.similarity.LevenshteinDistance;

@Slf4j
public class DemoClass1 {

    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE = LevenshteinDistance.getDefaultInstance();

    public static String null2emptyWithTrim( String str ){
        if( str == null ){
            str = "";
        }
        str = str.trim();
        return str;
    }

    public static String requiredStringParamCheck(String param, String paramRemark) {
        param = null2emptyWithTrim( param );
        if( param.length() == 0 ){
            String msg = "操作失败,请求参数 \"" + paramRemark + "\" 为空";
            log.error( msg );
            throw new BusinessLogicException( msg );
        }
        return param;
    }

    public static double calculateSimilarity( String str1,String str2 ){
        int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
        double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
        System.out.println("相似度:" + similarity);
        return similarity;
    }
}
DemoClass2.java:

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.similarity.LevenshteinDistance;

@Slf4j
public class DemoClass2 {

    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE = LevenshteinDistance.getDefaultInstance();
    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE1 = LevenshteinDistance.getDefaultInstance();
    private static final LevenshteinDistance LEVENSHTEIN_DISTANCE2 = LevenshteinDistance.getDefaultInstance();

    public static String null2emptyWithTrim( String str ){
        // if( str == null ){
        //     str = "";
        // }
        // str = str.trim();
        return str;
    }

    public static String requiredStringParamCheck(String param, String paramRemark) {
        return null;
    }

    public static double calculateSimilarity( String str1,String str2 ){
        try {
            int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
            double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
            return similarity;
        }catch ( Exception e ){
            e.printStackTrace();
            return 0d;
        }
    }
}

测试输出:


  
  import com.goldwind.ipark.common.exception.BusinessLogicException;
  import lombok.extern.slf4j.Slf4j;
  import org.apache.commons.text.similarity.LevenshteinDistance;
  
  @Slf4j
+ public class DemoClass2 {
- public class DemoClass1 {
  
      private static final LevenshteinDistance LEVENSHTEIN_DISTANCE = LevenshteinDistance.getDefaultInstance();
+     private static final LevenshteinDistance LEVENSHTEIN_DISTANCE1 = LevenshteinDistance.getDefaultInstance();
+     private static final LevenshteinDistance LEVENSHTEIN_DISTANCE2 = LevenshteinDistance.getDefaultInstance();
  
      public static String null2emptyWithTrim( String str ){
+         // if( str == null ){
+         //     str = "";
+         // }
+         // str = str.trim();
-         if( str == null ){
-             str = "";
-         }
-         str = str.trim();
          return str;
      }
  
      public static String requiredStringParamCheck(String param, String paramRemark) {
+         return null;
-         param = null2emptyWithTrim( param );
-         if( param.length() == 0 ){
-             String msg = "操作失败,请求参数 \"" + paramRemark + "\" 为空";
-             log.error( msg );
-             throw new BusinessLogicException( msg );
-         }
-         return param;
      }
  
      public static double calculateSimilarity( String str1,String str2 ){
+         try {
+             int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
+             double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
+             return similarity;
+         }catch ( Exception e ){
+             e.printStackTrace();
+             return 0d;
+         }
-         int distance = LEVENSHTEIN_DISTANCE.apply(str1, str2);
-         double similarity = 1 - (double) distance / Math.max(str1.length(), str2.length());
-         System.out.println("相似度:" + similarity);
-         return similarity;
      }
  }
11-29 18:45