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
;
17 * This is the wrapper class that exposes the different types of 'state values'
18 * available to use in the State System.
20 * This also defines how these values are to be stored in the History Tree. For
21 * example, we can save numerical values as integers instead of arrays of
24 * For now the two available types are either int or String.
29 public abstract class TmfStateValue
implements ITmfStateValue
{
32 * Retrieve directly the value object contained within. Implementing
33 * subclasses may limit the return type here.
35 * It's protected, since we do not want to expose this directly in the
36 * public API (and require all its users to manually cast to the right
37 * types). All accesses to the values should go through the "unbox-"
40 * @return The underneath object assigned to this state value.
42 protected abstract Object
getValue();
45 * Specify how to "serialize" this value when writing it to a file.
46 * Alternatively you can return "null" here if you do not need a byte-array
47 * indirection (the getValue will get written as-in instead of the offset in
50 public abstract byte[] toByteArray();
53 public boolean equals(Object other
) {
57 if (!(other
instanceof TmfStateValue
)) {
61 /* If both types are different they're necessarily not equal */
62 if (this.getType() != ((TmfStateValue
) other
).getType()) {
67 * This checks for the case where we'd compare two null values (and so
74 /* The two are valid and comparable, let's compare them */
75 return this.getValue().equals(((TmfStateValue
) other
).getValue());
79 public int hashCode() {
83 return this.getValue().hashCode();
87 * Return the max size that a variable-length state value can have when
90 * @return The maximum size in bytes
92 public static int getStateValueMaxSize() {
93 return Byte
.MAX_VALUE
;
97 * Since all "null state values" are the same, we only need one copy in
100 private static TmfStateValue nullValue
= new NullStateValue();
103 * Return an instance of a "null" value. Only one copy exists in memory.
105 * @return A null value
107 public final static TmfStateValue
nullValue() {
112 * Factory constructor for Integer state values
114 * @param intValue The integer value to contain
115 * @return The newly-created TmfStateValue object
117 public static TmfStateValue
newValueInt(int intValue
) {
118 if (intValue
== -1) {
121 return new IntegerStateValue(intValue
);
125 * Factory constructor for String state values
127 * @param strValue The string value to contain
128 * @return The newly-create TmfStateValue object
130 public static TmfStateValue
newValueString(String strValue
) {
131 if (strValue
== null) {
134 return new StringStateValue(strValue
);
138 public int unboxInt() throws StateValueTypeException
{
140 /* Int value expected, return "-1" instead */
144 if (this.getType() != 0) { /* 0 = int type */
145 throw new StateValueTypeException();
147 return (Integer
) this.getValue();
151 public String
unboxStr() throws StateValueTypeException
{
153 /* String value expected, return "nullValue" instead */
154 return "nullValue"; //$NON-NLS-1$
157 if (this.getType() != 1) { /* 1 = string type */
158 throw new StateValueTypeException();
160 return (String
) this.getValue();
This page took 0.051097 seconds and 5 git commands to generate.