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 MK |
15 | import java.util.List; |
16 | ||
17 | import org.eclipse.jdt.annotation.NonNull; | |
18 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
19 | ||
20 | import com.google.common.base.Joiner; | |
21 | import com.google.common.collect.ImmutableList; | |
866e5b51 FC |
22 | |
23 | /** | |
d37aaa7f | 24 | * A CTF sequence definition (a fixed-size array). |
486efb2e | 25 | * |
d37aaa7f FC |
26 | * An array where the size is fixed but declared in the trace, unlike array |
27 | * where it is declared with a literal | |
28 | * | |
7b4f13e6 | 29 | * @deprecated use {@link AbstractArrayDefinition} |
d37aaa7f FC |
30 | * @version 1.0 |
31 | * @author Matthew Khouzam | |
32 | * @author Simon Marchi | |
866e5b51 | 33 | */ |
7b4f13e6 | 34 | @Deprecated |
a4fa4e36 MK |
35 | public final class SequenceDefinition extends Definition { |
36 | ||
866e5b51 FC |
37 | // ------------------------------------------------------------------------ |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
a4fa4e36 | 41 | private final ImmutableList<Definition> fDefinitions; |
866e5b51 FC |
42 | |
43 | // ------------------------------------------------------------------------ | |
44 | // Constructors | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
9ac2eb62 MK |
47 | /** |
48 | * Constructor | |
49 | * | |
50 | * @param declaration | |
51 | * the parent declaration | |
52 | * @param definitionScope | |
53 | * the parent scope | |
54 | * @param fieldName | |
55 | * the field name | |
a4fa4e36 MK |
56 | * @param definitions |
57 | * Definitions | |
58 | * @since 3.0 | |
9ac2eb62 | 59 | */ |
a4fa4e36 MK |
60 | public SequenceDefinition(@NonNull SequenceDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, List<Definition> definitions) { |
61 | super(declaration, definitionScope, fieldName); | |
62 | fDefinitions = ImmutableList.copyOf(definitions); | |
866e5b51 FC |
63 | } |
64 | ||
65 | // ------------------------------------------------------------------------ | |
66 | // Getters/Setters/Predicates | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
9ac2eb62 | 69 | @Override |
866e5b51 | 70 | public SequenceDeclaration getDeclaration() { |
a4fa4e36 | 71 | return (SequenceDeclaration) super.getDeclaration(); |
866e5b51 FC |
72 | } |
73 | ||
9ac2eb62 MK |
74 | /** |
75 | * The length of the sequence in number of elements so a sequence of 5 | |
76 | * GIANT_rediculous_long_ints is the same as a sequence of 5 bits. (5) | |
77 | * | |
78 | * @return the length of the sequence | |
79 | */ | |
866e5b51 | 80 | public int getLength() { |
a4fa4e36 | 81 | return fDefinitions.size(); |
866e5b51 FC |
82 | } |
83 | ||
9ac2eb62 MK |
84 | /** |
85 | * Get the element at i | |
86 | * | |
87 | * @param i | |
88 | * the index (cannot be negative) | |
ab04fc6b | 89 | * @return The element at I, if I > length, null, if I < 0, the method |
9ac2eb62 MK |
90 | * throws an out of bounds exception |
91 | */ | |
866e5b51 | 92 | public Definition getElem(int i) { |
a4fa4e36 | 93 | if (i > fDefinitions.size()) { |
866e5b51 FC |
94 | return null; |
95 | } | |
a4fa4e36 | 96 | return fDefinitions.get(i); |
866e5b51 FC |
97 | } |
98 | ||
99 | // ------------------------------------------------------------------------ | |
100 | // Operations | |
101 | // ------------------------------------------------------------------------ | |
102 | ||
866e5b51 FC |
103 | @Override |
104 | public String toString() { | |
105 | StringBuilder b = new StringBuilder(); | |
106 | ||
a4fa4e36 MK |
107 | if (getDeclaration().isString()) { |
108 | for (Definition def : fDefinitions) { | |
109 | IntegerDefinition character = (IntegerDefinition) def; | |
866e5b51 FC |
110 | |
111 | if (character.getValue() == 0) { | |
112 | break; | |
113 | } | |
114 | ||
115 | b.append(character.toString()); | |
116 | } | |
117 | } else { | |
118 | b.append('['); | |
a4fa4e36 MK |
119 | Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ |
120 | b.append(joiner.join(fDefinitions)); | |
121 | b.append(']'); | |
866e5b51 FC |
122 | } |
123 | ||
124 | return b.toString(); | |
125 | } | |
126 | } |