ss: Add serialization logic to state values
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / statesystem / core / statevalue / StringStateValue.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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.tracecompass.statesystem.core.statevalue;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
19
20 /**
21 * A state value containing a variable-sized string
22 *
23 * @version 1.0
24 * @author Alexandre Montplaisir
25 */
26 final class StringStateValue extends TmfStateValue {
27
28 private final String value;
29
30 public StringStateValue(String valueAsString) {
31 this.value = valueAsString;
32 }
33
34 @Override
35 public Type getType() {
36 return Type.STRING;
37 }
38
39 @Override
40 public boolean isNull() {
41 return false;
42 }
43
44 @Override
45 public boolean equals(@Nullable Object object) {
46 if (!(object instanceof StringStateValue)) {
47 return false;
48 }
49 StringStateValue other = (StringStateValue) object;
50 return value.equals(other.value);
51 }
52
53 @Override
54 public int hashCode() {
55 return value.hashCode();
56 }
57
58 @Override
59 public String toString() {
60 return value;
61 }
62
63 @Override
64 public byte[] serialize() {
65 byte[] strBytes = value.getBytes();
66 ByteBuffer buffer = ByteBuffer.allocate(Byte.BYTES + strBytes.length);
67 buffer.put(getType().getByte());
68 buffer.put(strBytes);
69 return buffer.array();
70 }
71
72 // ------------------------------------------------------------------------
73 // Unboxing methods
74 // ------------------------------------------------------------------------
75
76 @Override
77 public String unboxStr() {
78 return value;
79 }
80
81 @Override
82 public int compareTo(@Nullable ITmfStateValue other) {
83 if (other == null) {
84 throw new IllegalArgumentException();
85 }
86
87 switch (other.getType()) {
88 case DOUBLE:
89 throw new StateValueTypeException("A String state value cannot be compared to a Double state value."); //$NON-NLS-1$
90 case INTEGER:
91 throw new StateValueTypeException("A String state value cannot be compared to an Integer state value."); //$NON-NLS-1$
92 case LONG:
93 throw new StateValueTypeException("A String state value cannot be compared to a Long state value."); //$NON-NLS-1$
94 case NULL:
95 /*
96 * We assume that every string state value is greater than a null
97 * state value.
98 */
99 return 1;
100 case STRING:
101 StringStateValue otherStringValue = (StringStateValue) other;
102 return value.compareTo(otherStringValue.value);
103 default:
104 throw new StateValueTypeException("A String state value cannot be compared to the type " + other.getType()); //$NON-NLS-1$
105 }
106
107 }
108
109 }
This page took 0.040554 seconds and 5 git commands to generate.