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