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 org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
18 * <b><u>ArrayDefinition</u></b>
20 public class ArrayDefinition
extends Definition
{
22 // ------------------------------------------------------------------------
24 // ------------------------------------------------------------------------
26 private final ArrayDeclaration declaration
;
27 private Definition definitions
[];
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
35 * @param declaration the parent declaration
36 * @param definitionScope the parent scope
37 * @param fieldName the field name
39 public ArrayDefinition(ArrayDeclaration declaration
,
40 IDefinitionScope definitionScope
, String fieldName
) {
41 super(definitionScope
, fieldName
);
43 this.declaration
= declaration
;
45 definitions
= new Definition
[declaration
.getLength()];
47 for (int i
= 0; i
< declaration
.getLength(); i
++) {
48 definitions
[i
] = declaration
.getElementType().createDefinition(
49 definitionScope
, fieldName
+ "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
53 // ------------------------------------------------------------------------
54 // Getters/Setters/Predicates
55 // ------------------------------------------------------------------------
58 * @return the definitions
60 public Definition
[] getDefinitions() {
66 * the definitions to set
68 public void setDefinitions(Definition
[] definitions
) {
69 this.definitions
= definitions
;
73 * Get the element at i
74 * @param i the index (cannot be negative)
75 * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception
77 public Definition
getElem(int i
) {
78 if (i
> definitions
.length
) {
82 return definitions
[i
];
86 public ArrayDeclaration
getDeclaration() {
91 * Sometimes, strings are encoded as an array of 1-byte integers (each one
92 * being an UTF-8 byte).
94 * @return true if this array is in fact an UTF-8 string. false if it's a
95 * "normal" array of generic Definition's.
97 public boolean isString() {
98 IntegerDeclaration elemInt
;
100 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
102 * If the first byte is a "character", we'll consider the whole
103 * array a character string.
105 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
106 if (elemInt
.isCharacter()) {
113 // ------------------------------------------------------------------------
115 // ------------------------------------------------------------------------
118 public void read(BitBuffer input
) {
119 for (Definition definition
: definitions
) {
120 definition
.read(input
);
125 public String
toString() {
126 StringBuilder b
= new StringBuilder();
128 if (this.isString()) {
129 for (Definition def
: definitions
) {
130 IntegerDefinition character
= (IntegerDefinition
) def
;
132 if (character
.getValue() == 0) {
136 b
.append(character
.toString());
138 } else if (definitions
== null) {
139 b
.append("[ ]"); //$NON-NLS-1$
142 for (int i
= 0; i
< (definitions
.length
- 1); i
++) {
144 b
.append(definitions
[i
].toString());
148 b
.append(definitions
[definitions
.length
- 1].toString());
149 b
.append(" ]"); //$NON-NLS-1$