/* * [InsensitiveOrder.java] * * Summary: Comparator to sort Funduc Search/Replace *.srs scripts. * * 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. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-11-13 initial version * @since 2011-11-13 */ class InsensitiveOrder implements Comparator { /** * Sort SRS Script items, user's preferred order. * Defines an alternate sort order for String with JDK 1.5+ generics. * Compare two String Objects. * Compares whole object case insensitively. * 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; } // we now known a and b have the same section hence same typue if ( a instanceof SRItem ) { // b can't be null SRItem as = ( SRItem ) a; SRItem bs = ( SRItem ) b; diff = ST.nullSafeStringCompareIgnoreCase( as.searchUntrimmed, bs.searchUntrimmed ); if ( diff != 0 ) { return diff; } diff = ST.nullSafeStringCompareIgnoreCase( as.replaceUntrimmed, bs.replaceUntrimmed ); if ( diff != 0 ) { return diff; } // tie breaker, sort including command switches return ST.nullSafeStringCompareIgnoreCase( as.options, bs.options ); // comments are irrelevant. } 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 and hydrogen atoms. } throw new IllegalArgumentException( "Program bug [10]: Unexpected Object type compared" ); } }