/* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package migrate.lib.tuple; import java.util.Arrays; import java.util.List; import org.openjena.atlas.lib.ColumnMap; import org.openjena.atlas.lib.Lib; import org.openjena.atlas.lib.NotImplemented; /** Tuples - use less space by specific implementations for * various sizes. Saves the array overhead of 3 slots. * And 3 slots for two items might amount to a lot of space. */ public abstract class ZTuple { static final int NulItemHashCode = 3 ; public abstract T get(int idx) ; // Immutable // public abstract T set(int idx, T item) ; public List asList() { return Arrays.asList(tuple()) ; } public T[] tuple() { int N = size() ; @SuppressWarnings("unchecked") T[] array = (T[])new Object[N] ; for (int i = 0; i < N; i++) array[i] = get(i) ; return array ; } /** Return a tuple with the column mapping applied */ public ZTuple map(ColumnMap colMap) { //return colMap.map(this) ; throw new NotImplemented("Tuple.map") ; } /** Return a tuple with the column mapping reversed */ public ZTuple unmap(ColumnMap colMap) { //return colMap.unmap(this) ; throw new NotImplemented("Tuple.unmap") ; } public abstract int size() ; // Cache? @Override public int hashCode() { int x = 99 ; for ( int i = 0; i < size() ; i++) x ^= get(i).hashCode() ; return x ; } @Override public boolean equals(Object other) { if ( this == other ) return true ; if ( ! ( other instanceof ZTuple ) ) return false ; ZTuple x = (ZTuple)other ; if ( x.size() != this.size() ) return false ; return equalElements(x) ; } protected boolean equalElements(ZTuple tuple) { for ( int i = 0 ; i < tuple.size() ; i++ ) { Object obj1 = get(i) ; Object obj2 = tuple.get(i) ; if ( ! Lib.equal(obj1, obj2) ) return false ; } return true ; } } /* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */