1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.datastructures.map;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.io.Serializable;
19 import java.util.Arrays;
22 * n-tuple (all fields are set) or n-tuple query (some fields possibly null)
24 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
26 public class Tuple implements Serializable, Externalizable {
34 public Tuple(Object ... fields)
37 hashCode = Arrays.hashCode(fields);
39 for (Object o : fields) {
40 if (o!=null) associativity |= mask;
46 public int hashCode() {
51 public boolean equals(Object obj) {
52 if (obj == null) return false;
53 if (!(obj.getClass().equals(this.getClass()))) return false;
54 Tuple other = (Tuple) obj;
55 return Arrays.deepEquals(fields, other.fields);
59 public String toString() {
60 return Arrays.toString(fields);
63 public Object getField(int index)
68 public Object tryGetField(int index)
70 return index >= 0 && index < fields.length ? fields[index] : null;
73 @SuppressWarnings("unchecked")
74 public <T> T getTypedField(int index)
76 return (T) fields[index];
79 @SuppressWarnings("unchecked")
80 public <T> T tryGetTypedField(int index)
82 return index >= 0 && index < fields.length ? (T) fields[index] : null;
85 public Object[] getFields()
95 public int getAssociativity()
101 * Tuple is full if all fields are set (not null)
102 * @return <code>true</code> if all fields are set
104 public boolean isFull()
106 return associativity == (1 << fields.length)-1;
110 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
111 int count = in.readInt();
112 fields = new Object[count];
113 for (int i=0; i<count; i++)
114 fields[i] = in.readObject();
115 hashCode = Arrays.hashCode(fields);
119 public void writeExternal(ObjectOutput out) throws IOException {
120 out.writeInt(fields.length);
121 for (Object o : fields)