3f0a29004861fca15ae08dd5344efe80c2e4bf94
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / SequenceDefinition.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.linuxtools.ctf.core.event.scope.IDefinitionScope;
19
20 import com.google.common.base.Joiner;
21 import com.google.common.collect.ImmutableList;
22
23 /**
24 * A CTF sequence definition (a fixed-size array).
25 *
26 * An array where the size is fixed but declared in the trace, unlike array
27 * where it is declared with a literal
28 *
29 * @deprecated use {@link AbstractArrayDefinition}
30 * @version 1.0
31 * @author Matthew Khouzam
32 * @author Simon Marchi
33 */
34 @Deprecated
35 public final class SequenceDefinition extends Definition {
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40
41 private final ImmutableList<Definition> fDefinitions;
42
43 // ------------------------------------------------------------------------
44 // Constructors
45 // ------------------------------------------------------------------------
46
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
56 * @param definitions
57 * Definitions
58 * @since 3.0
59 */
60 public SequenceDefinition(@NonNull SequenceDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, List<Definition> definitions) {
61 super(declaration, definitionScope, fieldName);
62 fDefinitions = ImmutableList.copyOf(definitions);
63 }
64
65 // ------------------------------------------------------------------------
66 // Getters/Setters/Predicates
67 // ------------------------------------------------------------------------
68
69 @Override
70 public SequenceDeclaration getDeclaration() {
71 return (SequenceDeclaration) super.getDeclaration();
72 }
73
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 */
80 public int getLength() {
81 return fDefinitions.size();
82 }
83
84 /**
85 * Get the element at i
86 *
87 * @param i
88 * the index (cannot be negative)
89 * @return The element at I, if I &gt; length, null, if I &lt; 0, the method
90 * throws an out of bounds exception
91 */
92 public Definition getElem(int i) {
93 if (i > fDefinitions.size()) {
94 return null;
95 }
96 return fDefinitions.get(i);
97 }
98
99 // ------------------------------------------------------------------------
100 // Operations
101 // ------------------------------------------------------------------------
102
103 @Override
104 public String toString() {
105 StringBuilder b = new StringBuilder();
106
107 if (getDeclaration().isString()) {
108 for (Definition def : fDefinitions) {
109 IntegerDefinition character = (IntegerDefinition) def;
110
111 if (character.getValue() == 0) {
112 break;
113 }
114
115 b.append(character.toString());
116 }
117 } else {
118 b.append('[');
119 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
120 b.append(joiner.join(fDefinitions));
121 b.append(']');
122 }
123
124 return b.toString();
125 }
126 }
This page took 0.033209 seconds and 4 git commands to generate.