Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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 | ||
a4fa4e36 | 15 | import java.util.List; |
0594c61c | 16 | |
a4fa4e36 MK |
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; | |
866e5b51 FC |
24 | |
25 | /** | |
a511da0d | 26 | * A CTF array definition |
d37aaa7f | 27 | * |
a4fa4e36 MK |
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. | |
d37aaa7f | 32 | * |
7b4f13e6 | 33 | * @deprecated use {@link AbstractArrayDefinition} |
d37aaa7f FC |
34 | * @version 1.0 |
35 | * @author Matthew Khouzam | |
36 | * @author Simon Marchi | |
866e5b51 | 37 | */ |
a4fa4e36 | 38 | @NonNullByDefault |
7b4f13e6 | 39 | @Deprecated |
363edfc2 | 40 | public final class ArrayDefinition extends AbstractArrayDefinition{ |
866e5b51 FC |
41 | |
42 | // ------------------------------------------------------------------------ | |
43 | // Attributes | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
a4fa4e36 | 46 | private final ImmutableList<Definition> fDefinitions; |
866e5b51 FC |
47 | |
48 | // ------------------------------------------------------------------------ | |
49 | // Constructors | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
9ac2eb62 MK |
52 | /** |
53 | * Constructor | |
a4fa4e36 MK |
54 | * |
55 | * @param declaration | |
56 | * the parent declaration | |
57 | * @param definitionScope | |
58 | * the parent scope | |
59 | * @param fieldName | |
60 | * the field name | |
61 | * @param definitions | |
62 | * the content of the array | |
63 | * @since 3.0 | |
9ac2eb62 | 64 | */ |
866e5b51 | 65 | public ArrayDefinition(ArrayDeclaration declaration, |
a4fa4e36 MK |
66 | @Nullable IDefinitionScope definitionScope, |
67 | String fieldName, | |
68 | List<Definition> definitions) { | |
69 | super(declaration, definitionScope, fieldName); | |
70 | @SuppressWarnings("null") | |
71 | @NonNull ImmutableList<Definition> list = ImmutableList.copyOf(definitions); | |
72 | fDefinitions = list; | |
866e5b51 | 73 | |
866e5b51 FC |
74 | } |
75 | ||
76 | // ------------------------------------------------------------------------ | |
77 | // Getters/Setters/Predicates | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
363edfc2 | 80 | @Override |
a4fa4e36 MK |
81 | public List<Definition> getDefinitions() { |
82 | return fDefinitions; | |
866e5b51 FC |
83 | } |
84 | ||
9ac2eb62 MK |
85 | /** |
86 | * Get the element at i | |
a4fa4e36 | 87 | * |
9ac2eb62 | 88 | * @param i the index (cannot be negative) |
ab04fc6b | 89 | * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception |
9ac2eb62 | 90 | */ |
a4fa4e36 | 91 | @Nullable |
866e5b51 | 92 | public Definition getElem(int i) { |
a4fa4e36 | 93 | if (i > fDefinitions.size()) { |
866e5b51 FC |
94 | return null; |
95 | } | |
96 | ||
a4fa4e36 | 97 | return fDefinitions.get(i); |
866e5b51 FC |
98 | } |
99 | ||
9ac2eb62 | 100 | @Override |
866e5b51 | 101 | public ArrayDeclaration getDeclaration() { |
a4fa4e36 | 102 | return (ArrayDeclaration) super.getDeclaration(); |
866e5b51 FC |
103 | } |
104 | ||
866e5b51 FC |
105 | // ------------------------------------------------------------------------ |
106 | // Operations | |
107 | // ------------------------------------------------------------------------ | |
108 | ||
866e5b51 FC |
109 | @Override |
110 | public String toString() { | |
111 | StringBuilder b = new StringBuilder(); | |
112 | ||
a4fa4e36 MK |
113 | if (getDeclaration().isString()) { |
114 | for (Definition def : fDefinitions) { | |
866e5b51 FC |
115 | IntegerDefinition character = (IntegerDefinition) def; |
116 | ||
117 | if (character.getValue() == 0) { | |
118 | break; | |
119 | } | |
120 | ||
121 | b.append(character.toString()); | |
122 | } | |
866e5b51 FC |
123 | } else { |
124 | b.append('['); | |
a4fa4e36 MK |
125 | Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ |
126 | b.append(joiner.join(fDefinitions)); | |
127 | b.append(']'); | |
866e5b51 FC |
128 | } |
129 | ||
a4fa4e36 MK |
130 | @SuppressWarnings("null") |
131 | @NonNull String ret = b.toString(); | |
132 | return ret; | |
866e5b51 FC |
133 | } |
134 | } |