X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=org.eclipse.linuxtools.ctf.core%2Fsrc%2Forg%2Feclipse%2Flinuxtools%2Fctf%2Fcore%2Fevent%2Ftypes%2FArrayDefinition.java;h=695de3cddd8c25866e37db09d8be183cd6460900;hb=a4fa4e3606484778f4ea4ba3e42944c4c78430b2;hp=410be08a38f57460e1a413cfc30f549bca9eb728;hpb=60ae41e177bc783d3a400742bb4728b94dcf5c97;p=deliverable%2Ftracecompass.git diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java index 410be08a38..695de3cddd 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java @@ -12,32 +12,36 @@ package org.eclipse.linuxtools.ctf.core.event.types; -import java.util.Arrays; +import java.util.List; -import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; -import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +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; /** * 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. + * 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. * * @version 1.0 * @author Matthew Khouzam * @author Simon Marchi */ -public class ArrayDefinition extends Definition { +@NonNullByDefault +public final class ArrayDefinition extends Definition { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final ArrayDeclaration declaration; - private Definition definitions[]; + private final ImmutableList fDefinitions; // ------------------------------------------------------------------------ // Constructors @@ -45,22 +49,26 @@ public class ArrayDefinition extends Definition { /** * 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); + @Nullable IDefinitionScope definitionScope, + String fieldName, + List definitions) { + super(declaration, definitionScope, fieldName); + @SuppressWarnings("null") + @NonNull ImmutableList list = ImmutableList.copyOf(definitions); + fDefinitions = list; - this.declaration = declaration; - - definitions = new Definition[declaration.getLength()]; - - for (int i = 0; i < declaration.getLength(); i++) { - definitions[i] = declaration.getElementType().createDefinition( - definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ - } } // ------------------------------------------------------------------------ @@ -69,77 +77,42 @@ public class ArrayDefinition extends Definition { /** * @return the definitions + * @since 3.0 */ - public Definition[] getDefinitions() { - return Arrays.copyOf(definitions, definitions.length); - } - - /** - * @param definitions - * the definitions to set - */ - public void setDefinitions(Definition[] definitions) { - this.definitions = Arrays.copyOf(definitions, definitions.length); + public List 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) throws CTFReaderException { - 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) { @@ -148,20 +121,15 @@ public class ArrayDefinition extends Definition { 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; } }