EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.classify]

COVERAGE SUMMARY FOR SOURCE FILE [SubclassClassifier.java]

nameclass, %method, %block, %line, %
SubclassClassifier.java67%  (2/3)90%  (9/10)95%  (116/122)91%  (29/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SubclassClassifier$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class SubclassClassifier$ClassComparator100% (1/1)100% (3/3)86%  (12/14)75%  (3/4)
compare (Class, Class): int 100% (1/1)75%  (6/8)67%  (2/3)
SubclassClassifier$ClassComparator (): void 100% (1/1)100% (3/3)100% (1/1)
SubclassClassifier$ClassComparator (SubclassClassifier$1): void 100% (1/1)100% (3/3)100% (1/1)
     
class SubclassClassifier100% (1/1)86%  (6/7)96%  (104/108)93%  (26/28)
setDefaultValue (Object): void 0%   (0/1)0%   (0/4)0%   (0/2)
SubclassClassifier (): void 100% (1/1)100% (4/4)100% (2/2)
SubclassClassifier (Map, Object): void 100% (1/1)100% (20/20)100% (6/6)
SubclassClassifier (Object): void 100% (1/1)100% (7/7)100% (2/2)
classify (Object): Object 100% (1/1)100% (63/63)100% (13/13)
getDefault (): Object 100% (1/1)100% (3/3)100% (1/1)
setTypeMap (Map): void 100% (1/1)100% (7/7)100% (2/2)

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 */
16package org.springframework.batch.classify;
17 
18import java.io.Serializable;
19import java.util.Comparator;
20import java.util.HashMap;
21import java.util.Map;
22import java.util.Set;
23import java.util.TreeSet;
24import java.util.concurrent.ConcurrentHashMap;
25import java.util.concurrent.ConcurrentMap;
26 
27/**
28 * A {@link Classifier} for a parameterised object type based on a map.
29 * Classifies objects according to their inheritance relation with the supplied
30 * type map. If the object to be classified is one of the keys of the provided
31 * map, or is a subclass of one of the keys, then the map entry value for that
32 * key is returned. Otherwise returns the default value which is null by
33 * default.
34 * 
35 * @author Dave Syer
36 * 
37 */
38public class SubclassClassifier<T, C> implements Classifier<T, C> {
39 
40        private ConcurrentMap<Class<? extends T>, C> classified = new ConcurrentHashMap<Class<? extends T>, C>();
41 
42        private C defaultValue = null;
43 
44        /**
45         * Create a {@link SubclassClassifier} with null default value.
46         * 
47         */
48        public SubclassClassifier() {
49                this(null);
50        }
51 
52        /**
53         * Create a {@link SubclassClassifier} with supplied default value.
54         * 
55         * @param defaultValue
56         */
57        public SubclassClassifier(C defaultValue) {
58                this(new HashMap<Class<? extends T>, C>(), defaultValue);
59        }
60 
61        /**
62         * Create a {@link SubclassClassifier} with supplied default value.
63         * 
64         * @param defaultValue
65         */
66        public SubclassClassifier(Map<Class<? extends T>, C> typeMap, C defaultValue) {
67                super();
68                this.classified = new ConcurrentHashMap<Class<? extends T>, C>(typeMap);
69                this.defaultValue = defaultValue;
70        }
71 
72        /**
73         * Public setter for the default value for mapping keys that are not found
74         * in the map (or their subclasses). Defaults to false.
75         * 
76         * @param defaultValue the default value to set
77         */
78        public void setDefaultValue(C defaultValue) {
79                this.defaultValue = defaultValue;
80        }
81 
82        /**
83         * Set the classifications up as a map. The keys are types and these will be
84         * mapped along with all their subclasses to the corresponding value. The
85         * most specific types will match first.
86         * 
87         * @param map a map from type to class
88         */
89        public void setTypeMap(Map<Class<? extends T>, C> map) {
90                this.classified = new ConcurrentHashMap<Class<? extends T>, C>(map);
91        }
92 
93        /**
94         * Return the value from the type map whose key is the class of the given
95         * Throwable, or its nearest ancestor if a subclass.
96         * 
97         */
98        public C classify(T classifiable) {
99 
100                if (classifiable == null) {
101                        return defaultValue;
102                }
103 
104                @SuppressWarnings("unchecked")
105                Class<? extends T> exceptionClass = (Class<? extends T>) classifiable.getClass();
106                if (classified.containsKey(exceptionClass)) {
107                        return classified.get(exceptionClass);
108                }
109 
110                // check for subclasses
111                Set<Class<? extends T>> classes = new TreeSet<Class<? extends T>>(new ClassComparator());
112                classes.addAll(classified.keySet());
113                for (Class<? extends T> cls : classes) {
114                        if (cls.isAssignableFrom(exceptionClass)) {
115                                C value = classified.get(cls);
116                                this.classified.put(exceptionClass, value);
117                                return value;
118                        }
119                }
120 
121                return defaultValue;
122        }
123 
124        /**
125         * Return the default value supplied in the constructor (default false).
126         */
127        final public C getDefault() {
128                return defaultValue;
129        }
130 
131        /**
132         * Comparator for classes to order by inheritance.
133         * 
134         * @author Dave Syer
135         * 
136         */
137        private static class ClassComparator implements Comparator<Class<?>>, Serializable {
138                /**
139                 * @return 1 if arg0 is assignable from arg1, -1 otherwise
140                 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
141                 */
142                public int compare(Class<?> arg0, Class<?> arg1) {
143                        if (arg0.isAssignableFrom(arg1)) {
144                                return 1;
145                        }
146                        return -1;
147                }
148        }
149 
150}

[all classes][org.springframework.batch.classify]
EMMA 2.0.5312 (C) Vladimir Roubtsov