11/*
2- * Copyright 2006-2022 the original author or authors.
2+ * Copyright 2006-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2424 * A {@link Classifier} for a parameterised object type based on a map. Classifies objects
2525 * according to their inheritance relation with the supplied type map. If the object to be
2626 * classified is one of the keys of the provided map, or is a subclass of one of the keys,
27- * then the map entry value for that key is returned. Otherwise returns the default value
27+ * then the map entry value for that key is returned. Otherwise, returns the default value
2828 * which is null by default.
2929 *
3030 * @author Dave Syer
3131 * @author Gary Russell
32+ * @author Artem Bilan
3233 * @param <T> the type of the thing to classify
3334 * @param <C> the output of the classifier
3435 */
3536@ SuppressWarnings ("serial" )
3637public class SubclassClassifier <T , C > implements Classifier <T , C > {
3738
38- private ConcurrentMap <Class <? extends T >, C > classified = new ConcurrentHashMap <>() ;
39+ private ConcurrentMap <Class <? extends T >, C > classified ;
3940
40- private C defaultValue = null ;
41+ private C defaultValue ;
4142
4243 /**
4344 * Create a {@link SubclassClassifier} with null default value.
44- *
4545 */
4646 public SubclassClassifier () {
4747 this (null );
@@ -86,7 +86,7 @@ public void setTypeMap(Map<Class<? extends T>, C> map) {
8686 }
8787
8888 /**
89- * The keys is the type and this will be mapped along with all subclasses to the
89+ * The key is the type and this will be mapped along with all subclasses to the
9090 * corresponding value. The most specific types will match first.
9191 * @param type the type of the input object
9292 * @param target the target value for all such types
@@ -103,7 +103,6 @@ public void add(Class<? extends T> type, C target) {
103103 */
104104 @ Override
105105 public C classify (T classifiable ) {
106-
107106 if (classifiable == null ) {
108107 return this .defaultValue ;
109108 }
@@ -116,7 +115,9 @@ public C classify(T classifiable) {
116115
117116 // check for subclasses
118117 C value = null ;
119- for (Class <?> cls = exceptionClass ; !cls .equals (Object .class ) && value == null ; cls = cls .getSuperclass ()) {
118+ for (Class <?> cls = exceptionClass .getSuperclass (); !cls .equals (Object .class )
119+ && value == null ; cls = cls .getSuperclass ()) {
120+
120121 value = this .classified .get (cls );
121122 }
122123
0 commit comments