Interface Comparison

All Superinterfaces:
HistoryEntry
All Known Subinterfaces:
FileComparison, FolderComparison, ImageComparison, TextComparison

public interface Comparison extends HistoryEntry
The base type of all comparisons - folder, text and image. Defines the types and methods common to all of them.

Comparisons are created with Comparisons.create(Comparison.Type, String[], ComparisonListener). Each comparison opens in its own DeltaWalker window or view; the actual comparison runs when compare() is called. The object returned by create() is a FolderComparison, a TextComparison or an ImageComparison, depending on the requested Comparison.Type, so the methods of those types can be called on it directly.


 const Comparison = Java.type('com.deltawalker.script.Comparison');

 const IN = App.getProperty('user.home') + '/test/samples';
 const comp = App.getComparisons().create(Comparison.Type.FOLDER,
         [IN + '/f0', IN + '/f1'], null);

 await comp.compare();
 if (comp.hasDifferences()) {
     console.log('Nodes: ' + comp.getDeltaCount());
 }
 comp.close();
 

A comparison runs one long-running operation (compare, merge, sync, report or print) at a time. Starting another one while an operation is still pending on the same comparison fails with a rejected operation.

Since:
2.0
  • Method Details

    • addListener

      void addListener(ComparisonListener listener)
      Adds a ComparisonListener that is notified of the ComparisonEvents of this comparison.

      Listeners are a legacy mechanism; new scripts should await the operation they are interested in instead.

      Adding the same, or an equal, listener more than once has no effect. To remove a listener use removeListener(ComparisonListener).

      Parameters:
      listener - the listener to add; cannot be null
      Throws:
      NullPointerException - if listener is null
      Since:
      2.0
      See Also:
    • close

      void close()
      Closes the window or view showing this comparison, without saving changes. Comparisons hold system resources - the bigger they are, the more - so it is good practice to close comparisons that are no longer needed.

      The window is closed asynchronously. Once it is closed, isClosed() returns true, any pending operation is cancelled, and the comparison should no longer be used.

      Since:
      2.0
    • compare

      Object compare() throws IOException
      Compares the resources this comparison was created with, and returns a promise that settles when the comparison completes. The work starts immediately; await the result before reading comparison results.
      
       const op = comp.compare();
       console.log('Comparison running...');
       await op;
       console.log('Differences found: ' + comp.hasDifferences());
       

      The returned promise is a native JavaScript Promise: it can be awaited more than once and used in Promise.all(). It is rejected if another operation is already pending on this comparison, and cancelled (see ScriptCancellationError) if the comparison is closed before it completes.

      Returns:
      a promise that resolves when the comparison completes
      Throws:
      IOException - declared for compatibility; failures are reported by rejecting the returned promise
      Since:
      2.0
    • hasDifferences

      boolean hasDifferences()
      Answers whether the last compare() found any differences between the resources of this comparison.

      To analyze the differences use TextComparison.getAllDeltas() or FolderComparison.getAllDeltas(). For image comparisons this method currently always returns false.

      Returns:
      true if the resources are different, false if they are the same
      Since:
      2.0
      See Also:
    • print

      Object print()
      Prints this comparison to a printer or a PDF file using the application's PrinterSettings and PageSettings, and returns a promise that settles when printing completes.
      
       const PageOrientation = Java.type('com.deltawalker.script.PageOrientation');
      
       const settings = App.getSettings();
       settings.getPage().setOrientation(PageOrientation.LANDSCAPE);
       // Printing to a PDF file is available on macOS only
       settings.getPrinter().setPrintToFile(true);
       settings.getPrinter().setFilePath(App.getProperty('user.home') + '/comparison.pdf');
       await comp.print();
       

      Like the promise returned by compare(), the returned promise is a native JavaScript Promise that can be awaited more than once.

      Returns:
      a promise that resolves when printing completes
      Since:
      2.0
    • isClosed

      boolean isClosed()
      Answers whether this comparison has been closed, by the user or by close(), and a Comparison.State.CLOSED event delivered.

      A closed comparison cannot run further operations such as compare, merge, sync, report or print. Once this method returns true it never returns false again.

      Returns:
      true if this comparison has been closed, false otherwise
      Since:
      3.0
    • removeListener

      void removeListener(ComparisonListener listener)
      Removes a listener previously added with addListener(ComparisonListener). Has no effect if the listener is not registered.
      Parameters:
      listener - the listener to remove; cannot be null
      Throws:
      NullPointerException - if listener is null
      Since:
      2.0