Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil -*-
   *
   * Copyright (c) 2011 Edugility LLC.
   *
   * 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.
  *
  * THIS 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.
  *
  * The original copy of this license is available at
  * http://www.opensource.org/license/mit-license.html.
  */
 package com.edugility.throwables;
 
 
 import java.util.List;

A class to assist with processing java.lang.Throwable instances.

Author(s):
Laird Nelson
Version:
1.0-SNAPSHOT
Since:
1.0-SNAPSHOT
 
 public final class Throwables {

  
Creates a new Throwables object.
 
   private Throwables() {
     super();
   }

  
Returns the "deepest" cause reachable from the supplied java.lang.Throwable.

Parameters:
t the java.lang.Throwable to investigate; may be null
Returns:
the "deepest" cause reachable from the supplied java.lang.Throwable, or null
 
   public static final Throwable getPrimordialCause(Throwable t) {
     if (t == null) {
       return null;
     }
     Throwable cause = t;
     while ((cause = cause.getCause()) != null) {
       t = cause;
     }
     return t;
   }

  
Creates and returns a view of the supplied java.lang.Throwable and its causal chain as a java.util.List.

This method never returns null.

This method allocates a new java.util.List when called.

Parameters:
throwable the java.lang.Throwable in question; may be null
Returns:
a java.util.List of java.lang.Throwables; never null; the return value will always contain the supplied java.lang.Throwable as its first element unless the supplied java.lang.Throwable is null
 
   public static final List<ThrowabletoList(final Throwable throwable) {
     final List<Throwablel = toList(throwablenull);
     if (l == null) {
       return Collections.emptyList();
     } else {
       return Collections.unmodifiableList(l);
     }
   }
  
  
Creates and returns a view of the supplied java.lang.Throwable and its causal chain as a java.util.List.

This method never returns null.

This method mutates the supplied java.util.List when possible. If the supplied java.util.List is null, a new java.util.List is allocated.

Parameters:
throwable the java.lang.Throwable in question; may be null
l the java.util.List to return; may be null in which case a new java.util.List will be allocated
Returns:
a java.util.List of java.lang.Throwables; never null; the return value will always contain the supplied java.lang.Throwable as its first element unless the supplied java.lang.Throwable is null
  private static final List<ThrowabletoList(final Throwable throwableList<Throwablel) {
    final List<ThrowablereturnValue;
    if (throwable == null) {
      returnValue = Collections.emptyList();
    } else {
      final Iterable<?> throwables;
      if (throwable instanceof Iterable) {
        throwables = (Iterable<?>)throwable;
      } else {
        throwables = Collections.singleton(throwable);
      }
      if (l == null) {
        l = new ArrayList<Throwable>();
      }
      final Iterator<?> iterator = throwables.iterator();
      boolean found = false;
      if (iterator != null) {
        while (iterator.hasNext()) {
          final Object o = iterator.next();
          if (o instanceof Throwable) {
            final Throwable t = (Throwable)o;
            found = found || t == throwable;
            l.add(t);
            final Throwable cause = t.getCause();
            if (cause != null) {
              assert cause != t// shouldn't be possible according to Throwable contract
              final List<Throwablelist = toList(causel); // recursive call
              assert list == l;
              found = found || l.contains(throwable);
            }
          }
        }
      }
      if (!found) {
        l.add(0, throwable);
      }
      returnValue = l;
    }
    return returnValue;
  }

  
Returns the first java.lang.Throwable in the java.lang.Throwable.getCause() of the supplied java.lang.Throwable (or the supplied java.lang.Throwable itself) that is an instance of the supplied java.lang.Class.

This method may return null.

Parameters:
t the java.lang.Throwable whose causal chain should be investigated; may be null in which case null will be returned
throwableClass the java.lang.Class whose java.lang.Class.isInstance(java.lang.Object) method will be called; if null then null will be returned
Returns:
the first java.lang.Throwable in the causal chain that is an instance of the supplied java.lang.Class, or null
  public static final <T extends Throwable> T firstInstance(Throwable tfinal Class<T> throwableClass) {
    @SuppressWarnings("unchecked")
    final T returnValue = (T)nthInstance(tnew InstanceOfPredicate(throwableClass), 0);
    return returnValue;
  }

  
Returns a java.lang.Throwable in the java.lang.Throwable.getCause() of the supplied java.lang.Throwable (or the supplied java.lang.Throwable itself) that is the appropriate instance of the supplied java.lang.Class.

This method may return null.

Parameters:
throwable the java.lang.Throwable whose causal chain should be investigated; may be null in which case null will be returned
throwableClass the java.lang.Class whose java.lang.Class.isInstance(java.lang.Object) method will be called; if null then null will be returned
zeroBasedOccurrence the 0-based number that identifies which of the potentially many matches to return. 0 will return the first java.lang.Throwable that matches, 1 will return the second one, and so on.
Returns:
the nth java.lang.Throwable in the causal chain that is an instance of the supplied java.lang.Class, or null
  public static final <T extends Throwable> T nthInstance(Throwable throwablefinal Class<T> throwableClassint zeroBasedOccurrence) {
    @SuppressWarnings("unchecked")
    final T returnValue = (T)nthInstance(throwablenew InstanceOfPredicate(throwableClass), zeroBasedOccurrence);
    return returnValue;
  }

  
Returns the nth occurrence of a java.lang.Throwable—in the supplied java.lang.Throwable's java.lang.Throwable.getCause()—that is Throwables.Predicate.apply(java.lang.Throwable), where n is equal to the supplied zeroBasedOccurrence parameter value plus one.

This method may return null.

Parameters:
throwable the java.lang.Throwable to investigate; may be null in which case null will be returned
predicate the Throwables.Predicate to use to evaluate fitness; may be null in which case this method will behave as though a Throwables.Predicate had been supplied to it that returns true in all cases from its Throwables.Predicate.apply(java.lang.Throwable) method
zeroBasedOccurrence which occurrence to return, starting at 0
Returns:
the zeroBasedOccurrence + 1th occurrence of a java.lang.Throwable—in the supplied java.lang.Throwable's causal chain—that is Throwables.Predicate.apply(java.lang.Throwable)
  public static final Throwable nthInstance(Throwable throwablefinal Predicate predicateint zeroBasedOccurrence) {
    zeroBasedOccurrence = Math.max(0, zeroBasedOccurrence);
    Throwable returnValue = null;
    final List<Throwablelist = toList(throwable);
    assert list != null;
    // Note: do not use getCause(); the work has already been done
    int numberOfOccurrences = 0;
    for (int i = 0; i < list.size(); i++) {
      final Throwable t = list.get(i);
      if (t != null && (predicate == null || predicate.apply(t)) && zeroBasedOccurrence == numberOfOccurrences++) {
        returnValue = t;
        break;
      }
    }
    return returnValue;
  }

  
Returns the last java.lang.Throwable in the java.lang.Throwable.getCause() of the supplied java.lang.Throwable (or the supplied java.lang.Throwable itself) that is an instance of the supplied java.lang.Class.

This method may return null.

Parameters:
t the java.lang.Throwable whose causal chain should be investigated; may be null in which case null will be returned
throwableClass the java.lang.Class whose java.lang.Class.isInstance(java.lang.Object) method will be called; if null then null will be returned
Returns:
the last java.lang.Throwable in the causal chain that is an instance of the supplied java.lang.Class, or null
  public static final <T extends Throwable> T lastInstance(Throwable tfinal Class<T> throwableClass) {
    T returnValue = null;
    if (throwableClass != null) {
      while (t != null) {
        if (throwableClass.isInstance(t)) {
          returnValue = throwableClass.cast(t);
        }
        t = t.getCause();
      }
    }
    return returnValue;
  }

  
A predicate that evaluates a java.lang.Throwable for some user-defined condition.

Author(s):
Laird Nelson
Version:
1.0-SNAPSHOT
Since:
1.0-SNAPSHOT
  public static interface Predicate extends Serializable {

    
Evaluates the supplied java.lang.Throwable for fitness and returns true if the supplied java.lang.Throwable is deemed fit.

Parameters:
t the java.lang.Throwable in question; may be null
Returns:
true if the supplied java.lang.Throwable is fit; false otherwise
    public boolean apply(final Throwable t);
  }

  
A Throwables.Predicate that determines whether a java.lang.Throwable is an instance of a given java.lang.Class.

Author(s):
Laird Nelson
Version:
1.0-SNAPSHOT
Since:
1.0-SNAPSHOT
  private static final class InstanceOfPredicate implements Predicate {

    
The version number of the serialized representation of this class.

    private static final long serialVersionUID = 1L;

    
The java.lang.Class to use for the test.

This field may be null.

    private final Class<? extends Throwablecls;

    
Creates a new Throwables.InstanceOfPredicate.

Parameters:
cls the java.lang.Class to use for the predicate test; may be null
    private InstanceOfPredicate(final Class<? extends Throwablecls) {
      super();
      this. = cls;
    }

    
Returns true if the supplied java.lang.Throwable is an instance of the java.lang.Class supplied to this Throwables.InstanceOfPredicate's Throwables.InstanceOfPredicate(java.lang.Class).

Parameters:
t the java.lang.Throwable to test; may be null
Returns:
true if the supplied java.lang.Throwable is an instance of the java.lang.Class supplied to this Throwables.InstanceOfPredicate's Throwables.InstanceOfPredicate(java.lang.Class); false in all other cases
    @Override
    public final boolean apply(final Throwable t) {
      if (t == null) {
        return false;
      }
      if (this. == null) {
        return false;
      }
      if (!this..isInstance(t)) {
        return false;
      }
      return true;
    }
  }
New to GrepCode? Check out our FAQ X