001 /* 002 * Copyright 2003-2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.convert1.util; 017 018 import java.util.HashMap; 019 import java.util.Iterator; 020 021 /** 022 * A map which stores objects by a key Class. 023 * When obtaining the object, it will check inheritence and 024 * interface trees to see if the Class matches. 025 */ 026 public class ClassMap extends ProxyMap { 027 028 private Inheritor inh; 029 030 public ClassMap() { 031 this(new BasicInheritor()); 032 } 033 034 public ClassMap(Inheritor inh) { 035 super(new HashMap()); 036 this.inh = inh; 037 } 038 039 public Inheritor getInheritor() { 040 return inh; 041 } 042 043 /** 044 * Get the object from the map. If the key is not 045 * a Class object, then it uses the Class of the object. 046 */ 047 public Object get(Object key) { 048 if(key == null) { 049 return null; 050 } 051 Class clss = null; 052 053 if(key instanceof Class) { 054 clss = (Class) key; 055 } else { 056 clss = key.getClass(); 057 } 058 059 Object obj = super.get(clss); 060 061 if(obj == null) { 062 063 inh.setTarget(clss); 064 Iterator itr = inh.iterator(); 065 while(itr.hasNext() && obj == null) { 066 obj = super.get(itr.next()); 067 } 068 } 069 070 return obj; 071 } 072 073 }