
dk.eobjects.metamodel.data.Row Maven / Gradle / Ivy
/**
* This file is part of MetaModel.
*
* MetaModel is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MetaModel 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MetaModel. If not, see .
*/
package dk.eobjects.metamodel.data;
import java.io.Serializable;
import java.util.Comparator;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import dk.eobjects.metamodel.query.SelectItem;
import dk.eobjects.metamodel.schema.Column;
import dk.eobjects.metamodel.util.ObjectComparator;
/**
* Represents a row of data in a DataSet. Each row is a mapping between
* SelectItems and values for each SelectItem.
*
* @see DataSet
* @see SelectItem
*/
public class Row implements Serializable, Cloneable {
private static final long serialVersionUID = 8334770862164547972L;
private SelectItem[] _items;
private Object[] _values;
/**
* Constructs a row from an array of SelectItems and an array of
* corresponding values
*
* @param items
* the array of SelectItems
* @param values
* the array of values
*/
public Row(SelectItem[] items, Object[] values) {
if (items == null || values == null) {
throw new IllegalArgumentException("Arguments cannot be null");
}
if (items.length != values.length) {
throw new IllegalArgumentException(
"SelectItems length and values length must be equal");
}
_items = items;
_values = values;
}
/**
* Gets the value of the provided SelectItem.
*
* @param item
* @return the value that corresponds to the provided SelectItem. Can be
* null if either the value is null or if no value exists
* that matches the SelectItem.
*/
public Object getValue(SelectItem item) {
if (item != null) {
for (int i = 0; i < _items.length; i++) {
if (item.equalsIgnoreAlias(_items[i])) {
return _values[i];
}
}
}
return null;
}
/**
* Shorthand method for getting the value of a SelectItem based on the
* provided column. Invoking this method is equivalent to invoking
* getValue(new SelectItem(column)).
*
* @param column
* @return the value of the specified column
*/
public Object getValue(Column column) {
SelectItem selectItem = new SelectItem(column);
return getValue(selectItem);
}
/**
* Gets the index of a SelectItem in the row.
*
* @param item
* @return the index of a SelectItem in the row. If the SelectItem is not
* found -1 will be returned.
*/
public int indexOf(SelectItem item) {
if (item != null) {
for (int i = 0; i < _items.length; i++) {
if (item.equalsIgnoreAlias(_items[i])) {
return i;
}
}
}
return -1;
}
/**
* Gets the value of the row at a given index
*
* @param index
* @return the value at the specified index
* @throws ArrayIndexOutOfBoundsException
* if the provided index is out of range
*/
public Object getValue(int index) throws ArrayIndexOutOfBoundsException {
return _values[index];
}
public SelectItem[] getSelectItems() {
return _items;
}
public Object[] getValues() {
return _values;
}
@Override
public String toString() {
return "Row[values=" + ArrayUtils.toString(_values) + "]";
}
@Override
public int hashCode() {
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(_items);
hcb.append(_values);
return hcb.toHashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Row) {
Row that = (Row) obj;
EqualsBuilder eb = new EqualsBuilder();
eb.append(this._values.length, that._values.length);
Comparator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy