package net.sf.kerner.utils.collections.list.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.sf.kerner.utils.Transformer;
import net.sf.kerner.utils.collections.list.FactoryList;
import net.sf.kerner.utils.collections.list.TransformerList;

public abstract class AbstractListTransformer<T, V> extends ListWalkerDefault<T> implements Transformer<T, V>,
        TransformerList<T, V> {

    protected final FactoryList<V> factory;

    protected volatile List<V> result;

    protected volatile int currentIndex;

    public AbstractListTransformer() {
        this(new ArrayListFactory<V>());
    }

    public AbstractListTransformer(final FactoryList<V> factory) {
        this.factory = factory;
        super.addVisitor(new DefaultListVisitorImpl<T>() {
            @Override
            public Void visit(final T element, final int index) {
                setCurrentIndex(index);
                result.add(transform(element));
                return null;
            }
        });
    }

    @Override
    public synchronized void beforeWalk() {
        super.beforeWalk();
        result = factory.createCollection();
    }

    protected int getCurrentIndex() {
        return currentIndex;
    }

    private void setCurrentIndex(final int currentIndex) {
        this.currentIndex = currentIndex;
    }

    /**
     * if {@code element == null}, empty list is returned.
     */
    public synchronized List<V> transformCollection(final Collection<? extends T> element) {
        if (element != null)
            walk(new ArrayList<T>(element));
        return result;
    }

}

/**********************************************************************
Copyright (c) 2013 Alexander Kerner. All rights reserved.
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 net.sf.kerner.utils.collections.list.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.sf.kerner.utils.Transformer;
import net.sf.kerner.utils.collections.list.FactoryList;
import net.sf.kerner.utils.collections.list.TransformerList;

/**
 * 
 * TODO description
 * 
 * <p>
 * <b>Example:</b><br>
 * 
 * </p>
 * <p>
 * 
 * <pre>
 * TODO example
 * </pre>
 * 
 * </p>
 * <p>
 * last reviewed: 0000-00-00
 * </p>
 * 
 * @author <a href="mailto:alexanderkerner24@gmail.com">Alexander Kerner</a>
 * @version 2013-08-15
 * 
 * @param <T>
 * @param <V>
 */
public abstract class AbstractListTransformer<T, V> extends ListWalkerDefault<T> implements Transformer<T, V>,
        TransformerList<T, V> {

    protected final FactoryList<V> factory;

    protected volatile List<V> result;

    protected volatile int currentIndex;

    public AbstractListTransformer() {
        this(new ArrayListFactory<V>());
    }

    public AbstractListTransformer(final FactoryList<V> factory) {
        this.factory = factory;
        super.addVisitor(new DefaultListVisitorImpl<T>() {
            @Override
            public Void visit(final T element, final int index) {
                setCurrentIndex(index);
                result.add(transform(element));
                return null;
            }
        });
    }

    @Override
    public synchronized void beforeWalk() {
        super.beforeWalk();
        result = factory.createCollection();
    }

    protected int getCurrentIndex() {
        return currentIndex;
    }

    public synchronized List<V> getAgain() {
        return Collections.unmodifiableList(result);
    }

    private void setCurrentIndex(final int currentIndex) {
        this.currentIndex = currentIndex;
    }

    /**
     * if {@code element == null}, empty list is returned.
     */
    public synchronized List<V> transformCollection(final Collection<? extends T> element) {
        if (element != null)
            walk(new ArrayList<T>(element));
        return result;
    }

}

Add AbstractListTransformer#getAgain().