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
.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 // ------------------------------------------------------------------------
33 public ArrayDefinition(ArrayDeclaration declaration
,
34 IDefinitionScope definitionScope
, String fieldName
) {
35 super(definitionScope
, fieldName
);
37 this.declaration
= declaration
;
39 definitions
= new Definition
[declaration
.getLength()];
41 for (int i
= 0; i
< declaration
.getLength(); i
++) {
42 definitions
[i
] = declaration
.getElementType().createDefinition(
43 definitionScope
, fieldName
+ "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
47 // ------------------------------------------------------------------------
48 // Getters/Setters/Predicates
49 // ------------------------------------------------------------------------
52 * @return the definitions
54 public Definition
[] getDefinitions() {
60 * the definitions to set
62 public void setDefinitions(Definition
[] definitions
) {
63 this.definitions
= definitions
;
66 public Definition
getElem(int i
) {
67 if (i
> definitions
.length
) {
71 return definitions
[i
];
74 public ArrayDeclaration
getDeclaration() {
79 * Sometimes, strings are encoded as an array of 1-byte integers (each one
80 * being an UTF-8 byte).
82 * @return true if this array is in fact an UTF-8 string. false if it's a
83 * "normal" array of generic Definition's.
85 public boolean isString() {
86 IntegerDeclaration elemInt
;
88 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
90 * If the first byte is a "character", we'll consider the whole
91 * array a character string.
93 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
94 if (elemInt
.isCharacter()) {
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 public void read(BitBuffer input
) {
107 for (Definition definition
: definitions
) {
108 definition
.read(input
);
113 public String
toString() {
114 StringBuilder b
= new StringBuilder();
116 if (this.isString()) {
117 for (Definition def
: definitions
) {
118 IntegerDefinition character
= (IntegerDefinition
) def
;
120 if (character
.getValue() == 0) {
124 b
.append(character
.toString());
126 } else if (definitions
== null) {
127 b
.append("[ ]"); //$NON-NLS-1$
130 for (int i
= 0; i
< (definitions
.length
- 1); i
++) {
132 b
.append(definitions
[i
].toString());
136 b
.append(definitions
[definitions
.length
- 1].toString());
137 b
.append(" ]"); //$NON-NLS-1$