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

dk.eobjects.metamodel.data.Row Maven / Gradle / Ivy

Go to download

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 comparator = ObjectComparator.getComparator();
			for (int i = 0; i < _values.length; i++) {
				eb.appendSuper(0 == comparator.compare(this._values[i],
						that._values[i]));
			}
			eb.append(this._items, that._items);
			return eb.isEquals();
		}
		return false;
	}

	@Override
	protected Row clone() {
		return new Row(_items, _values);
	}

	/**
	 * Creates a row similar to this one but only with a subset of the select
	 * items
	 * 
	 * @param selectItems
	 * @return
	 */
	public Row getSubSelection(SelectItem[] selectItems) {
		Object[] values = new Object[selectItems.length];
		for (int i = 0; i < selectItems.length; i++) {
			SelectItem selectItem = selectItems[i];
			values[i] = getValue(selectItem);
		}
		return new Row(selectItems, values);
	}
}