Java String Match Count countMatchingLines(Throwable ex, Throwable cause)

Here you can find the source of countMatchingLines(Throwable ex, Throwable cause)

Description

Count the number of lines that match between an exception and some cause.

License

Open Source License

Declaration

public static int countMatchingLines(Throwable ex, Throwable cause) 

Method Source Code

//package com.java2s;
/*/* w w w.  j  a  v  a 2  s.co m*/
  Copyright (c) 2005-2014 www.kodemore.com
    
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
    
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
    
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

public class Main {
    /**
     * Count the number of lines that match between an exception and some
     * cause.  The comparison is performed from the end of the stack trace.
     */
    public static int countMatchingLines(Throwable ex, Throwable cause) {
        StackTraceElement[] exElements = ex.getStackTrace();
        StackTraceElement[] causeElements = cause.getStackTrace();

        int i = 0;
        int n = Math.min(exElements.length, causeElements.length);
        while (i < n) {
            StackTraceElement exElement = exElements[exElements.length - i - 1];
            StackTraceElement causeElement = causeElements[causeElements.length - i - 1];
            if (!causeElement.equals(exElement))
                break;
            i++;
        }
        return i;
    }
}

Related

  1. countMatches(String string, char find)
  2. countMatches(String string, char toMatch)
  3. countMatches(String string, String other)
  4. countMatches(String theString, String... sep)
  5. countMatches(String[] query, String[] entry)