ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / ArrayDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
13 package org.eclipse.linuxtools.ctf.core.event.types;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
21
22 import com.google.common.base.Joiner;
23 import com.google.common.collect.ImmutableList;
24
25 /**
26 * A CTF array definition
27 *
28 * Arrays are fixed-length. Their length is declared in the type declaration
29 * within the meta-data. They contain an array of "inner type" elements, which
30 * can refer to any type not containing the type of the array being declared (no
31 * circular dependency). The length is the number of elements in an array.
32 *
33 * @version 1.0
34 * @author Matthew Khouzam
35 * @author Simon Marchi
36 */
37 @NonNullByDefault
38 public final class ArrayDefinition extends Definition {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
44 private final ImmutableList<Definition> fDefinitions;
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 /**
51 * Constructor
52 *
53 * @param declaration
54 * the parent declaration
55 * @param definitionScope
56 * the parent scope
57 * @param fieldName
58 * the field name
59 * @param definitions
60 * the content of the array
61 * @since 3.0
62 */
63 public ArrayDefinition(ArrayDeclaration declaration,
64 @Nullable IDefinitionScope definitionScope,
65 String fieldName,
66 List<Definition> definitions) {
67 super(declaration, definitionScope, fieldName);
68 @SuppressWarnings("null")
69 @NonNull ImmutableList<Definition> list = ImmutableList.copyOf(definitions);
70 fDefinitions = list;
71
72 }
73
74 // ------------------------------------------------------------------------
75 // Getters/Setters/Predicates
76 // ------------------------------------------------------------------------
77
78 /**
79 * @return the definitions
80 * @since 3.0
81 */
82 public List<Definition> getDefinitions() {
83 return fDefinitions;
84 }
85
86 /**
87 * Get the element at i
88 *
89 * @param i the index (cannot be negative)
90 * @return The element at I, if I &gt; length, null, if I &lt; 0, the method throws an out of bounds exception
91 */
92 @Nullable
93 public Definition getElem(int i) {
94 if (i > fDefinitions.size()) {
95 return null;
96 }
97
98 return fDefinitions.get(i);
99 }
100
101 @Override
102 public ArrayDeclaration getDeclaration() {
103 return (ArrayDeclaration) super.getDeclaration();
104 }
105
106 // ------------------------------------------------------------------------
107 // Operations
108 // ------------------------------------------------------------------------
109
110 @Override
111 public String toString() {
112 StringBuilder b = new StringBuilder();
113
114 if (getDeclaration().isString()) {
115 for (Definition def : fDefinitions) {
116 IntegerDefinition character = (IntegerDefinition) def;
117
118 if (character.getValue() == 0) {
119 break;
120 }
121
122 b.append(character.toString());
123 }
124 } else {
125 b.append('[');
126 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
127 b.append(joiner.join(fDefinitions));
128 b.append(']');
129 }
130
131 @SuppressWarnings("null")
132 @NonNull String ret = b.toString();
133 return ret;
134 }
135 }
This page took 0.034645 seconds and 5 git commands to generate.