/* * [PossibleDeDupOrder.java] * * Summary: Comparator to sort Funduc Search/Replace *.srs scripts for detecting possible dups. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-11-13 initial version */ package com.mindprod.sortsrs; import com.mindprod.common18.ST; import java.util.Comparator; /** * Comparator to sort Funduc Search/Replace *.srs scripts for detecting possible dups. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-11-13 initial version * @since 2011-11-13 */ class PossibleDeDupOrder implements Comparator { /** * Sort SRS Script items, precise case-sensitive order, all fields considered. * Defines an alternate sort order for String with JDK 1.5+ generics. * Compare two String Objects. * Compares whole object case sensitively. * Informally, returns (a-b), or +ve if a comes after b. * The Java source code for this Comparator was generated by the * Canadian Mind Products ComparatorCutter Applet at http://mindprod.com/applet/comparatorcutter.html * * @param a first String to compare * @param b second String to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( Item a, Item b ) { assert a != null && b != null : "null compare parms"; int diff = a.section - b.section; if ( diff != 0 ) { return diff; } if ( a instanceof SRItem ) { // b must be an SRItem too. SRItem as = ( SRItem ) a; SRItem bs = ( SRItem ) b; return ST.nullSafeStringCompareIgnoreCase( as.searchUntrimmed, bs.searchUntrimmed ); // for dup purpose, replace, options, comments are irrelevant // must not include them or compare for equal would not work. } if ( a instanceof PathItem ) { // two pathItems PathItem ap = ( PathItem ) a; PathItem bp = ( PathItem ) b; return ST.nullSafeStringCompareIgnoreCase( ap.path, bp.path ); } if ( a instanceof EndItem ) { return 0; // all as identical as hydrogen atoms. } throw new IllegalArgumentException( "Program bug [11]: Unexpected Object type compared" ); } }