GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-diff-utils/README.md at master · java-diff-utils/java-diff-utils

Expand file tree

/

README.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
141 lines (101 loc) · 5.48 KB
/

README.md

Copy path

File metadata and controls

141 lines (101 loc) · 5.48 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

java-diff-utils

Status

Build Status

Build Status using GitHub Actions

Codacy Badge

Maven Central

Intro

Diff Utils library is an open source library for performing comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on.

The main reason to build this library was the lack of easy-to-use libraries with all the usual stuff you need while working with diff files. Originally it was inspired by JRCS library and its nice design of diff module.

This is originally a fork of java-diff-utils from Google Code Archive.

GPG Signature Validation

The GPG signing key in [KEYS] is used for this project's artifacts.

API

Javadocs of the actual release version: Javadocs java-diff-utils

Examples

Look here to find more helpful information and examples.

These two outputs are generated using java-diff-utils. The source code can also be found on the Examples page:

Producing a one liner including all difference information.

// Create a configured DiffRowGenerator
DiffRowGenerator generator = DiffRowGenerator.create()
                .showInlineDiffs(true)
                .mergeOriginalRevised(true)
                .inlineDiffByWord(true)
                .oldTag(f -> "~")      // Introduce markdown style for strikethrough
                .newTag(f -> "**")     // Introduce markdown style for bold
                .build();

// Compute the differences for two test texts
List<DiffRow> rows = generator.generateDiffRows(
                Arrays.asList("This is a test sentence."),
                Arrays.asList("This is a test for diffutils."));

System.out.println(rows.get(0).getOldLine());

This is a test sentencefor diffutils.

Producing a side-by-side view of computed differences.

DiffRowGenerator generator = DiffRowGenerator.create()
                .showInlineDiffs(true)
                .inlineDiffByWord(true)
                .oldTag(f -> "~")
                .newTag(f -> "**")
                .build();
List<DiffRow> rows = generator.generateDiffRows(
                Arrays.asList("This is a test sentence.", "This is the second line.", "And here is the finish."),
                Arrays.asList("This is a test for diffutils.", "This is the second line."));

System.out.println("|original|new|");
System.out.println("|--------|---|");
for (DiffRow row : rows) {
    System.out.println("|" + row.getOldLine() + "|" + row.getNewLine() + "|");
}
original new
This is a test sentence. This is a test for diffutils.
This is the second line. This is the second line.
And here is the finish.

Main Features

  • Computing the difference between two texts.
  • Capable of handling more than plain ASCII. Arrays or lists of any type that implement hashCode() and equals() correctly can be subject to differencing using this library
  • Patch and unpatch the text with the given patch
  • Parsing the unified diff format
  • Producing human-readable differences
  • Inline difference construction
  • Algorithms:
    • Myers standard algorithm
    • Myers with linear space improvement
    • HistogramDiff using the JGit library

Algorithms

  • Myers diff
  • HistogramDiff

But it can easily be replaced by any other which is better for handling your texts. I have a plan to add the implementation of some in the future.

Source Code conventions

Recently a checkstyle process was integrated into the build process. java-diff-utils follows the Sun Java format convention. There are no tabs allowed. Use spaces.

public static <T> Patch<T> diff(List<T> original, List<T> revised,
    BiPredicate<T, T> equalizer) throws DiffException {
    if (equalizer != null) {
        return DiffUtils.diff(original, revised,
        new MyersDiff<>(equalizer));
    }
    return DiffUtils.diff(original, revised, new MyersDiff<>());
}

This is a valid piece of source code:

  • blocks without braces are not allowed
  • after control statements (if, while, for) a whitespace is expected
  • the opening brace should be in the same line as the control statement

To Install

Just add the code below to your Maven dependencies:

<dependency>
    <groupId>io.github.java-diff-utils</groupId>
    <artifactId>java-diff-utils</artifactId>
    <version>4.15</version>
</dependency>

or using Gradle:

// https://mvnrepository.com/artifact/io.github.java-diff-utils/java-diff-utils
implementation "io.github.java-diff-utils:java-diff-utils:4.12"
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.