-
Notifications
You must be signed in to change notification settings - Fork 12
/
ArrayDefinition.java
121 lines (107 loc) · 4.42 KB
/
ArrayDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
* Copyright (c) 2014 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Matthew Khouzam - Initial API and implementation
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.types;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
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.
*
* @author Matthew Khouzam
*/
@NonNullByDefault
public final class ArrayDefinition extends AbstractArrayDefinition {
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
private final List<Definition> fDefinitions;
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
/**
* Constructor
*
* @param declaration
* the parent declaration
* @param definitionScope
* the parent scope
* @param fieldName
* the field name
* @param definitions
* the content of the array
*/
public ArrayDefinition(CompoundDeclaration declaration,
@Nullable IDefinitionScope definitionScope,
String fieldName,
List<Definition> definitions) {
super(declaration, definitionScope, fieldName);
fDefinitions = ImmutableList.copyOf(definitions);
}
// ------------------------------------------------------------------------
// Getters/Setters/Predicates
// ------------------------------------------------------------------------
@Override
public List<Definition> getDefinitions() {
return fDefinitions;
}
@Override
public int getLength() {
return fDefinitions.size();
}
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
@Override
public String toString() {
if (fDefinitions.isEmpty() ) {
return "[]"; //$NON-NLS-1$
}
Definition firstDefinition = fDefinitions.get(0);
if (firstDefinition instanceof IntegerDefinition) {
IntegerDefinition integerDefinition = (IntegerDefinition) firstDefinition;
if (integerDefinition.getDeclaration().isCharacter()) {
// String will return "String", no brackets.
// Brackets can be added by others like fields
StringBuilder b = new StringBuilder();
for (Definition definition : fDefinitions) {
// Blind cast since all definitions are of the same type.
// 0 means the string is terminated.
if (((IntegerDefinition) definition).getIntegerValue() == 0) {
return b.toString();
}
b.append(definition);
}
return b.toString();
}
}
StringBuilder b = new StringBuilder();
b.append('[');
Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
b.append(joiner.join(fDefinitions));
b.append(']');
return b.toString();
}
}