/*
* GRAL: GRAphing Library for Java(R)
*
* (C) Copyright 2009-2016 Erich Seifert ,
* Michael Seifert
*
* This file is part of GRAL.
*
* GRAL is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRAL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GRAL. If not, see .
*/
package de.erichseifert.gral.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import de.erichseifert.gral.data.statistics.Statistics;
/**
* Class for accessing a specific column of a data source. The data of the
* column can be accessed using the {@code get(int)} method.
*
* Example for accessing value at column 2, row 3 of a data source:
*
* Column col = new Column(dataSource, 2);
* Number v = col.get(3);
*
*
* @see DataSource
*/
public class Column> implements Iterable, Serializable {
/** Version id for serialization. */
private static final long serialVersionUID = 7380420622890027262L;
private final Class dataType;
private final List data;
public Column(Class dataType, T... data) {
this(dataType, Arrays.asList(data));
}
public Column(Class dataType, Iterable data) {
this.dataType = dataType;
this.data = new ArrayList();
for (T item : data) {
this.data.add(item);
}
}
public T get(int row) {
return row >= data.size() ? null : data.get(row);
}
public int size() {
return data.size();
}
/**
* Returns whether this column only contains numbers.
* @return {@code true} if this column is numeric, otherwise {@code false}.
*/
public boolean isNumeric() {
return Number.class.isAssignableFrom(getType());
}
public Class extends Comparable>> getType() {
return dataType;
}
public double getStatistics(String key) {
return new Statistics(data).get(key);
}
@Override
public int hashCode() {
return dataType.hashCode() ^ data.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Column)) {
return false;
}
Column> column = (Column>) obj;
return getType().equals(column.getType()) && data.equals(column.data);
}
@Override
public Iterator iterator() {
return data.iterator();
}
}
Ads help maintain this website.