Example usage for org.apache.commons.lang StringUtils indexOfDifference

List of usage examples for org.apache.commons.lang StringUtils indexOfDifference

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils indexOfDifference.

Prototype

public static int indexOfDifference(String str1, String str2) 

Source Link

Document

Compares two Strings, and returns the index at which the Strings begin to differ.

Usage

From source file:StringUtilsTrial.java

public static void main(String[] args) {

    // Returns index where the Strings start to differ
    System.out.println("2) Index Of Difference between ABCXYZ and ABCPQR >>>"
            + StringUtils.indexOfDifference("ABCXYZ", "ABCPQR"));

}

From source file:com.atlassian.theplugin.util.CodeNavigationUtil.java

/**
 * Note: must be run from event dispatch thread or inside read-action only!
 *///ww  w.  j a v a  2s  .com
@Nullable
public static PsiFile guessMatchingFile(String pathname, PsiFile[] psifiles, VirtualFile baseDir) {
    // PL-822 fix
    // for making it work if pathname comes from different OS than the file itself
    // e.g. Bamboo works on Windows, send pathname and the user has a project opened in IDEA on Linux
    pathname = pathname.replace('\\', '/');

    PsiFile bestMatch = null;

    int difference = 0;

    if (psifiles != null) {

        for (PsiFile psiFile : psifiles) {
            // we use hard-coded '/' as separator in order to make string comparison platform independent
            String absolutePath = psiFile.getVirtualFile().getUrl();
            if (absolutePath == null) {
                continue;
            }

            int diff = StringUtils.indexOfDifference(StringUtils.reverse(pathname),
                    StringUtils.reverse(absolutePath));
            if (diff >= FilenameUtils.getName(absolutePath).length()
                    && (diff > difference || absolutePath.equals(pathname))) {
                difference = diff;
                bestMatch = psiFile;
                if (absolutePath.equals(pathname)) {
                    break;
                }
            }
        }
    }
    return bestMatch;
}

From source file:name.vysoky.xhtml.Corrector.java

private void printDifferences(String original, String revised) {
    int index = StringUtils.indexOfDifference(original, revised);
    if (index == -1)
        return;/*from w w  w.j a  v  a2 s.  c  om*/
    String difference = StringUtils.difference(original, revised);
    String before = original.substring(index);
    if (Replacer.isVerbose())
        System.out.println("'" + before + "'-> '" + difference + "'");
}

From source file:com.dubture.composer.test.ComposerCoreTestPlugin.java

/**
 * Compares expected result with the actual.
 * //from  w ww.  j  a  va2s. com
 * @param expected
 * @param actual
 * @return difference string or <code>null</code> in case expected result is
 *         equal to the actual.
 */
public static String compareContents(String expected, String actual) {
    actual = actual.replaceAll("[\r\n]+", "\n").trim();
    expected = expected.replaceAll("[\r\n]+", "\n").trim();

    int expectedDiff = StringUtils.indexOfDifference(actual, expected);
    if (expectedDiff >= 0) {
        int actualDiff = StringUtils.indexOfDifference(expected, actual);
        return getDiffError(expected, actual, expectedDiff, actualDiff);
    }
    return null;
}

From source file:com.dubture.composer.test.ComposerCoreTestPlugin.java

/**
 * Compares expected result with the actual ingoring whitespace characters
 * //www . j  a va  2 s  .c  o  m
 * @param expected
 * @param actual
 * @return difference string or <code>null</code> in case expected result is
 *         equal to the actual.
 */
public static String compareContentsIgnoreWhitespace(String expected, String actual) {
    String tmpExpected = expected;
    String tmpActual = actual;
    String diff = StringUtils.difference(tmpExpected, tmpActual);
    while (diff.length() > 0) {
        String diff2 = StringUtils.difference(tmpActual, tmpExpected);

        if (!Character.isWhitespace(diff.charAt(0)) && !Character.isWhitespace(diff2.charAt(0))) {
            int expectedDiff = StringUtils.indexOfDifference(tmpActual, tmpExpected)
                    + (expected.length() - tmpExpected.length());
            int actualDiff = StringUtils.indexOfDifference(tmpExpected, tmpActual)
                    + (actual.length() - tmpActual.length());
            return getDiffError(expected, actual, expectedDiff, actualDiff);
        }

        tmpActual = diff.trim();
        tmpExpected = diff2.trim();

        diff = StringUtils.difference(tmpExpected, tmpActual);
    }
    return null;
}

From source file:com.netflix.priam.backup.AbstractBackupPath.java

/**
 * Given a date range, find a common string prefix Eg: 20120212, 20120213 =>
 * 2012021/*from  w w w  .  ja v a2  s.  c o  m*/
 */
public String match(Date start, Date end) {
    String sString = formatDate(start);
    String eString = formatDate(end);
    int diff = StringUtils.indexOfDifference(sString, eString);
    if (diff < 0)
        return sString;
    return sString.substring(0, diff);
}

From source file:com.opengamma.util.test.AbstractDbUpgradeTest.java

@Test(groups = TestGroup.UNIT_DB)
public void testDatabaseUpgrade() {
    for (Triple<String, String, String> comparison : _comparisons) {
        /*/*from  w w  w  .  j a va2  s  . c  om*/
         * System.out.println(comparison.getFirst() + " expected:");
         * System.out.println(comparison.getSecond());
         * System.out.println(comparison.getFirst() + " found:");
         * System.out.println(comparison.getThird());
         */
        int diff = StringUtils.indexOfDifference(comparison.getSecond(), comparison.getThird());
        if (diff >= 0) {
            System.err.println("Difference at " + diff);
            System.err.println("Upgraded --->..."
                    + StringUtils.substring(comparison.getSecond(), diff - 200, diff) + "<-!!!->"
                    + StringUtils.substring(comparison.getSecond(), diff, diff + 200) + "...");
            System.err.println(" Created --->..."
                    + StringUtils.substring(comparison.getThird(), diff - 200, diff) + "<-!!!->"
                    + StringUtils.substring(comparison.getThird(), diff, diff + 200) + "...");
        }
        assertEquals(_databaseType + ": " + comparison.getFirst(), comparison.getSecond(),
                comparison.getThird());
    }
}

From source file:com.lm.lic.manager.controller.UpdateDomainLicActiveUserCountController.java

/**
 * @param incomingDomain/*from ww  w .  ja  v a2s.  co m*/
 * @param currDomain
 * @return
 */
private boolean isIncomingDomainCurrentDomain(String incomingDomain, String currDomain) {

    // Just in case a verification occurs before the current domain is set.
    if (StringUtils.isEmpty(currDomain))
        return false;

    int indexInCurrentDomain = StringUtils.indexOfDifference(incomingDomain, currDomain);
    boolean bothIncomingAndCurrentEqual = indexInCurrentDomain == -1;

    return bothIncomingAndCurrentEqual;
}

From source file:org.apache.cocoon.util.NetUtils.java

/**
 * Relativize an absolute resource on a given absolute path.
 *
 * @param path The absolute path//from www. ja  va 2 s  . c  o  m
 * @param absoluteResource The absolute resource
 * @return the resource relative to the given path
 */
public static String relativize(String path, String absoluteResource) {
    if (StringUtils.isEmpty(path)) {
        return absoluteResource;
    }

    if (path.charAt(path.length() - 1) != '/') {
        path += "/";
    }

    if (absoluteResource.startsWith(path)) {
        // resource is direct descentant
        return absoluteResource.substring(path.length());
    } else {
        // resource is not direct descendant
        int index = StringUtils.indexOfDifference(path, absoluteResource);
        if (index > 0 && path.charAt(index - 1) != '/') {
            index = path.substring(0, index).lastIndexOf('/');
            index++;
        }
        String pathDiff = path.substring(index);
        String resource = absoluteResource.substring(index);
        int levels = StringUtils.countMatches(pathDiff, "/");
        StringBuffer b = new StringBuffer(levels * 3 + resource.length());
        for (int i = 0; i < levels; i++) {
            b.append("../");
        }
        b.append(resource);
        return b.toString();
    }
}

From source file:org.archive.util.PrefixFinder.java

/**
 * Extracts prefixes of a given string from a SortedSet.  If an element
 * of the given set is a prefix of the given string, then that element
 * is added to the result list./* w ww.  jav  a2 s. c  o  m*/
 * 
 * <p>Put another way, for every element in the result list, the following 
 * expression will be true: <tt>string.startsWith(element.getKey())</tt>.
 * 
 * @param set     the sorted set containing potential prefixes
 * @param input   the string whose prefixes to find 
 * @return   the list of prefixes 
 */
public static List<String> find(SortedSet<String> set, String input) {
    LinkedList<String> result = new LinkedList<String>();
    set = headSetInclusive(set, input);
    for (String last = last(set); last != null; last = last(set)) {
        if (input.startsWith(last)) {
            result.push(last);
            set = set.headSet(last);
        } else {
            // Find the longest common prefix.
            int p = StringUtils.indexOfDifference(input, last);
            if (p <= 0) {
                return result;
            }
            last = input.substring(0, p);
            set = headSetInclusive(set, last);
        }
    }
    return result;
}