All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aksw.commons.collections.ConvertingList Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.collections;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;

import com.google.common.base.Converter;

public class ConvertingList>
    extends ConvertingCollection
    implements List
{
    public ConvertingList(C backend, Converter converter) {
        super(backend, converter);
    }

    @Override
    public boolean addAll(Collection c) {
        Collection transformed = c.stream().map(item -> converter.reverse().convert(item)).collect(Collectors.toList());
        boolean result = backend.addAll(transformed);
        return result;
    }
    
    @Override
    public boolean addAll(int index, Collection c) {
        Collection transformed = c.stream().map(item -> converter.reverse().convert(item)).collect(Collectors.toList());
        boolean result = backend.addAll(index, transformed);
        return result;
    }

    @Override
    public F get(int index) {
        B raw = backend.get(index);
        F result = converter.convert(raw);
        return result;
    }

    @Override
    public F set(int index, F element) {
        B raw = converter.reverse().convert(element);
        backend.set(index, raw);
        return element;
    }

    @Override
    public void add(int index, F element) {
        addAll(index, Collections.singleton(element));
    }

    @Override
    public F remove(int index) {
        B raw = backend.remove(index);
        F result = converter.convert(raw);
        return result;
    }

    @Override
    public int indexOf(Object o) {
        // TODO Check whether this cast is possible
        int result;
        try {
            F front = (F)o;
            B raw = converter.reverse().convert(front);
            result = backend.indexOf(raw);

        } catch(ClassCastException e) {
            result = -1;
            // Treat class cast exception as 'item does not exist in listt'
        }

        return result;
    }

    @Override
    public int lastIndexOf(Object o) {
        // TODO Check whether this cast is possible
        int result;
        try {
            F front = (F)o;
            B raw = converter.reverse().convert(front);
            result = backend.lastIndexOf(raw);

        } catch(ClassCastException e) {
            result = -1;
            // Treat class cast exception as 'item does not exist in listt'
        }

        return result;
    }

    @Override
    public ListIterator listIterator() {
        ListIterator core = backend.listIterator();

        ListIterator result = new ConvertingListIterator<>(core, converter);
        return result;
    }

    @Override
    public ListIterator listIterator(int index) {
        ListIterator core = backend.listIterator();

        ListIterator result = new ConvertingListIterator<>(core, converter);
        return result;
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        List subList = backend.subList(fromIndex, toIndex);
        List result = new ConvertingList<>(subList, converter);
        return result;
    }

//    public static > List createSafe(Collection backend, Converter converter) {
//        Collection safeBackend = ConverterUtils.safeCollection(backend, converter);
//        List result = new ListFromConverter<>(safeBackend, converter);
//        return result;
//    }

}