tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / ArrayDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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.Arrays;
16
17 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
18 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
19
20 /**
21 * A CTF array definition
22 *
23 * Arrays are fixed-length. Their length is declared in the type
24 * declaration within the meta-data. They contain an array of "inner type"
25 * elements, which can refer to any type not containing the type of the
26 * array being declared (no circular dependency). The length is the number
27 * of elements in an array.
28 *
29 * @version 1.0
30 * @author Matthew Khouzam
31 * @author Simon Marchi
32 */
33 public class ArrayDefinition extends Definition {
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 private final ArrayDeclaration declaration;
40 private Definition definitions[];
41
42 // ------------------------------------------------------------------------
43 // Constructors
44 // ------------------------------------------------------------------------
45
46 /**
47 * Constructor
48 * @param declaration the parent declaration
49 * @param definitionScope the parent scope
50 * @param fieldName the field name
51 */
52 public ArrayDefinition(ArrayDeclaration declaration,
53 IDefinitionScope definitionScope, String fieldName) {
54 super(definitionScope, fieldName);
55
56 this.declaration = declaration;
57
58 definitions = new Definition[declaration.getLength()];
59
60 for (int i = 0; i < declaration.getLength(); i++) {
61 definitions[i] = declaration.getElementType().createDefinition(
62 definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
63 }
64 }
65
66 // ------------------------------------------------------------------------
67 // Getters/Setters/Predicates
68 // ------------------------------------------------------------------------
69
70 /**
71 * @return the definitions
72 */
73 public Definition[] getDefinitions() {
74 return Arrays.copyOf(definitions, definitions.length);
75 }
76
77 /**
78 * @param definitions
79 * the definitions to set
80 */
81 public void setDefinitions(Definition[] definitions) {
82 this.definitions = Arrays.copyOf(definitions, definitions.length);
83 }
84
85 /**
86 * Get the element at i
87 * @param i the index (cannot be negative)
88 * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception
89 */
90 public Definition getElem(int i) {
91 if (i > definitions.length) {
92 return null;
93 }
94
95 return definitions[i];
96 }
97
98 @Override
99 public ArrayDeclaration getDeclaration() {
100 return declaration;
101 }
102
103 /**
104 * Sometimes, strings are encoded as an array of 1-byte integers (each one
105 * being an UTF-8 byte).
106 *
107 * @return true if this array is in fact an UTF-8 string. false if it's a
108 * "normal" array of generic Definition's.
109 */
110 public boolean isString() {
111 IntegerDeclaration elemInt;
112
113 if (declaration.getElementType() instanceof IntegerDeclaration) {
114 /*
115 * If the first byte is a "character", we'll consider the whole
116 * array a character string.
117 */
118 elemInt = (IntegerDeclaration) declaration.getElementType();
119 if (elemInt.isCharacter()) {
120 return true;
121 }
122 }
123 return false;
124 }
125
126 // ------------------------------------------------------------------------
127 // Operations
128 // ------------------------------------------------------------------------
129
130 @Override
131 public void read(BitBuffer input) throws CTFReaderException {
132 for (Definition definition : definitions) {
133 definition.read(input);
134 }
135 }
136
137 @Override
138 public String toString() {
139 StringBuilder b = new StringBuilder();
140
141 if (this.isString()) {
142 for (Definition def : definitions) {
143 IntegerDefinition character = (IntegerDefinition) def;
144
145 if (character.getValue() == 0) {
146 break;
147 }
148
149 b.append(character.toString());
150 }
151 } else if (definitions == null) {
152 b.append("[ ]"); //$NON-NLS-1$
153 } else {
154 b.append('[');
155 for (int i = 0; i < (definitions.length - 1); i++) {
156 b.append(' ');
157 b.append(definitions[i].toString());
158 b.append(',');
159 }
160 b.append(' ');
161 b.append(definitions[definitions.length - 1].toString());
162 b.append(" ]"); //$NON-NLS-1$
163 }
164
165 return b.toString();
166 }
167 }
This page took 0.035306 seconds and 6 git commands to generate.