Fix NLS-related Javadoc warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statevalue / TmfStateValue.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 ******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.statevalue;
14
15 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
16
17
18 /**
19 * This is the wrapper class that exposes the different types of 'state values'
20 * available to use in the State System.
21 *
22 * This also defines how these values are to be stored in the History Tree. For
23 * example, we can save numerical values as integers instead of arrays of
24 * 1-digit characters.
25 *
26 * For now the two available types are either int or String.
27 *
28 * @version 1.0
29 * @author Alexandre Montplaisir
30 */
31 public abstract class TmfStateValue implements ITmfStateValue {
32
33 /**
34 * Retrieve directly the value object contained within. Implementing
35 * subclasses may limit the return type here.
36 *
37 * It's protected, since we do not want to expose this directly in the
38 * public API (and require all its users to manually cast to the right
39 * types). All accesses to the values should go through the "unbox-"
40 * methods.
41 *
42 * @return The underneath object assigned to this state value.
43 */
44 protected abstract Object getValue();
45
46 /**
47 * Specify how to "serialize" this value when writing it to a file.
48 * Alternatively you can return "null" here if you do not need a byte-array
49 * indirection (the getValue will get written as-in instead of the offset in
50 * the file block)
51 *
52 * @return The state value in byte array form
53 */
54 public abstract byte[] toByteArray();
55
56 @Override
57 public boolean equals(Object other) {
58 if (this == other) {
59 return true;
60 }
61 if (!(other instanceof TmfStateValue)) {
62 return false;
63 }
64
65 /* If both types are different they're necessarily not equal */
66 if (this.getType() != ((TmfStateValue) other).getType()) {
67 return false;
68 }
69
70 /*
71 * This checks for the case where we'd compare two null values (and so
72 * avoid a NPE below)
73 */
74 if (this.isNull()) {
75 return true;
76 }
77
78 /* The two are valid and comparable, let's compare them */
79 return this.getValue().equals(((TmfStateValue) other).getValue());
80 }
81
82 @Override
83 public int hashCode() {
84 if (this.isNull()) {
85 return 0;
86 }
87 return this.getValue().hashCode();
88 }
89
90 /**
91 * Return the max size that a variable-length state value can have when
92 * serialized.
93 *
94 * @return The maximum size in bytes
95 */
96 public static int getStateValueMaxSize() {
97 return Byte.MAX_VALUE;
98 }
99
100 /*
101 * Since all "null state values" are the same, we only need one copy in
102 * memory.
103 */
104 private static TmfStateValue nullValue = new NullStateValue();
105
106 /**
107 * Return an instance of a "null" value. Only one copy exists in memory.
108 *
109 * @return A null value
110 */
111 public final static TmfStateValue nullValue() {
112 return nullValue;
113 }
114
115 /**
116 * Factory constructor for Integer state values
117 *
118 * @param intValue The integer value to contain
119 * @return The newly-created TmfStateValue object
120 */
121 public static TmfStateValue newValueInt(int intValue) {
122 if (intValue == -1) {
123 return nullValue();
124 }
125 return new IntegerStateValue(intValue);
126 }
127
128 /**
129 * Factory constructor for String state values
130 *
131 * @param strValue The string value to contain
132 * @return The newly-create TmfStateValue object
133 */
134 public static TmfStateValue newValueString(String strValue) {
135 if (strValue == null) {
136 return nullValue();
137 }
138 return new StringStateValue(strValue);
139 }
140
141 /**
142 * Factory constructor for Long state values
143 *
144 * @param longValue The long value to contain
145 * @return The newly-create TmfStateValue object
146 * @since 2.0
147 */
148 public static TmfStateValue newValueLong(long longValue) {
149 if (longValue == -1) {
150 return nullValue();
151 }
152 return new LongStateValue(longValue);
153 }
154
155 @Override
156 public int unboxInt() throws StateValueTypeException {
157 if (this.isNull()) {
158 /* Int value expected, return "-1" instead */
159 return -1;
160 }
161
162 if (this.getType() != Type.INTEGER) {
163 throw new StateValueTypeException();
164 }
165 return (Integer) this.getValue();
166 }
167
168 @Override
169 public String unboxStr() throws StateValueTypeException {
170 if (this.isNull()) {
171 /* String value expected, return "nullValue" instead */
172 return "nullValue"; //$NON-NLS-1$
173 }
174
175 if (this.getType() != Type.STRING) {
176 throw new StateValueTypeException();
177 }
178 return (String) this.getValue();
179 }
180
181 /**
182 * @since 2.0
183 */
184 @Override
185 public long unboxLong() throws StateValueTypeException {
186 if (this.isNull()) {
187 /* Long value expected, return "-1" instead */
188 return -1;
189 }
190
191 if (this.getType() != Type.LONG) {
192 throw new StateValueTypeException();
193 }
194 return (Long) this.getValue();
195 }
196 }
This page took 0.035007 seconds and 5 git commands to generate.