btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / ArrayDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
a4fa4e36
MK
15import java.util.List;
16
17import org.eclipse.jdt.annotation.NonNull;
18import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
19import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
20import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21
22import com.google.common.collect.ArrayListMultimap;
23import com.google.common.collect.ImmutableList;
24import com.google.common.collect.ImmutableList.Builder;
25import com.google.common.collect.Multimap;
26
866e5b51 27/**
d37aaa7f
FC
28 * A CTF array declaration
29 *
9639bf3a
MK
30 * Arrays are fixed-length. Their length is declared in the type declaration
31 * within the meta-data. They contain an array of "inner type" elements, which
32 * can refer to any type not containing the type of the array being declared (no
33 * circular dependency). The length is the number of elements in an array.
d37aaa7f 34 *
9377d173
MK
35 * @deprecated use
36 * {@link org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration}
d37aaa7f
FC
37 * @version 1.0
38 * @author Matthew Khouzam
39 * @author Simon Marchi
866e5b51 40 */
7b4f13e6 41@Deprecated
9377d173 42public class ArrayDeclaration extends CompoundDeclaration {
866e5b51
FC
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47
a4fa4e36
MK
48 private final int fLength;
49 private final IDeclaration fElemType;
50
51 /**
52 * <pre>
53 * Cache where we can pre-generate the children names
54 * Key&colon; parent name
55 * Value&colon; children names
56 * ex: field &#8594; &lbrace;field&lbrack;0&rbrack;, field&lbrack;1&rbrack;, &hellip; field&lbrack;n&rbrack;&rbrace;
57 * </pre>
58 *
59 * TODO: investigate performance
60 */
61 private final Multimap<String, String> fChildrenNames = ArrayListMultimap.<String, String> create();
866e5b51
FC
62
63 // ------------------------------------------------------------------------
64 // Constructors
65 // ------------------------------------------------------------------------
66
9ac2eb62
MK
67 /**
68 * Constructor
9639bf3a
MK
69 *
70 * @param length
71 * how many elements in the array
72 * @param elemType
73 * what type of element is in the array
9ac2eb62 74 */
866e5b51 75 public ArrayDeclaration(int length, IDeclaration elemType) {
a4fa4e36
MK
76 fLength = length;
77 fElemType = elemType;
866e5b51
FC
78 }
79
80 // ------------------------------------------------------------------------
81 // Getters/Setters/Predicates
82 // ------------------------------------------------------------------------
83
9377d173 84 @Override
866e5b51 85 public IDeclaration getElementType() {
a4fa4e36 86 return fElemType;
866e5b51
FC
87 }
88
9ac2eb62
MK
89 /**
90 *
91 * @return how many elements in the array
92 */
866e5b51 93 public int getLength() {
a4fa4e36 94 return fLength;
866e5b51
FC
95 }
96
97 // ------------------------------------------------------------------------
98 // Operations
99 // ------------------------------------------------------------------------
100
a4fa4e36
MK
101 /**
102 * @since 3.0
103 */
7b4f13e6 104 @Deprecated
866e5b51
FC
105 @Override
106 public ArrayDefinition createDefinition(IDefinitionScope definitionScope,
a4fa4e36
MK
107 @NonNull String fieldName, BitBuffer input) throws CTFReaderException {
108 alignRead(input);
109 List<Definition> definitions = read(input, definitionScope, fieldName);
110 return new ArrayDefinition(this, definitionScope, fieldName, definitions);
866e5b51
FC
111 }
112
113 @Override
114 public String toString() {
115 /* Only used for debugging */
116 return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
117 }
118
a4fa4e36
MK
119 @NonNull
120 private List<Definition> read(@NonNull BitBuffer input, IDefinitionScope definitionScope, String fieldName) throws CTFReaderException {
121 Builder<Definition> definitions = new ImmutableList.Builder<>();
122 if (!fChildrenNames.containsKey(fieldName)) {
123 for (int i = 0; i < fLength; i++) {
124 fChildrenNames.put(fieldName, fieldName + '[' + i + ']');
125 }
126 }
127 List<String> elemNames = (List<String>) fChildrenNames.get(fieldName);
128 for (int i = 0; i < fLength; i++) {
129 String name = elemNames.get(i);
130 if (name == null) {
131 throw new IllegalStateException();
132 }
133 definitions.add(fElemType.createDefinition(definitionScope, name, input));
134 }
135 @SuppressWarnings("null")
136 @NonNull ImmutableList<Definition> ret = definitions.build();
137 return ret;
138 }
139
140 /**
141 * @since 3.0
142 */
143 @Override
144 public int getMaximumSize() {
145 long val = (long) fLength * fElemType.getMaximumSize();
146 return (int) Math.min(Integer.MAX_VALUE, val);
147 }
148
866e5b51 149}
This page took 0.087367 seconds and 5 git commands to generate.