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