    001package javax.visrec.ml.data;
    002
    003import java.util.ArrayList;
    004import java.util.Arrays;
    005import java.util.Iterator;
    006import java.util.List;
    007import java.util.Random;
    008import java.util.stream.Collectors;
    009
    010/**
    011 * Basic implementation of {@link DataSet} interface
    012 *
    013 * @author Zoran Sevarac
    014 */
    015public class BasicDataSet<E> implements DataSet<E>{
    016
    017    private List<E> items;
    018    private Column[] columns;
    019
    020    /**
    021     * Creates an instance of {@link BasicDataSet}
    022     * @param cols columns of the data set.
    023     */
    024    public BasicDataSet(Column[] cols) {
    025        this.columns = cols;
    026        items = new ArrayList<>();
    027    }
    028
    029    /**
    030     * Creates an instance of {@link BasicDataSet}
    031     * @param elements items of the data set.
    032     */
    033    public BasicDataSet(List<E> elements) {
    034        this.items = elements;
    035    }
    036
    037    @Override
    038    public DataSet<E> add(E item) {
    039        items.add(item);
    040        return this;
    041    }
    042
    043    @Override
    044    public DataSet<E>  addAll(DataSet<E> items) {
    045        this.items.addAll(this.items);
    046        return this;
    047    }
    048
    049    @Override
    050    public E get(int index) {
    051        return items.get(index);
    052    }
    053
    054    @Override
    055    public void clear() {
    056        items.clear();
    057    }
    058
    059    @Override
    060    public boolean isEmpty() {
    061        return items.isEmpty();
    062    }
    063
    064    @Override
    065    public int size() {
    066        return items.size();
    067    }
    068
    069    @Override
    070    public DataSet<E>[]  split(int parts) {
    071        throw new UnsupportedOperationException("not implemented");
    072    }
    073
    074    @Override
    075    public DataSet<E>[]  split(int parts, Random rnd) {
    076        throw new UnsupportedOperationException("not implemented");
    077    }
    078
    079    @Override
    080    public DataSet<E>[] split(double... parts) {
    081        throw new UnsupportedOperationException("not implemented");
    082    }
    083
    084    @Override
    085    public DataSet[] split(Random rnd, double... parts) {
    086        throw new UnsupportedOperationException("not implemented");
    087    }
    088
    089
    090    // this shuld go to utilities class or default method?
    091    public String[] getTargetLabels() {
    092        List<String> targetLabels = Arrays.asList(columns).stream()
    093                        .filter((col) -> col.isTarget())
    094                        .map((col) -> col.getName() )
    095                        .collect(Collectors.toList());
    096        return targetLabels.toArray(new String[0]);
    097    }
    098
    099//    public void setColumnNames(String[] labels) {
    100//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    101//    }
    102
    103    @Override
    104    public Iterator<E> iterator() {
    105        return items.iterator();
    106    }
    107
    108    @Override
    109    public List<E> getItems() {
    110        return items;
    111    }
    112
    113
    114
    115}