]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/map/Tuple.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / map / Tuple.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.datastructures.map;
13
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;
20
21 /**
22  * n-tuple (all fields are set) or n-tuple query (some fields possibly null)
23  *
24  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
25  */
26 public class Tuple implements Serializable, Externalizable {
27
28         Object[] fields;
29         private int hashCode;
30         int associativity;
31         
32         Tuple() {}
33         
34         public Tuple(Object ... fields)
35         {
36                 this.fields = fields;
37                 hashCode = Arrays.hashCode(fields);
38                 int mask = 1;
39                 for (Object o : fields) {
40                         if (o!=null) associativity |= mask;
41                         mask <<= 1;
42                 }
43         }
44         
45         @Override
46         public int hashCode() {
47                 return hashCode;
48         }
49         
50         @Override
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);
56         }
57         
58     @Override
59     public String toString() {
60         return Arrays.toString(fields);
61     }
62     
63     public Object getField(int index)
64     {
65         return fields[index];
66     }
67     
68     public Object tryGetField(int index)
69     {
70         return index >= 0 && index < fields.length ? fields[index] : null;
71     }
72     
73     @SuppressWarnings("unchecked")
74     public <T> T getTypedField(int index)
75     {
76         return (T) fields[index];
77     }
78     
79     @SuppressWarnings("unchecked")
80     public <T> T tryGetTypedField(int index)
81     {
82         return index >= 0 && index < fields.length ? (T) fields[index] : null;
83     }
84     
85     public Object[] getFields()
86     {
87         return fields;
88     }
89     
90     public int getLevel()
91     {
92         return fields.length;
93     }
94     
95     public int getAssociativity() 
96     {
97         return associativity;
98     }
99
100         /**
101          * Tuple is full if all fields are set (not null) 
102          * @return <code>true</code> if all fields are set
103          */
104         public boolean isFull()
105         {
106                 return associativity == (1 << fields.length)-1;
107         }
108     
109         @Override
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);
116         }
117
118         @Override
119         public void writeExternal(ObjectOutput out) throws IOException {
120                 out.writeInt(fields.length);
121                 for (Object o : fields)
122                         out.writeObject(o);
123         }
124                 
125 }
126