1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.util
.Collection
;
16 import java
.util
.List
;
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
;
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
;
28 * A CTF sequence declaration.
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}
34 * @author Matthew Khouzam
35 * @author Simon Marchi
38 public class SequenceDeclaration
extends CompoundDeclaration
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 private final IDeclaration fElemType
;
45 private final String fLengthName
;
46 private final Multimap
<String
, String
> fPaths
= ArrayListMultimap
.<String
, String
>create();
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
56 * the name of the field describing the length
60 public SequenceDeclaration(String lengthName
, IDeclaration elemType
) {
62 fLengthName
= lengthName
;
65 // ------------------------------------------------------------------------
66 // Getters/Setters/Predicates
67 // ------------------------------------------------------------------------
70 public IDeclaration
getElementType() {
75 * Gets the name of the length field
77 * @return the name of the length field
79 public String
getLengthName() {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
90 @SuppressWarnings("null") // immutablelist
92 public SequenceDefinition
createDefinition(
93 IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFReaderException
{
94 IDefinition lenDef
= null;
96 if (definitionScope
!= null) {
97 lenDef
= definitionScope
.lookupDefinition(getLengthName());
100 if (lenDef
== null) {
101 throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
104 if (!(lenDef
instanceof IntegerDefinition
)) {
105 throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
108 IntegerDefinition lengthDefinition
= (IntegerDefinition
) lenDef
;
110 if (lengthDefinition
.getDeclaration().isSigned()) {
111 throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
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$
119 Collection
<String
> collection
= fPaths
.get(fieldName
);
120 while (collection
.size() < length
) {
121 fPaths
.put(fieldName
, fieldName
+ '[' + collection
.size() + ']');
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
));
128 return new SequenceDefinition(this, definitionScope
, fieldName
, definitions
.build());
132 public String
toString() {
133 /* Only used for debugging */
134 return "[declaration] sequence[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
141 public int getMaximumSize() {
142 return Integer
.MAX_VALUE
;