Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.batch.classify;
 
 import java.util.Map;
 import java.util.Set;
A Classifier for a parameterised object type based on a map. Classifies objects according to their inheritance relation with the supplied type map. If the object to be classified is one of the keys of the provided map, or is a subclass of one of the keys, then the map entry vale for that key is returned. Otherwise returns the default value which is null by default.

Author(s):
Dave Syer
 
 public class SubclassClassifier<T, C> implements Classifier<T, C> {
 
 	private Map<Class<? extends T>, C> classified = new HashMap<Class<? extends T>, C>();
 
 	private C defaultValue = null;

Create a SubclassClassifier with null default value.
 
 	public SubclassClassifier() {
 		this(null);
 	}

Create a SubclassClassifier with supplied default value.

Parameters:
defaultValue
 
 	public SubclassClassifier(C defaultValue) {
 		this(new HashMap<Class<? extends T>, C>(), defaultValue);
 	}

Create a SubclassClassifier with supplied default value.

Parameters:
defaultValue
 
 	public SubclassClassifier(Map<Class<? extends T>, C> typeMap, C defaultValue) {
 		super();
 		this. = new HashMap<Class<? extends T>, C>(typeMap);
 		this. = defaultValue;
 	}

Public setter for the default value for mapping keys that are not found in the map (or their subclasses). Defaults to false.

Parameters:
defaultValue the default value to set
 
 	public void setDefaultValue(C defaultValue) {
 		this. = defaultValue;
 	}

Set the classifications up as a map. The keys are types and these will be mapped along with all their subclasses to the corresponding value. The most specific types will match first.

Parameters:
map a map from type to class
 
 	public void setTypeMap(Map<Class<? extends T>, C> map) {
 		this. = new HashMap<Class<? extends T>, C>(map);
 	}

Return the value from the type map whose key is the class of the given Throwable, or its nearest ancestor if a subclass.
 
 	public C classify(T classifiable) {
 
 		if (classifiable == null) {
 			return ;
		}
		@SuppressWarnings("unchecked")
		Class<? extends T> exceptionClass = (Class<? extends T>) classifiable.getClass();
		if (.containsKey(exceptionClass)) {
			return .get(exceptionClass);
		}
		// check for subclasses
		Set<Class<? extends T>> classes = new TreeSet<Class<? extends T>>(new ClassComparator());
		for (Class<? extends T> cls : classes) {
			if (cls.isAssignableFrom(exceptionClass)) {
value = .get(cls);
				this..put(exceptionClassvalue);
				return value;
			}
		}
		return ;
	}

Return the default value supplied in the constructor (default false).
	final public C getDefault() {
		return ;
	}

Comparator for classes to order by inheritance.

Author(s):
Dave Syer
	private static class ClassComparator implements Comparator<Class<?>>, Serializable {

Returns:
1 if arg0 is assignable from arg1, -1 otherwise
See also:
java.util.Comparator.compare(java.lang.Object,java.lang.Object)
		public int compare(Class<?> arg0Class<?> arg1) {
			if (arg0.isAssignableFrom(arg1)) {
				return 1;
			}
			return -1;
		}
	}
New to GrepCode? Check out our FAQ X