Coverage Report - org.apache.commons.io.TaggedIOException
 
Classes in this File Line Coverage Branch Coverage Complexity
TaggedIOException
80%
8/10
87%
7/8
1.8
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.Serializable;
 21  
 
 22  
 /**
 23  
  * An {@link IOException} decorator that adds a serializable tag to the
 24  
  * wrapped exception. Both the tag and the original exception can be used
 25  
  * to determine further processing when this exception is caught.
 26  
  *
 27  
  * @since 2.0
 28  
  */
 29  0
 public class TaggedIOException extends IOExceptionWithCause {
 30  
 
 31  
     /**
 32  
      * Generated serial version UID.
 33  
      */
 34  
     private static final long serialVersionUID = -6994123481142850163L;
 35  
 
 36  
     /**
 37  
      * Checks whether the given throwable is tagged with the given tag.
 38  
      * <p>
 39  
      * This check can only succeed if the throwable is a
 40  
      * {@link TaggedIOException} and the tag is {@link Serializable}, but
 41  
      * the argument types are intentionally more generic to make it easier
 42  
      * to use this method without type casts.
 43  
      * <p>
 44  
      * A typical use for this method is in a <code>catch</code> block to
 45  
      * determine how a caught exception should be handled:
 46  
      * <pre>
 47  
      * Serializable tag = ...;
 48  
      * try {
 49  
      *     ...;
 50  
      * } catch (Throwable t) {
 51  
      *     if (TaggedIOExcepton.isTaggedWith(t, tag)) {
 52  
      *         // special processing for tagged exception
 53  
      *     } else {
 54  
      *         // handling of other kinds of exceptions
 55  
      *     }
 56  
      * }
 57  
      * </pre>
 58  
      *
 59  
      * @param throwable The Throwable object to check
 60  
      * @param tag tag object
 61  
      * @return {@code true} if the throwable has the specified tag,
 62  
      * otherwise {@code false}
 63  
      */
 64  
     public static boolean isTaggedWith(final Throwable throwable, final Object tag) {
 65  44
         return tag != null
 66  
             && throwable instanceof TaggedIOException
 67  
             && tag.equals(((TaggedIOException) throwable).tag);
 68  
     }
 69  
 
 70  
     /**
 71  
      * Throws the original {@link IOException} if the given throwable is
 72  
      * a {@link TaggedIOException} decorator the given tag. Does nothing
 73  
      * if the given throwable is of a different type or if it is tagged
 74  
      * with some other tag.
 75  
      * <p>
 76  
      * This method is typically used in a <code>catch</code> block to
 77  
      * selectively rethrow tagged exceptions.
 78  
      * <pre>
 79  
      * Serializable tag = ...;
 80  
      * try {
 81  
      *     ...;
 82  
      * } catch (Throwable t) {
 83  
      *     TaggedIOExcepton.throwCauseIfTagged(t, tag);
 84  
      *     // handle other kinds of exceptions
 85  
      * }
 86  
      * </pre>
 87  
      *
 88  
      * @param throwable an exception
 89  
      * @param tag tag object
 90  
      * @throws IOException original exception from the tagged decorator, if any
 91  
      */
 92  
     public static void throwCauseIfTaggedWith(final Throwable throwable, final Object tag)
 93  
             throws IOException {
 94  20
         if (isTaggedWith(throwable, tag)) {
 95  12
             throw ((TaggedIOException) throwable).getCause();
 96  
         }
 97  8
     }
 98  
 
 99  
     /**
 100  
      * The tag of this exception.
 101  
      */
 102  
     private final Serializable tag;
 103  
 
 104  
     /**
 105  
      * Creates a tagged wrapper for the given exception.
 106  
      *
 107  
      * @param original the exception to be tagged
 108  
      * @param tag tag of this exception
 109  
      */
 110  
     public TaggedIOException(final IOException original, final Serializable tag) {
 111  22
         super(original.getMessage(), original);
 112  22
         this.tag = tag;
 113  22
     }
 114  
 
 115  
     /**
 116  
      * Returns the serializable tag object.
 117  
      *
 118  
      * @return tag object
 119  
      */
 120  
     public Serializable getTag() {
 121  0
         return tag;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Returns the wrapped exception. The only difference to the overridden
 126  
      * {@link Throwable#getCause()} method is the narrower return type.
 127  
      *
 128  
      * @return wrapped exception
 129  
      */
 130  
     @Override
 131  
     public IOException getCause() {
 132  14
         return (IOException) super.getCause();
 133  
     }
 134  
 
 135  
 }