ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / SequenceDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.event.types;
14
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
19 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
20 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21
22 import com.google.common.collect.ArrayListMultimap;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableList.Builder;
25 import com.google.common.collect.Multimap;
26
27 /**
28 * A CTF sequence declaration.
29 *
30 * An array where the size is fixed but declared in the trace, unlike array
31 * where it is declared with a literal
32 *
33 * @version 1.0
34 * @author Matthew Khouzam
35 * @author Simon Marchi
36 */
37 public class SequenceDeclaration extends Declaration {
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 private final IDeclaration fElemType;
44 private final String fLengthName;
45 private final Multimap<String, String> fPaths = ArrayListMultimap.<String, String>create();
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
51 /**
52 * Constructor
53 *
54 * @param lengthName
55 * the name of the field describing the length
56 * @param elemType
57 * The element type
58 */
59 public SequenceDeclaration(String lengthName, IDeclaration elemType) {
60 fElemType = elemType;
61 fLengthName = lengthName;
62 }
63
64 // ------------------------------------------------------------------------
65 // Getters/Setters/Predicates
66 // ------------------------------------------------------------------------
67
68 /**
69 * Gets the element type
70 *
71 * @return the element type
72 */
73 public IDeclaration getElementType() {
74 return fElemType;
75 }
76
77 /**
78 * Gets the name of the length field
79 *
80 * @return the name of the length field
81 */
82 public String getLengthName() {
83 return fLengthName;
84 }
85
86 @Override
87 public long getAlignment() {
88 return getElementType().getAlignment();
89 }
90
91
92 // ------------------------------------------------------------------------
93 // Operations
94 // ------------------------------------------------------------------------
95
96 /**
97 * Is the Sequence a string?
98 * @return true, if the elements are chars, false otherwise
99 * @since 3.0
100 */
101 public boolean isString(){
102 IntegerDeclaration elemInt;
103 IDeclaration elementType = getElementType();
104 if (elementType instanceof IntegerDeclaration) {
105 elemInt = (IntegerDeclaration) elementType;
106 if (elemInt.isCharacter()) {
107 return true;
108 }
109 }
110 return false;
111 }
112
113 /**
114 * @since 3.0
115 */
116 @SuppressWarnings("null") // immutablelist
117 @Override
118 public SequenceDefinition createDefinition(
119 IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException {
120 Definition lenDef = null;
121
122 if (definitionScope != null) {
123 lenDef = definitionScope.lookupDefinition(getLengthName());
124 }
125
126 if (lenDef == null) {
127 throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
128 }
129
130 if (!(lenDef instanceof IntegerDefinition)) {
131 throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
132 }
133
134 IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef;
135
136 if (lengthDefinition.getDeclaration().isSigned()) {
137 throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
138 }
139
140 long length = lengthDefinition.getValue();
141 if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) {
142 throw new CTFReaderException("Sequence length too long " + length); //$NON-NLS-1$
143 }
144
145 Collection<String> collection = fPaths.get(fieldName);
146 while (collection.size() < length) {
147 fPaths.put(fieldName, fieldName + '[' + collection.size() + ']');
148 }
149 List<String> paths = (List<String>) fPaths.get(fieldName);
150 Builder<Definition> definitions = new ImmutableList.Builder<>();
151 for (int i = 0; i < length; i++) {
152 definitions.add(fElemType.createDefinition(definitionScope, paths.get(i), input));
153 }
154 return new SequenceDefinition(this, definitionScope, fieldName, definitions.build());
155 }
156
157 @Override
158 public String toString() {
159 /* Only used for debugging */
160 return "[declaration] sequence[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
161 }
162
163 /**
164 * @since 3.0
165 */
166 @Override
167 public int getMaximumSize() {
168 return Integer.MAX_VALUE;
169 }
170
171 }
This page took 0.036734 seconds and 5 git commands to generate.