I made a Diff tool for Java files

Overview

It's a continuation of the one I wrote earlier (or yesterday).

Click here for the relevant article!

I wrote a Java source analysis tool in Java ... https://qiita.com/banana5388/items/cd8a549d1885aa6dd039

In the comment of the previous article, you pointed out that "let's start with something simple", so I started by simply getting the number of difference steps between the two source codes.

(Don't say redevelopment of the wheels !!!!!! It was fun to make something !!!) </ font>

So the input and output of what I will create this time is like this.

INPUT :Absolute path of comparison source and comparison destination
OUTPUT:Number of steps with different code

specification

What I was thinking about for the time being is something like this.

  1. Analyze the obtained code for each of the comparison source and comparison destination character by character.
  2. If different characters appear between the comparison source and the comparison destination, stop the comparison source at the current character position and move the comparison destination character to the next character analysis.
  3. If the same character as the comparison source appears at the comparison destination, the character analysis of the comparison source is restarted.
  4. If a line feed character appears at the comparison destination while the comparison source character analysis is stopped, add 1 difference step.
  5. Continue the process of [2. --4] until the characters of the comparison source or comparison destination are completed.
  6. Output how many lines are different as a result of analysis

For the time being, I implemented this as it is.

The implementation of the main part looks like this.

JavaCodeDifference.java



   /**
     *Compares two codes and returns the number of matching lines
     *
     * @param origCode Comparison source code (stored in a list for each line)
     * @param destCode Comparison code (stored in a list for each line)
     * @return Number of matching lines for comparison
     */
    public static int countCodeDiff(List<String> origCode, List<String> destCode) {

        int cnt = 0;

        //Generating iterators for strings

        //The method "getNotIncludeIgnoreCode" is based on the line-by-line code information stored in the list.
        //Erase half-width spaces, full-width spaces, tab characters, and line feed characters, and add a line feed character at the end.
        //It is a method that finally returns as one character string (String type).
        StringCharacterIterator origCodeIterator =  new StringCharacterIterator(getNotIncludeIgnoreCode(origCode));
        StringCharacterIterator destCodeIterator =  new StringCharacterIterator(getNotIncludeIgnoreCode(destCode));

        //Execute until the character of either the comparison source or the comparison destination ends
        while (origCodeIterator.current() != StringCharacterIterator.DONE &&
                destCodeIterator.current() != StringCharacterIterator.DONE) {

            //Increase the count when different code characters are included in the comparison source and comparison destination, and the comparison destination has a line break first.
            if (origCodeIterator.current() == destCodeIterator.current()) {
                destCodeIterator.next();
                origCodeIterator.next();
            } else {
                if (destCodeIterator.current() == '\n') {
                    cnt++;
                }
                destCodeIterator.next();
            }
        }

        return cnt;
    }

Um, isn't it a good feeling? ??

test

First test

I prepared the text for testing.

test1.java


aaaa
iiii
uuuu

test2.java


aaaa
iiii
uuuu

It's not a java file at all, but for the time being, it's for testing, so is this okay? Well, the output is. .. ..

> Number of different lines: 0

Yeah, it looks good.

Second test

Another test

test1.java


aaaa
iiii
uuuu

test2.java


aaa
iii
uuu

Well, the output is. .. ..

> Difference Code Line Count:3

This looks good too.

Impressions

For the time being, I am satisfied because it is moving according to the specifications.

Rather, the specifications are rather loose ... I would like to reimplement it so that it can be used.

The source is open to the public

Yes. https://github.com/satodaiki/DiffTool

Change log

2018/12/2 First edition 2018/12/3 There was some error in the source, so I fixed it.

Recommended Posts

I made a Diff tool for Java files
I made a new Java deployment tool
I made a check tool for the release module
I made a shopify app @java
I made a package.xml generation tool.
I made a plugin for IntelliJ IDEA
I made a method to ask for Premium Friday (Java 8 version)
I made a primality test program in Java
[Java] What should I use for writing files?
I made a rock-paper-scissors game in Java (CLI)
I made a Docker image of SDAPS for Japanese
I made a simple calculation problem game in Java
I made a method to ask for Premium Friday
Try Easy Ramdom, a PropertyBase Testing tool for java
I made a library for displaying tutorials on Android.
I made a Wrapper that calls KNP from Java
I made roulette in Java.
I made a chat app.
[Paiza] I made my own utility for answering questions [Java]
[Beginner] I made a program to sell cakes in Java
I investigated Randoop, a JUnit test class generator for Java
I made a Dockerfile to start Glassfish 5 using Oracle Java
Ruby: I made a FizzBuzz program!
I created a PDF in Java.
I made a GUI with Swing
I made a simple recommendation function.
I made an annotation in Java.
I made a matching app (Android app)
[Android] I made a pedometer app.
I want to recursively search for files under a specific directory
I made a reply function for the Rails Tutorial extension (Part 1)
I made a source that automatically generates JPA Entity class files
I made a question that can be used for a technical interview
I made a reply function for the Rails Tutorial extension (Part 5):
I made a tool to output the difference of CSV file
A story that I finally understood Java for statement as a non-engineer
[Ruby] I made a simple Ping client
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
[java tool] A tool that deletes files under the specified path by extension
Java for All! I read everyone's Java #minjava
I tried Cassandra's Object Mapper for Java
I was in trouble at work, so I made a plugin for IntelliJ
Made a one-liner method for Premium Friday
I made a risky die with Ruby
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a rock-paper-scissors app with android
I made a class that can use JUMAN and KNP from Java
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
I made a bulletin board using Docker 1
I made a reply function for Rails Tutorial extension (Part 2): Change model
I made a THETA API client that can be used for plug-in development
Learn Java with Progate → I will explain because I made a basic game myself
I made a simple graph library for smartphone apps [MP Android Chart Kai]
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
A story about Java 11 support for Web services
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
java I tried to break a simple block
[Java] Processes all files under a certain directory
I studied for 3 weeks and passed Java Bronze