68e0ff7a6c6cedca090356b1b0a4c4d50d0a0085
[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 * @deprecated use {@link org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration}
33 * @version 1.0
34 * @author Matthew Khouzam
35 * @author Simon Marchi
36 */
37 @Deprecated
38 public class SequenceDeclaration extends CompoundDeclaration {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
44 private final IDeclaration fElemType;
45 private final String fLengthName;
46 private final Multimap<String, String> fPaths = ArrayListMultimap.<String, String>create();
47
48 // ------------------------------------------------------------------------
49 // Constructors
50 // ------------------------------------------------------------------------
51
52 /**
53 * Constructor
54 *
55 * @param lengthName
56 * the name of the field describing the length
57 * @param elemType
58 * The element type
59 */
60 public SequenceDeclaration(String lengthName, IDeclaration elemType) {
61 fElemType = elemType;
62 fLengthName = lengthName;
63 }
64
65 // ------------------------------------------------------------------------
66 // Getters/Setters/Predicates
67 // ------------------------------------------------------------------------
68
69 @Override
70 public IDeclaration getElementType() {
71 return fElemType;
72 }
73
74 /**
75 * Gets the name of the length field
76 *
77 * @return the name of the length field
78 */
79 public String getLengthName() {
80 return fLengthName;
81 }
82
83 // ------------------------------------------------------------------------
84 // Operations
85 // ------------------------------------------------------------------------
86
87 /**
88 * @since 3.0
89 */
90 @SuppressWarnings("null") // immutablelist
91 @Override
92 public SequenceDefinition createDefinition(
93 IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException {
94 IDefinition lenDef = null;
95
96 if (definitionScope != null) {
97 lenDef = definitionScope.lookupDefinition(getLengthName());
98 }
99
100 if (lenDef == null) {
101 throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
102 }
103
104 if (!(lenDef instanceof IntegerDefinition)) {
105 throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
106 }
107
108 IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef;
109
110 if (lengthDefinition.getDeclaration().isSigned()) {
111 throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
112 }
113
114 long length = lengthDefinition.getValue();
115 if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) {
116 throw new CTFReaderException("Sequence length too long " + length); //$NON-NLS-1$
117 }
118
119 Collection<String> collection = fPaths.get(fieldName);
120 while (collection.size() < length) {
121 fPaths.put(fieldName, fieldName + '[' + collection.size() + ']');
122 }
123 List<String> paths = (List<String>) fPaths.get(fieldName);
124 Builder<Definition> definitions = new ImmutableList.Builder<>();
125 for (int i = 0; i < length; i++) {
126 definitions.add(fElemType.createDefinition(definitionScope, paths.get(i), input));
127 }
128 return new SequenceDefinition(this, definitionScope, fieldName, definitions.build());
129 }
130
131 @Override
132 public String toString() {
133 /* Only used for debugging */
134 return "[declaration] sequence[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
135 }
136
137 /**
138 * @since 3.0
139 */
140 @Override
141 public int getMaximumSize() {
142 return Integer.MAX_VALUE;
143 }
144
145 }
This page took 0.042168 seconds and 4 git commands to generate.