X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2Fmap%2FTuple.java;fp=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2Fmap%2FTuple.java;h=a57a0a813d258c3c7bed1b35a923af09bbc6d353;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/map/Tuple.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/map/Tuple.java new file mode 100644 index 000000000..a57a0a813 --- /dev/null +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/map/Tuple.java @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.datastructures.map; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.Serializable; +import java.util.Arrays; + +/** + * n-tuple (all fields are set) or n-tuple query (some fields possibly null) + * + * @author Toni Kalajainen + */ +public class Tuple implements Serializable, Externalizable { + + Object[] fields; + private int hashCode; + int associativity; + + Tuple() {} + + public Tuple(Object ... fields) + { + this.fields = fields; + hashCode = Arrays.hashCode(fields); + int mask = 1; + for (Object o : fields) { + if (o!=null) associativity |= mask; + mask <<= 1; + } + } + + @Override + public int hashCode() { + return hashCode; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (!(obj.getClass().equals(this.getClass()))) return false; + Tuple other = (Tuple) obj; + return Arrays.deepEquals(fields, other.fields); + } + + @Override + public String toString() { + return Arrays.toString(fields); + } + + public Object getField(int index) + { + return fields[index]; + } + + public Object tryGetField(int index) + { + return index >= 0 && index < fields.length ? fields[index] : null; + } + + @SuppressWarnings("unchecked") + public T getTypedField(int index) + { + return (T) fields[index]; + } + + @SuppressWarnings("unchecked") + public T tryGetTypedField(int index) + { + return index >= 0 && index < fields.length ? (T) fields[index] : null; + } + + public Object[] getFields() + { + return fields; + } + + public int getLevel() + { + return fields.length; + } + + public int getAssociativity() + { + return associativity; + } + + /** + * Tuple is full if all fields are set (not null) + * @return true if all fields are set + */ + public boolean isFull() + { + return associativity == (1 << fields.length)-1; + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + int count = in.readInt(); + fields = new Object[count]; + for (int i=0; i