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.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    
022    /**
023     * An inheritence sequence. The target class is first, 
024     * followed by its superclass and its interfaces. Then the 
025     * lookup recurses through the superclass and interfaces 
026     * with the same strategy. 
027     * It is ensured that java.lang.Object is last in the sequence.
028     */
029    public class BasicInheritor implements Inheritor {
030    
031        private Class clazz;
032        
033        public BasicInheritor() {
034        }
035        
036        public BasicInheritor(Class clazz) {
037            setTarget(clazz);
038        }
039        
040        public void setTarget(Class clazz) {
041            this.clazz = clazz;
042        }
043        
044        public Class getTarget() {
045            return clazz;
046        }
047        
048        public Iterator iterator() {
049            List list = new ArrayList();
050    
051            addToList(list, clazz);
052            addClassParents(list, clazz);
053    
054            list.add( Object.class );
055    
056            return list.iterator();
057        }
058        
059        private void addClassParents(List list, Class clazz) {
060            if (clazz == null) {
061                return;
062            }
063    
064            Class superclass = clazz.getSuperclass();
065            addToList(list, superclass);
066    
067            Class[] interfaces = clazz.getInterfaces();
068            for (int i = 0; i < interfaces.length; i++) {
069                addToList(list, interfaces[i]);
070            }
071    
072            for (int i = 0; i < interfaces.length; i++) {
073                addClassParents(list, interfaces[i]);
074            }
075    
076            addClassParents(list, superclass);
077        }
078        
079        private void addToList(List list, Class clazz) {
080            if( clazz == null ) {
081                return;
082            }
083    
084            if( Object.class == clazz ) {
085                return;
086            }
087    
088            if( list.contains( clazz ) ) {
089                return;
090            }
091    
092            list.add(clazz);
093        }
094        
095    }