| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.classify; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | /** |
| 23 | * A {@link Classifier} for exceptions that has only two classes (true and |
| 24 | * false). Classifies objects according to their inheritance relation with the |
| 25 | * supplied types. If the object to be classified is one of the provided types, |
| 26 | * or is a subclass of one of the types, then the non-default value is returned |
| 27 | * (usually true). |
| 28 | * |
| 29 | * @see SubclassClassifier |
| 30 | * |
| 31 | * @author Dave Syer |
| 32 | * |
| 33 | */ |
| 34 | public class BinaryExceptionClassifier extends SubclassClassifier<Throwable, Boolean> { |
| 35 | |
| 36 | /** |
| 37 | * Create a binary exception classifier with the provided default value. |
| 38 | * |
| 39 | * @param defaultValue defaults to false |
| 40 | */ |
| 41 | public BinaryExceptionClassifier(boolean defaultValue) { |
| 42 | super(defaultValue); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create a binary exception classifier with the provided classes and their |
| 47 | * subclasses. The mapped value for these exceptions will be the one |
| 48 | * provided (which will be the opposite of the default). |
| 49 | * |
| 50 | * @param value |
| 51 | */ |
| 52 | public BinaryExceptionClassifier(Collection<Class<? extends Throwable>> exceptionClasses, boolean value) { |
| 53 | this(!value); |
| 54 | if (exceptionClasses != null) { |
| 55 | Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>(); |
| 56 | for (Class<? extends Throwable> type : exceptionClasses) { |
| 57 | map.put(type, !getDefault()); |
| 58 | } |
| 59 | setTypeMap(map); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create a binary exception classifier with the default value false and |
| 65 | * value mapping true for the provided classes and their subclasses. |
| 66 | */ |
| 67 | public BinaryExceptionClassifier(Collection<Class<? extends Throwable>> exceptionClasses) { |
| 68 | this(exceptionClasses, true); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Create a binary exception classifier using the given classification map |
| 73 | * and a default classification of false. |
| 74 | * |
| 75 | * @param typeMap |
| 76 | */ |
| 77 | public BinaryExceptionClassifier(Map<Class<? extends Throwable>, Boolean> typeMap) { |
| 78 | this(typeMap, false); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Create a binary exception classifier using the given classification map |
| 83 | * and a default classification of false. |
| 84 | * |
| 85 | * @param typeMap |
| 86 | */ |
| 87 | public BinaryExceptionClassifier(Map<Class<? extends Throwable>, Boolean> typeMap, boolean defaultValue) { |
| 88 | super(typeMap, defaultValue); |
| 89 | } |
| 90 | |
| 91 | } |