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