Repository /Debellor/debellor-1.0.jar:org.debellor.core.data.DataVector


Back

No file description

Source code

/*
 *  Debellor
 *
 *  Copyright (C) 2008-2009 by Marcin Wojnarski
 *
 *  This program 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.
 *
 *  This program 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 this program.  If not, see http://www.gnu.org/licenses/.
 */
package org.debellor.core.data;

import java.util.Arrays;

import org.debellor.core.DataObject;
import org.debellor.core.DataType;

/**
 * Represents vectors of data items of different types.
 * Vectors can have fixed or variable length.
 * 
 * @author Marcin Wojnarski
 *
 */
public final class DataVector extends DataObject {

	/** 
	 * dataType of {@code DataVector}. 
	 * Contains information about number and types of items in the vector. 
	 */
	public static final class DataVectorType extends DataType {

		/** dataType of all items. */
		private final DataType[] itemTypes;
		
		/** Constructs a DataVectorType with a copy of the passed array (or variable-length list)
		 * as an array of types of this vector's items. */
		public DataVectorType(DataType... itemTypes) {
			super(DataVector.class);
			this.itemTypes = itemTypes.clone();
		}
		/** Constructs a DataVectorType for vectors of constant <code>length</code>
		 * containing items of the same <code>type</code>. */
		public DataVectorType(int length, DataType type) {
			super(DataVector.class);
			this.itemTypes = new DataType[length];
			for(int i = 0; i < length; i++)
				this.itemTypes[i] = type;
		}
		
		public int size() {
			return itemTypes.length;
		}
		public DataType get(int i) {
			return itemTypes[i];
		}
		
		/** Returns a shallow <i>copy</i> of the array of item types. */
		public DataType[] get() {
			return itemTypes.clone();
		}

		@Override
		public String toString() {
			return Arrays.toString(itemTypes);
		}
	}

	
	private final DataObject[] items;
	
	
	/** Constructs a new DataVector containing the items passed
	 * as an array or a variable-length list of DataObjects. */
	public DataVector(DataObject... items) {
		this.items = items.clone();
	}

	public int size() {
		return items.length;
	}
	public boolean isMissing(int i) {
		return items[i] == null;
	}
	public DataObject get(int i) {
		return items[i];
	}

	/** Returns a shallow copy> of the array of items of this DataVector.
	 * Since this method allocates a new array,
	 * if only read access to items is needed and the array of items
	 * is not going to be modified, it is recommended to use
	 * {@link #get(int)} method instead, which does not allocate new resources.
	 * Here, the existing array could not be returned, as this would violate
	 * immutability of data objects (the returned array could be modified
	 * afterwards, which would indirectly modify this DataVector object). */
	public DataObject[] get() {
		return items.clone();
	}
	
	@Override
	public String toString() {
		if(items.length == 0) return "[empty DataVector]";
		String s = "" + toString(0);
		for(int i = 1; i < items.length; i++)
			s += " " + toString(i);
		return "[" + s + "]";
	}
		
	private String toString(int i) {
		return (items[i] == null ? "missing" : items[i].toString());
	}

	@Override
	public boolean equals(Object obj) {
		if(this == obj) return true;
		if((obj == null) || !(obj instanceof DataVector)) return false;
		DataVector v = (DataVector) obj;
		if(items == v.items) return true;
		if(items.length != v.items.length) return false;
		for(int i = 0; i < items.length; i++) {
			DataObject a = items[i];
			DataObject b = v.items[i];
			if((a != b) && ((a == null) || !a.equals(b))) return false;
		}
		return true;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		for(int i = 0; i < items.length; i++) {
			DataObject d = items[i];
			hash ^= (d == null) ? (1 << i) : d.hashCode();
		}
		return hash;
	}

}

Copyright © 2008-2011 by TunedIT
Design by luksite