~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
Import upstream version 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    @Override
 
142
    public int unboxInt() throws StateValueTypeException {
 
143
        if (this.isNull()) {
 
144
            /* Int value expected, return "-1" instead */
 
145
            return -1;
 
146
        }
 
147
 
 
148
        if (this.getType() != 0) { /* 0 = int type */
 
149
            throw new StateValueTypeException();
 
150
        }
 
151
        return (Integer) this.getValue();
 
152
    }
 
153
 
 
154
    @Override
 
155
    public String unboxStr() throws StateValueTypeException {
 
156
        if (this.isNull()) {
 
157
            /* String value expected, return "nullValue" instead */
 
158
            return "nullValue"; //$NON-NLS-1$
 
159
        }
 
160
 
 
161
        if (this.getType() != 1) { /* 1 = string type */
 
162
            throw new StateValueTypeException();
 
163
        }
 
164
        return (String) this.getValue();
 
165
    }
 
166
}