
dk.eobjects.metamodel.data.Row Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of MetaModel Show documentation
Show all versions of MetaModel Show documentation
The eobjects.dk MetaModel is a common domain model, query-engine
and optimizer for different kinds of datastores.
The newest version!
/*
* Copyright 2008 eobjects.dk
*
* 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 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
*/
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
* @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