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>
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
11
******************************************************************************/
13
package org.eclipse.linuxtools.tmf.core.statevalue;
15
import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
19
* This is the wrapper class that exposes the different types of 'state values'
20
* available to use in the State System.
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
26
* For now the two available types are either int or String.
29
* @author Alexandre Montplaisir
31
public abstract class TmfStateValue implements ITmfStateValue {
34
* Retrieve directly the value object contained within. Implementing
35
* subclasses may limit the return type here.
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-"
42
* @return The underneath object assigned to this state value.
44
protected abstract Object getValue();
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
52
* @return The state value in byte array form
54
public abstract byte[] toByteArray();
57
public boolean equals(Object other) {
61
if (!(other instanceof TmfStateValue)) {
65
/* If both types are different they're necessarily not equal */
66
if (this.getType() != ((TmfStateValue) other).getType()) {
71
* This checks for the case where we'd compare two null values (and so
78
/* The two are valid and comparable, let's compare them */
79
return this.getValue().equals(((TmfStateValue) other).getValue());
83
public int hashCode() {
87
return this.getValue().hashCode();
91
* Return the max size that a variable-length state value can have when
94
* @return The maximum size in bytes
96
public static int getStateValueMaxSize() {
97
return Byte.MAX_VALUE;
101
* Since all "null state values" are the same, we only need one copy in
104
private static TmfStateValue nullValue = new NullStateValue();
107
* Return an instance of a "null" value. Only one copy exists in memory.
109
* @return A null value
111
public final static TmfStateValue nullValue() {
116
* Factory constructor for Integer state values
118
* @param intValue The integer value to contain
119
* @return The newly-created TmfStateValue object
121
public static TmfStateValue newValueInt(int intValue) {
122
if (intValue == -1) {
125
return new IntegerStateValue(intValue);
129
* Factory constructor for String state values
131
* @param strValue The string value to contain
132
* @return The newly-create TmfStateValue object
134
public static TmfStateValue newValueString(String strValue) {
135
if (strValue == null) {
138
return new StringStateValue(strValue);
142
public int unboxInt() throws StateValueTypeException {
144
/* Int value expected, return "-1" instead */
148
if (this.getType() != 0) { /* 0 = int type */
149
throw new StateValueTypeException();
151
return (Integer) this.getValue();
155
public String unboxStr() throws StateValueTypeException {
157
/* String value expected, return "nullValue" instead */
158
return "nullValue"; //$NON-NLS-1$
161
if (this.getType() != 1) { /* 1 = string type */
162
throw new StateValueTypeException();
164
return (String) this.getValue();