ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / types / StringDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2011, 2014 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
f357bcd4 13package org.eclipse.tracecompass.ctf.core.event.types;
866e5b51 14
d890ec37
MK
15import org.eclipse.jdt.annotation.NonNullByDefault;
16import org.eclipse.jdt.annotation.Nullable;
17import org.eclipse.tracecompass.common.core.NonNullUtils;
680f9173 18import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
19import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
20import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
a4fa4e36 21
866e5b51 22/**
d37aaa7f 23 * A CTF string declaration.
a511da0d 24 *
d37aaa7f
FC
25 * Strings are an array of bytes of variable size and are terminated by a '\0'
26 * "NULL" character. Their encoding is described in the TSDL meta-data. In
27 * absence of encoding attribute information, the default encoding is UTF-8.
28 *
29 * @version 1.0
30 * @author Matthew Khouzam
31 * @author Simon Marchi
866e5b51 32 */
d890ec37 33@NonNullByDefault
a4fa4e36 34public class StringDeclaration extends Declaration {
866e5b51 35
f068c622
MK
36 private static final StringDeclaration STRING_DEC_UTF8 = new StringDeclaration(Encoding.UTF8);
37 private static final StringDeclaration STRING_DEC_ASCII = new StringDeclaration(Encoding.ASCII);
38 private static final StringDeclaration STRING_DEC_NO_ENC = new StringDeclaration(Encoding.NONE);
39
866e5b51
FC
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
f068c622 44 private static final int BITS_PER_BYTE = Byte.SIZE;
a4fa4e36 45 private final Encoding fEncoding;
866e5b51
FC
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
9ac2eb62 51 /**
a511da0d 52 * Generate an encoded string declaration
e00e6663
MK
53 *
54 * @param encoding
55 * the encoding, utf8 or ascii
9ac2eb62 56 */
d890ec37 57 private StringDeclaration(Encoding encoding) {
a4fa4e36 58 fEncoding = encoding;
866e5b51
FC
59 }
60
d890ec37
MK
61 /**
62 * Create a StringDeclaration with the default UTF-8 encoding
63 *
64 * @return a {@link StringDeclaration} with UTF-8 encoding
65 */
66 public static StringDeclaration getStringDeclaration() {
67 return STRING_DEC_UTF8;
68 }
69
f068c622
MK
70 /**
71 * Create a StringDeclaration
72 *
73 * @param encoding
d890ec37
MK
74 * the {@link Encoding} can be Encoding.UTF8, Encoding.ASCII or
75 * other
f068c622
MK
76 * @return a {@link StringDeclaration}
77 * @throws IllegalArgumentException
78 * if the encoding is not recognized.
79 */
80 public static StringDeclaration getStringDeclaration(Encoding encoding) {
81 switch (encoding) {
82 case ASCII:
83 return STRING_DEC_ASCII;
84 case NONE:
85 return STRING_DEC_NO_ENC;
86 case UTF8:
87 return STRING_DEC_UTF8;
88 default:
89 throw new IllegalArgumentException("Unrecognized encoding: " + encoding); //$NON-NLS-1$
90 }
91 }
92
866e5b51
FC
93 // ------------------------------------------------------------------------
94 // Getters/Setters/Predicates
95 // ------------------------------------------------------------------------
96
9ac2eb62
MK
97 /**
98 *
99 * @return the character encoding.
100 */
101 public Encoding getEncoding() {
a4fa4e36 102 return fEncoding;
866e5b51
FC
103 }
104
fd74e6c1
MK
105 @Override
106 public long getAlignment() {
818bd3de 107 // See ctf 4.2.5: Strings are always aligned on byte size.
f068c622 108 return BITS_PER_BYTE;
fd74e6c1 109 }
a4fa4e36 110
a4fa4e36
MK
111 @Override
112 public int getMaximumSize() {
5b53901a
AM
113 /*
114 * Every definition can have a different size, so we do not scope this.
115 * Minimum size is one byte (8 bits) though.
116 */
117 return 8;
a4fa4e36 118 }
e00e6663 119
866e5b51
FC
120 // ------------------------------------------------------------------------
121 // Operations
122 // ------------------------------------------------------------------------
123
124 @Override
d890ec37 125 public StringDefinition createDefinition(@Nullable IDefinitionScope definitionScope,
680f9173 126 String fieldName, BitBuffer input) throws CTFException {
a4fa4e36
MK
127 String value = read(input);
128 return new StringDefinition(this, definitionScope, fieldName, value);
866e5b51
FC
129 }
130
680f9173 131 private String read(BitBuffer input) throws CTFException {
a4fa4e36
MK
132 /* Offset the buffer position wrt the current alignment */
133 alignRead(input);
134
135 StringBuilder sb = new StringBuilder();
f068c622 136 char c = (char) input.get(BITS_PER_BYTE, false);
a4fa4e36
MK
137 while (c != 0) {
138 sb.append(c);
f068c622 139 c = (char) input.get(BITS_PER_BYTE, false);
a4fa4e36 140 }
d890ec37 141 return NonNullUtils.checkNotNull(sb.toString());
a4fa4e36 142 }
e00e6663 143
866e5b51
FC
144 @Override
145 public String toString() {
146 /* Only used for debugging */
147 return "[declaration] string[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
148 }
149
e00e6663
MK
150 @Override
151 public int hashCode() {
152 final int prime = 31;
153 int result = prime;
d890ec37
MK
154 switch (fEncoding) {
155 case ASCII:
156 result += 1;
157 break;
158 case NONE:
159 result += 2;
160 break;
161 case UTF8:
162 result += 3;
163 break;
164 default:
165 break;
e00e6663
MK
166 }
167 return result;
168 }
169
170 @Override
d890ec37 171 public boolean equals(@Nullable Object obj) {
e00e6663
MK
172 if (this == obj) {
173 return true;
174 }
175 if (obj == null) {
176 return false;
177 }
178 if (getClass() != obj.getClass()) {
179 return false;
180 }
181 StringDeclaration other = (StringDeclaration) obj;
182 if (fEncoding != other.fEncoding) {
183 return false;
184 }
185 return true;
186 }
187
66aa25f0 188 @Override
d890ec37 189 public boolean isBinaryEquivalent(@Nullable IDeclaration other) {
66aa25f0
MK
190 return equals(other);
191 }
192
866e5b51 193}
This page took 0.068174 seconds and 5 git commands to generate.