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

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

Introduction

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

Prototype

public static String difference(String str1, String str2) 

Source Link

Document

Compares two Strings, and returns the portion where they differ.

Usage

From source file:StringUtilsExampleV1.java

public static void main(String args[]) {

    System.err.println(StringUtils.difference("govern", "government"));
}

From source file:com.rslakra.java.string.TestApacheStringUtils.java

public static void main(String args[]) {

    System.err.println(StringUtils.abbreviate("Take time off working", 0, 10));
    System.err.println(StringUtils.capitalize("vanderLust"));
    System.err.println(StringUtils.center("MTV", 7, '='));
    System.err.println(StringUtils.chomp("temperature", "ure"));
    System.err.println(StringUtils.chop("Dane"));
    System.err.println(StringUtils.contains("Dorothy", "oro"));
    System.err.println(StringUtils.containsNone("r u m t", new char[] { 'r', 'o' }));
    System.err.println(StringUtils.containsOnly("r u m t", new char[] { 'r', 'o' }));
    System.err.println(StringUtils.countMatches("arthur", "r"));
    System.err.println(StringUtils.deleteWhitespace("f f f f"));
    System.err.println(StringUtils.difference("govern", "government"));
    System.err.println(StringUtils.getLevenshteinDistance("govern", "government"));

}

From source file:dollar.DollarOperatorsRegressionTest.java

public void diff(@NotNull String desc, @NotNull String lhs, @NotNull String rhs) {
    final String difference = StringUtils.difference(lhs, rhs);
    if (!difference.isEmpty()) {
        fail("Difference for " + desc + " is " + difference + "\nCompare previous: " + lhs + "\nWith current "
                + rhs);/*from   ww w. j  a  va  2s . c  om*/
    }
}

From source file:br.com.renatoccosta.regexrenamer.RenamedFile.java

public String getRelativeFileNameBefore() {
    return StringUtils.difference(root, getFileNameBefore());
}

From source file:br.com.renatoccosta.regexrenamer.RenamedFile.java

public String getRelativeFileNameAfter() {
    return StringUtils.difference(root, getFileNameAfter());
}

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  ww  w  .jav a 2s. 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.atlassian.theplugin.idea.crucible.CrucibleHelper.java

public static Collection<UploadItem> getUploadItemsFromChanges(final Project project,
        final Collection<Change> changes) {
    Collection<UploadItem> uploadItems = new ArrayList<UploadItem>();
    for (Change change : changes) {
        try {//  w ww.  java 2  s  . co  m
            ContentRevision contentRevision;
            if (change.getBeforeRevision() != null) {
                contentRevision = change.getBeforeRevision();
            } else {
                //for added but not committed files after revision is available only
                contentRevision = change.getAfterRevision();
            }
            // PL-1619
            if (contentRevision == null) {
                continue;
            }

            String fileUrl = null;
            VirtualFile file = contentRevision.getFile().getVirtualFile();
            if (file == null) {
                file = contentRevision.getFile().getVirtualFileParent();
                if (file == null) {
                    continue;
                }
                fileUrl = VcsIdeaHelper.getRepositoryUrlForFile(project, file);
                if (fileUrl != null) {
                    fileUrl = fileUrl + "/" + contentRevision.getFile().getName();
                }
            } else {
                fileUrl = VcsIdeaHelper.getRepositoryUrlForFile(project, file);
            }

            try {
                URL url = new URL(fileUrl);
                fileUrl = url.getPath();
            } catch (MalformedURLException e) {
                String rootUrl = VcsIdeaHelper.getRepositoryRootUrlForFile(project,
                        contentRevision.getFile().getVirtualFile());
                fileUrl = StringUtils.difference(rootUrl, fileUrl);
            }

            ContentRevision revOld = change.getBeforeRevision();
            ContentRevision revNew = change.getAfterRevision();

            byte[] byteOld = revOld != null && revOld.getContent() != null ? revOld.getContent().getBytes()
                    : new byte[0];
            byte[] byteNew = revNew != null && revNew.getContent() != null ? revNew.getContent().getBytes()
                    : new byte[0];

            // @todo implement it handling of binary files
            uploadItems.add(new UploadItem(fileUrl, byteOld, byteNew));

        } catch (VcsException e) {
            throw new RuntimeException(e);
        }
    }
    return uploadItems;
}

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

/**
 * Compares expected result with the actual ingoring whitespace characters
 * /*from   w  w  w .j  av  a2 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:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.Period.java

/**
 * This function deals with a period of time with format the xxth day/month, etc
 * @param start the anchor time//from w ww  .java2s . c  o m
 * @param temporalPhrase
  * @return
  */
public static TimexChunk Periodrule(DateTime start, TemporalPhrase temporalPhrase) {

    int year;
    DateTime finish;
    String temp1;
    String temp2;
    Interval interval;
    interval = new Interval(start, start);
    String phrase = temporalPhrase.getPhrase();
    phrase = phrase.toLowerCase();
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");

    int modiword = 0;// 0 :no modified words 1:early,ealier 2:late,later
    // Handle some special cases
    TimexChunk tc = new TimexChunk();
    tc.addAttribute(TimexNames.type, TimexNames.DATE);
    if (phrase.contains("now") || phrase.contains("currently") || phrase.contains("current")
            || phrase.contains("today")) {
        DateTime virtualStart = interval.getStart();
        virtualStart = new DateTime(virtualStart.getYear(), virtualStart.getMonthOfYear(),
                virtualStart.getDayOfMonth(), virtualStart.getHourOfDay(), virtualStart.getMinuteOfHour(),
                virtualStart.getSecondOfMinute(), virtualStart.getMillisOfSecond() + 1);
        tc.addAttribute(TimexNames.value, TimexNames.PRESENT_REF);
        return tc;
    }
    if (phrase.contains("early") || phrase.contains("earlier")) {
        modiword = 1;
    }
    if (phrase.contains("late") || phrase.contains("later")) {
        modiword = 2;
    }

    String units = "";

    for (String unitStr : dateUnitSet) {
        units = units + unitStr + "|";
    }
    units += "s$";

    String patternStr = "(?:the)?\\s*(\\d{1,4})(?:th|nd|st|rd)\\s*(" + units + ")\\s*";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(phrase);
    boolean matchFound = matcher.find();
    if (matchFound) {
        temp1 = matcher.group(1);
        temp2 = matcher.group(2);

        String residual = StringUtils.difference(matcher.group(0), phrase);
        String anchorStr = "";
        if (residual.length() > 0) {
            TemporalPhrase anchorPhrase = new TemporalPhrase(residual, temporalPhrase.getTense());
            TimexChunk anchorTimex = TimexNormalizer.normalize(anchorPhrase);
            if (anchorTimex != null) {
                anchorStr = anchorTimex.getAttribute(TimexNames.value);
            }
        }

        if (temp2.equals("century")) {
            year = (Integer.parseInt(temp1) - 1) * 100;
            start = new DateTime(year, 1, 1, 0, 0, 0, 0);
            finish = new DateTime(year + 99, 12, 31, 23, 59, 59, 59);
            tc.addAttribute(TimexNames.value, String.valueOf(finish.getCenturyOfEra()));
            return tc;
        }

        else if (temp2.equals("decade")) {
            // e.g.: 3rd decade (of this century)
            // first we get this century is 20, then the 3rd decade is 203
            int anchorCentury = start.getCenturyOfEra();
            String val = String.valueOf(anchorCentury * 10 + temp1);
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("year")) {
            int anchorCentury = start.getCenturyOfEra();
            String val = String.valueOf(anchorCentury * 100 + temp1);
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("quarter")) {
            int anchorYear = start.getYear();
            String val = String.valueOf(anchorYear) + "-Q" + temp1;
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("month")) {
            int anchorYear = start.getYear();
            String monthStr = Integer.parseInt(temp1) < 10 ? "0" + temp1 : temp1;
            String val = String.valueOf(anchorYear) + "-" + monthStr;
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("day")) {
            String val = "";
            if (anchorStr.length() > 0) {
                List<String> normTimexList = Period.normTimexToList(anchorStr);
                String anchorYear = normTimexList.get(0);
                String anchorDate;
                String anchorMonth;
                if (normTimexList.size() == 1 || Integer.parseInt(temp1) > 31) {
                    anchorMonth = "01";
                } else {
                    anchorMonth = normTimexList.get(1);
                }
                DateTime normDateTime = new DateTime(Integer.parseInt(anchorYear),
                        Integer.parseInt(anchorMonth), 1, 0, 0);
                normDateTime = normDateTime.minusDays(-1 * Integer.parseInt(temp1));
                anchorYear = String.valueOf(normDateTime.getYear());
                anchorMonth = String.valueOf(normDateTime.getMonthOfYear());
                anchorDate = String.valueOf(normDateTime.getDayOfMonth());
                anchorMonth = anchorMonth.length() == 1 ? "0" + anchorMonth : anchorMonth;
                anchorDate = anchorDate.length() == 1 ? "0" + anchorDate : anchorDate;
                val = anchorYear + "-" + anchorMonth + "-" + anchorDate;

            } else {
                int month = Integer.parseInt(temp1) > 31 ? 1 : start.getMonthOfYear();
                DateTime normDateTime = new DateTime(start.getYear(), month, 1, 0, 0);
                normDateTime = normDateTime.minusDays(-1 * Integer.parseInt(temp1));
                String anchorYear = String.valueOf(normDateTime.getYear());
                String anchorMonth = String.valueOf(normDateTime.getMonthOfYear());
                String anchorDate = String.valueOf(normDateTime.getDayOfMonth());
                anchorMonth = anchorMonth.length() == 1 ? "0" + anchorMonth : anchorMonth;
                anchorDate = anchorDate.length() == 1 ? "0" + anchorDate : anchorDate;
                val = String.valueOf(anchorYear) + "-" + anchorMonth + "-" + anchorDate;
            }
            tc.addAttribute(TimexNames.value, String.valueOf(val));
            return tc;
        }

        else if (temp2.equals("s")) {

            if (Integer.parseInt(temp1) < 100) {
                year = start.getCenturyOfEra();
                year = year * 100 + Integer.parseInt(temp1);
                if (modiword == 0) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                }

                else if (modiword == 1) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.START);
                }

                else if (modiword == 2) {
                    tc.addAttribute(TimexNames.value, String.valueOf(year / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.END);
                }

                return tc;
            }

            else {
                if (modiword == 0) {
                    start = new DateTime(Integer.parseInt(temp1), 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 9, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                }

                else if (modiword == 1) {
                    start = new DateTime(Integer.parseInt(temp1), 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 3, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.START);
                }

                else if (modiword == 2) {
                    start = new DateTime(Integer.parseInt(temp1) + 7, 1, 1, 0, 0, 0, 0);
                    finish = new DateTime(Integer.parseInt(temp1) + 9, 12, 31, 23, 59, 59, 59);
                    tc.addAttribute(TimexNames.value, String.valueOf(finish.getYear() / 10));
                    tc.addAttribute(TimexNames.mod, TimexNames.END);
                }
                return tc;

            }
        }

    }
    return null;
}

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.TimeOfDay.java

/**
 * Convert a phrase with a specific time to timex3 format
 * @param start anchor time//  w  w  w  .j  a v  a2 s  . c o  m
 * @param temporalPhrase
 * @return
 */
public static TimexChunk timeRule(DateTime start, TemporalPhrase temporalPhrase) {
    String phrase = temporalPhrase.getPhrase();
    phrase = phrase.trim().toLowerCase().replace(" +", " ");
    phrase = converter(phrase);

    TimexChunk tc = new TimexChunk();
    tc.addAttribute(TimexNames.type, TimexNames.TIME);

    Pattern timePattern = Pattern.compile(timePatternStr);
    Matcher timePatternMatch = timePattern.matcher(phrase);
    boolean timeMatchFound = timePatternMatch.find();
    if (timeMatchFound) {
        String whole = timePatternMatch.group(0);
        String hour = timePatternMatch.group(1);
        String minute = timePatternMatch.group(2);
        String second = timePatternMatch.group(3);
        String amPm = timePatternMatch.group(4);

        if (hour != null && hour.length() == 1) {
            hour = "0" + hour;
        }
        if (minute != null && minute.length() == 1) {
            minute = "0" + minute;
        }
        if (second != null && second.length() == 1) {
            second = "0" + second;
        }

        String res = "";

        if (hour == null) {
            return null;
        }

        else if (minute == null) {
            if (amPm == null) {
                return null;
            } else {
                amPm = amPm.replace(".", "");
                if (amPm.equals("am")) {
                    res = "T" + hour;
                } else {
                    res = "T" + (12 + Integer.parseInt(hour));
                }
            }
        }

        else if (second == null) {
            if (amPm == null) {
                res = "T" + hour + minute;
            } else {
                amPm = amPm.replace(".", "");
                if (amPm.equals("am")) {
                    res = "T" + hour + minute;
                } else {
                    res = "T" + (12 + Integer.parseInt(hour)) + minute;
                }
            }
        }

        else {
            if (amPm == null) {
                res = "T" + hour + minute + second;
            } else {
                amPm = amPm.replace(".", "");
                if (amPm.equals("am")) {
                    res = "T" + hour + minute + second;
                } else {
                    res = "T" + (12 + Integer.parseInt(hour)) + minute + second;
                }
            }
        }

        String residual = StringUtils.difference(whole, phrase);
        if (residual.length() > 0) {
            residual = residual.trim().replace(" +", " ").toLowerCase();
            TimexChunk date = TimexNormalizer
                    .normalize(new TemporalPhrase(residual, temporalPhrase.getTense()));
            if (date != null) {
                res = date.getAttribute(TimexNames.value) + res;
            }
        } else {
            DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
            String dateStr = fmt.print(start);
            res = dateStr + res;
        }
        tc.addAttribute(TimexNames.value, res);
        return tc;
    }

    return null;

}