/*
 * Copyright (C) 2017 Dimitri Pfaffenrodt
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 de.pfaffenrodt.adapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A ClassPresenterSelector selects a {@link Presenter} based on the item's
 * Java class.
 */
public class ClassPresenterSelector extends PresenterSelector{
    private final ArrayList<Presenter> mPresenters = new ArrayList<Presenter>();

    private final HashMap<Class<?>, Object> mClassMap = new HashMap<Class<?>, Object>();

    /**
     * Sets a presenter to be used for the given class.
     * @param cls The data model class to be rendered.
     * @param presenter The presenter that renders the objects of the given class.
     * @return This ClassPresenterSelector object.
     */
    public ClassPresenterSelector addClassPresenter(Class<?> cls, Presenter presenter) {
        mClassMap.put(cls, presenter);
        if (!mPresenters.contains(presenter)) {
            mPresenters.add(presenter);
        }
        return this;
    }

    /**
     * Sets a presenter selector to be used for the given class.
     * @param cls The data model class to be rendered.
     * @param presenterSelector The presenter selector that finds the right presenter for a given
     *                          class.
     * @return This ClassPresenterSelector object.
     */
    public ClassPresenterSelector addClassPresenterSelector(Class<?> cls,
                                                                                                PresenterSelector presenterSelector) {
        mClassMap.put(cls, presenterSelector);
        Presenter[] innerPresenters = presenterSelector.getPresenters();
        for (int i = 0; i < innerPresenters.length; i++)
            if (!mPresenters.contains(innerPresenters[i])) {
                mPresenters.add(innerPresenters[i]);
            }
        return this;
    }

    @Override
    public Presenter getPresenter(Object item) {
        Class<?> cls = item.getClass();
        Object presenter = null;

        do {
            presenter = mClassMap.get(cls);
            if (presenter instanceof PresenterSelector) {
                Presenter innerPresenter = ((PresenterSelector) presenter).getPresenter(item);
                if (innerPresenter != null) {
                    return innerPresenter;
                }
            }
            cls = cls.getSuperclass();
        } while (presenter == null && cls != null);

        return (Presenter) presenter;
    }

    @Override
    public Presenter[] getPresenters() {
        return mPresenters.toArray(new Presenter[mPresenters.size()]);
    }
}

@@ -731,17 +731,16 @@
 ashMap;%0A
-%0A
 /**%0A * A
@@ -837,16 +837,22 @@
 %0Apublic 
+final 
 class Cl
@@ -897,16 +897,17 @@
 Selector
+ 
 %7B%0A    pr
@@ -977,25 +977,24 @@
 esenter%3E();%0A
-%0A
     private 
@@ -1070,17 +1070,16 @@
 ect%3E();%0A
-%0A
     /**%0A
@@ -1580,25 +1580,24 @@
 this;%0A    %7D%0A
-%0A
     /**%0A    
@@ -1986,92 +1986,8 @@
 ls,%0A
-                                                                                    
     
@@ -2025,32 +2025,32 @@
 nterSelector) %7B%0A
+
         mClassMa
@@ -2214,28 +2214,24 @@
 ++)%0A        
-    
 if (!mPresen
@@ -2271,36 +2271,32 @@
 ) %7B%0A            
-    
 mPresenters.add(
@@ -2316,20 +2316,16 @@
 rs%5Bi%5D);%0A
-    
         
@@ -2345,33 +2345,32 @@
 urn this;%0A    %7D%0A
-%0A
     @Override%0A  
@@ -2489,17 +2489,16 @@
 = null;%0A
-%0A
         
@@ -2911,17 +2911,16 @@
  null);%0A
-%0A
         
@@ -2955,17 +2955,16 @@
 ;%0A    %7D%0A
-%0A
     @Ove
@@ -3087,10 +3087,9 @@
 ;%0A    %7D%0A
+
 %7D
-%0A

update ClassPresenterSelector. removed some whitespaces