View Javadoc

1   package org.xvsm.core;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   /***
8    * A tuple can be used to structure multiple {@link Entry}s and write them as
9    * one unit into a container.
10   * 
11   * @author Christian Schreiber, Michael Proestler
12   * 
13   */
14  public class Tuple extends Entry implements Iterable<Entry> {
15  	/***
16  	 * Generated serial version UID.
17  	 */
18  	private static final long serialVersionUID = -7973426254054471248L;
19  
20  	/***
21  	 * This {@link List} contains the tuple.
22  	 */
23  	private List<Entry> entries;
24  
25  	/***
26  	 * Default constructor.
27  	 */
28  	public Tuple() {
29  		super();
30  		this.setEntryType(Entry.EntryTypes.TUPLE);
31  		this.entries = new ArrayList<Entry>();
32  	}
33  
34  	/***
35  	 * Default constructor.
36  	 */
37  	public Tuple(int size) {
38  		this();
39  		for (int i = 0; i < size; i++) {
40  			this.add((Entry) null);
41  		}
42  	}
43  
44  	/***
45  	 * Creates a new Tuple with the given {@link Entry}s.
46  	 * 
47  	 * @param ent
48  	 *            the entries for the tuple.
49  	 */
50  	public Tuple(Entry... ent) {
51  		this();
52  		this.add(ent);
53  	}
54  
55  	/***
56  	 * Adds an {@link Entry} to the end of the Tuple the tuple
57  	 * (1,2,3).addEntry(4) would become (1,2,3,4).
58  	 * 
59  	 * @param entry
60  	 *            the {@link Entry} to be added.
61  	 */
62  	public void add(Entry entry) {
63  		this.entries.add(entry);
64  	}
65  
66  	/***
67  	 * Adds the given {@link Entry}s to this tuple.
68  	 * 
69  	 * @param ent
70  	 *            the {@link Entry}s which shall be added.
71  	 */
72  	public void add(Entry... ent) {
73  		for (Entry e : ent) {
74  			this.add(e);
75  		}
76  	}
77  
78  	/***
79  	 * Sets the {@link Entry} at the given position of the tuple.
80  	 * 
81  	 * @param position
82  	 *            the position for the new Entry.
83  	 * @param entry
84  	 *            the {@link Entry} to be added.
85  	 */
86  	public void setEntry(int position, Entry entry) {
87  		while (this.entries.size() <= position) {
88  			this.add((Entry) null);
89  		}
90  		this.entries.set(position, entry);
91  	}
92  
93  	/***
94  	 * Returns the {@link Entry} at the given position in the tuple.
95  	 * 
96  	 * @param position
97  	 *            the position for the Entry.
98  	 * @return the Entry at the given position.
99  	 * 
100 	 * @throws IndexOutOfBoundsException
101 	 *             if the position is out of range (index < 0 || index >=
102 	 *             size()).
103 	 */
104 	public Entry getEntryAt(int position) {
105 		return this.entries.get(position);
106 	}
107 
108 	/***
109 	 * Returns the size of this Tuple.
110 	 * 
111 	 * @return the size of this Tuple.
112 	 */
113 	public int size() {
114 		return this.entries.size();
115 	}
116 
117 	/***
118 	 * Returns an entry array representing this tuple.
119 	 * 
120 	 * @return a entry array representing this tuple.
121 	 */
122 	public Entry[] toArray() {
123 		return this.entries.toArray(new Entry[0]);
124 	}
125 
126 	/***
127 	 * {@inheritDoc}.
128 	 */
129 	@Override
130 	public String toString() {
131 		return this.entries.toString() + ", " + super.toString();
132 	}
133 
134 	/***
135 	 * {@inheritDoc}.
136 	 */
137 	@Override
138 	public int hashCode() {
139 		final int prime = 31;
140 		int result = super.hashCode();
141 		result = prime * result + ((entries == null) ? 0 : entries.hashCode());
142 		return result;
143 	}
144 
145 	/***
146 	 * {@inheritDoc}
147 	 */
148 	// @Override
149 	public Iterator<Entry> iterator() {
150 		return this.entries.iterator();
151 	}
152 
153 	/***
154 	 * {@inheritDoc}
155 	 */
156 	@Override
157 	public Tuple getFlatCopy() {
158 		return new Tuple(this.entries.toArray(new Entry[0]));
159 	}
160 
161 	/***
162 	 * {@inheritDoc}
163 	 */
164 	@Override
165 	public boolean equals(Object o) {
166 		// super just checks if the ids are equal ... if
167 		// this is the case we have equal entries.
168 		return super.equals(o)
169 				|| (o instanceof Tuple
170 						&& ((Tuple) o).entries.size() == this.entries.size() && ((Tuple) o).entries
171 						.equals(this.entries));
172 	}
173 }