/*******************************************************************************
- * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
package org.eclipse.linuxtools.ctf.core.event.types;
-import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
/**
- * <b><u>ArrayDefinition</u></b>
+ * A CTF array definition
+ *
+ * Arrays are fixed-length. Their length is declared in the type declaration
+ * within the meta-data. They contain an array of "inner type" elements, which
+ * can refer to any type not containing the type of the array being declared (no
+ * circular dependency). The length is the number of elements in an array.
+ *
+ * @deprecated use {@link AbstractArrayDefinition}
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Simon Marchi
*/
-public class ArrayDefinition extends Definition {
+@NonNullByDefault
+@Deprecated
+public final class ArrayDefinition extends AbstractArrayDefinition{
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
- private final ArrayDeclaration declaration;
- private Definition definitions[];
+ private final ImmutableList<Definition> fDefinitions;
// ------------------------------------------------------------------------
// Constructors
/**
* Constructor
- * @param declaration the parent declaration
- * @param definitionScope the parent scope
- * @param fieldName the field name
+ *
+ * @param declaration
+ * the parent declaration
+ * @param definitionScope
+ * the parent scope
+ * @param fieldName
+ * the field name
+ * @param definitions
+ * the content of the array
+ * @since 3.0
*/
public ArrayDefinition(ArrayDeclaration declaration,
- IDefinitionScope definitionScope, String fieldName) {
- super(definitionScope, fieldName);
-
- this.declaration = declaration;
-
- definitions = new Definition[declaration.getLength()];
+ @Nullable IDefinitionScope definitionScope,
+ String fieldName,
+ List<Definition> definitions) {
+ super(declaration, definitionScope, fieldName);
+ @SuppressWarnings("null")
+ @NonNull ImmutableList<Definition> list = ImmutableList.copyOf(definitions);
+ fDefinitions = list;
- for (int i = 0; i < declaration.getLength(); i++) {
- definitions[i] = declaration.getElementType().createDefinition(
- definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
- }
}
// ------------------------------------------------------------------------
// Getters/Setters/Predicates
// ------------------------------------------------------------------------
- /**
- * @return the definitions
- */
- public Definition[] getDefinitions() {
- return definitions;
- }
-
- /**
- * @param definitions
- * the definitions to set
- */
- public void setDefinitions(Definition[] definitions) {
- this.definitions = definitions;
+ @Override
+ public List<Definition> getDefinitions() {
+ return fDefinitions;
}
/**
* Get the element at i
+ *
* @param i the index (cannot be negative)
- * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception
+ * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception
*/
+ @Nullable
public Definition getElem(int i) {
- if (i > definitions.length) {
+ if (i > fDefinitions.size()) {
return null;
}
- return definitions[i];
+ return fDefinitions.get(i);
}
@Override
public ArrayDeclaration getDeclaration() {
- return declaration;
- }
-
- /**
- * Sometimes, strings are encoded as an array of 1-byte integers (each one
- * being an UTF-8 byte).
- *
- * @return true if this array is in fact an UTF-8 string. false if it's a
- * "normal" array of generic Definition's.
- */
- public boolean isString() {
- IntegerDeclaration elemInt;
-
- if (declaration.getElementType() instanceof IntegerDeclaration) {
- /*
- * If the first byte is a "character", we'll consider the whole
- * array a character string.
- */
- elemInt = (IntegerDeclaration) declaration.getElementType();
- if (elemInt.isCharacter()) {
- return true;
- }
- }
- return false;
+ return (ArrayDeclaration) super.getDeclaration();
}
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
- @Override
- public void read(BitBuffer input) {
- for (Definition definition : definitions) {
- definition.read(input);
- }
- }
-
@Override
public String toString() {
StringBuilder b = new StringBuilder();
- if (this.isString()) {
- for (Definition def : definitions) {
+ if (getDeclaration().isString()) {
+ for (Definition def : fDefinitions) {
IntegerDefinition character = (IntegerDefinition) def;
if (character.getValue() == 0) {
b.append(character.toString());
}
- } else if (definitions == null) {
- b.append("[ ]"); //$NON-NLS-1$
} else {
b.append('[');
- for (int i = 0; i < (definitions.length - 1); i++) {
- b.append(' ');
- b.append(definitions[i].toString());
- b.append(',');
- }
- b.append(' ');
- b.append(definitions[definitions.length - 1].toString());
- b.append(" ]"); //$NON-NLS-1$
+ Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
+ b.append(joiner.join(fDefinitions));
+ b.append(']');
}
- return b.toString();
+ @SuppressWarnings("null")
+ @NonNull String ret = b.toString();
+ return ret;
}
}