/*
* Copyright © 2012 Alexander Yakushev
* All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this
* distribution, and is available at
* <http://www.eclipse.org/legal/epl-v10.html>.
*
* By using this software in any fashion, you are agreeing to be bound by the
* terms of this license.  You must not remove this notice, or any other, from
* this software.
*/
package neko.ui.adapters;

import android.widget.BaseAdapter;
import android.view.View;
import android.view.ViewGroup;
import clojure.lang.IFn;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;

public class InterchangeableListAdapter extends BaseAdapter {

    private IFn createViewFn;
    private IFn updateViewFn;
    private List data;

    public InterchangeableListAdapter(IFn createViewFn, IFn updateViewFn,
                                      List initialData) {
        super();
        this.createViewFn = createViewFn;
        this.updateViewFn = updateViewFn;
        this.data = initialData;
    }

    public InterchangeableListAdapter(IFn createViewFn, IFn updateViewFn,
                                      Map initialData) {
        this(createViewFn, updateViewFn, new ArrayList(initialData.entrySet()));
    }

    public InterchangeableListAdapter(IFn createViewFn, IFn updateViewFn,
                                      Set initialData) {
        this(createViewFn, updateViewFn, new ArrayList(initialData));
    }

    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = (View)createViewFn.invoke(parent.getContext());
        }
        updateViewFn.invoke(position, view, parent, data.get(position));
        return view;
    }

    public void setData(List newData) {
        this.data = newData;
        notifyDataSetInvalidated();
    }

    public void setData(Set newData) {
      setData(new ArrayList(newData));
    }

    public void setData(Map newData) {
      setData(new ArrayList(newData.entrySet()));
    }
}

@@ -630,80 +630,8 @@
 ist;
-%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.ArrayList;
 %0A%0Apu
@@ -1052,449 +1052,8 @@
  %7D%0A%0A
-    public InterchangeableListAdapter(IFn createViewFn, IFn updateViewFn,%0A                                      Map initialData) %7B%0A        this(createViewFn, updateViewFn, new ArrayList(initialData.entrySet()));%0A    %7D%0A%0A    public InterchangeableListAdapter(IFn createViewFn, IFn updateViewFn,%0A                                      Set initialData) %7B%0A        this(createViewFn, updateViewFn, new ArrayList(initialData));%0A    %7D%0A%0A    @Override%0A
     
@@ -1106,38 +1106,25 @@
 ze();%0A    %7D%0A
-    @Override
 %0A
+
     public O
@@ -1190,38 +1190,25 @@
 ion);%0A    %7D%0A
-    @Override
 %0A
+
     public l
@@ -1264,38 +1264,25 @@
 tion;%0A    %7D%0A
-    @Override
 %0A
+
     public b
@@ -1329,38 +1329,25 @@
 alse;%0A    %7D%0A
-    @Override
 %0A
+
     public b
@@ -1406,22 +1406,9 @@
   %7D%0A
-    @Override
 %0A
+
     
@@ -1762,29 +1762,24 @@
 ) %7B%0A        
-this.
 data = newDa
@@ -1829,186 +1829,6 @@
  %7D%0A%0A
-    public void setData(Set newData) %7B%0A      setData(new ArrayList(newData));%0A    %7D%0A%0A    public void setData(Map newData) %7B%0A      setData(new ArrayList(newData.entrySet()));%0A    %7D%0A
 %7D%0A

Revert "Makes the InterchangeableListAdapter useable with set and map datatypes."