Merge Generic State System core part
[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
16 /**
17 * This is the wrapper class that exposes the different types of 'state values'
18 * available to use in the State System.
19 *
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
22 * 1-digit characters.
23 *
24 * For now the two available types are either int or String.
25 *
26 * @author alexmont
27 *
28 */
29 public abstract class TmfStateValue implements ITmfStateValue {
30
31 /**
32 * Retrieve directly the value object contained within. Implementing
33 * subclasses may limit the return type here.
34 *
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-"
38 * methods.
39 *
40 * @return The underneath object assigned to this state value.
41 */
42 protected abstract Object getValue();
43
44 /**
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
48 * the file block)
49 */
50 public abstract byte[] toByteArray();
51
52 @Override
53 public boolean equals(Object other) {
54 if (this == other) {
55 return true;
56 }
57 if (!(other instanceof TmfStateValue)) {
58 return false;
59 }
60
61 /* If both types are different they're necessarily not equal */
62 if (this.getType() != ((TmfStateValue) other).getType()) {
63 return false;
64 }
65
66 /*
67 * This checks for the case where we'd compare two null values (and so
68 * avoid a NPE below)
69 */
70 if (this.isNull()) {
71 return true;
72 }
73
74 /* The two are valid and comparable, let's compare them */
75 return this.getValue().equals(((TmfStateValue) other).getValue());
76 }
77
78 @Override
79 public int hashCode() {
80 if (this.isNull()) {
81 return 0;
82 }
83 return this.getValue().hashCode();
84 }
85
86 /**
87 * Return the max size that a variable-length state value can have when
88 * serialized.
89 *
90 * @return The maximum size in bytes
91 */
92 public static int getStateValueMaxSize() {
93 return Byte.MAX_VALUE;
94 }
95
96 /*
97 * Since all "null state values" are the same, we only need one copy in
98 * memory.
99 */
100 private static TmfStateValue nullValue = new NullStateValue();
101
102 /**
103 * Return an instance of a "null" value. Only one copy exists in memory.
104 *
105 * @return A null value
106 */
107 public final static TmfStateValue nullValue() {
108 return nullValue;
109 }
110
111 /**
112 * Factory constructor for Integer state values
113 *
114 * @param intValue The integer value to contain
115 * @return The newly-created TmfStateValue object
116 */
117 public static TmfStateValue newValueInt(int intValue) {
118 if (intValue == -1) {
119 return nullValue();
120 }
121 return new IntegerStateValue(intValue);
122 }
123
124 /**
125 * Factory constructor for String state values
126 *
127 * @param strValue The string value to contain
128 * @return The newly-create TmfStateValue object
129 */
130 public static TmfStateValue newValueString(String strValue) {
131 if (strValue == null) {
132 return nullValue();
133 }
134 return new StringStateValue(strValue);
135 }
136
137 @Override
138 public int unboxInt() throws StateValueTypeException {
139 if (this.isNull()) {
140 /* Int value expected, return "-1" instead */
141 return -1;
142 }
143
144 if (this.getType() != 0) { /* 0 = int type */
145 throw new StateValueTypeException();
146 }
147 return (Integer) this.getValue();
148 }
149
150 @Override
151 public String unboxStr() throws StateValueTypeException {
152 if (this.isNull()) {
153 /* String value expected, return "nullValue" instead */
154 return "nullValue"; //$NON-NLS-1$
155 }
156
157 if (this.getType() != 1) { /* 1 = string type */
158 throw new StateValueTypeException();
159 }
160 return (String) this.getValue();
161 }
162 }
This page took 0.051097 seconds and 5 git commands to generate.