Skip to main content
summaryrefslogtreecommitdiffstats
blob: 159aae5ceaa85e860d53449ca57043c85320a279 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2012, 2013 Ericsson
 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 ******************************************************************************/

package org.eclipse.linuxtools.tmf.core.statevalue;

import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;


/**
 * This is the wrapper class that exposes the different types of 'state values'
 * available to use in the State System.
 *
 * This also defines how these values are to be stored in the History Tree. For
 * example, we can save numerical values as integers instead of arrays of
 * 1-digit characters.
 *
 * For now the two available types are either int or String.
 *
 * @version 1.0
 * @author Alexandre Montplaisir
 */
public abstract class TmfStateValue implements ITmfStateValue {

    /**
     * Retrieve directly the value object contained within. Implementing
     * subclasses may limit the return type here.
     *
     * It's protected, since we do not want to expose this directly in the
     * public API (and require all its users to manually cast to the right
     * types). All accesses to the values should go through the "unbox-"
     * methods.
     *
     * @return The underneath object assigned to this state value.
     */
    protected abstract Object getValue();

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof TmfStateValue)) {
            return false;
        }

        /* If both types are different they're necessarily not equal */
        if (this.getType() != ((TmfStateValue) other).getType()) {
            return false;
        }

        /*
         * This checks for the case where we'd compare two null values (and so
         * avoid a NPE below)
         */
        if (this.isNull()) {
            return true;
        }

        /* The two are valid and comparable, let's compare them */
        return this.getValue().equals(((TmfStateValue) other).getValue());
    }

    @Override
    public int hashCode() {
        if (this.isNull()) {
            return 0;
        }
        return this.getValue().hashCode();
    }

    /*
     * Since all "null state values" are the same, we only need one copy in
     * memory.
     */
    private static TmfStateValue nullValue = new NullStateValue();

    /**
     * Return an instance of a "null" value. Only one copy exists in memory.
     *
     * @return A null value
     */
    public static final TmfStateValue nullValue() {
        return nullValue;
    }

    /**
     * Factory constructor for Integer state values
     *
     * @param intValue The integer value to contain
     * @return The newly-created TmfStateValue object
     */
    public static TmfStateValue newValueInt(int intValue) {
        if (intValue == -1) {
            return nullValue();
        }
        return new IntegerStateValue(intValue);
    }

    /**
     * Factory constructor for String state values
     *
     * @param strValue The string value to contain
     * @return The newly-create TmfStateValue object
     */
    public static TmfStateValue newValueString(String strValue) {
        if (strValue == null) {
            return nullValue();
        }
        return new StringStateValue(strValue);
    }

    /**
     * Factory constructor for Long state values
     *
     * @param longValue The long value to contain
     * @return The newly-create TmfStateValue object
     * @since 2.0
     */
    public static TmfStateValue newValueLong(long longValue) {
        if (longValue == -1) {
            return nullValue();
        }
        return new LongStateValue(longValue);
    }

    @Override
    public int unboxInt() throws StateValueTypeException {
        if (this.isNull()) {
            /* Int value expected, return "-1" instead */
            return -1;
        }

        if (this.getType() != Type.INTEGER) {
            throw new StateValueTypeException();
        }
        return (Integer) this.getValue();
    }

    @Override
    public String unboxStr() throws StateValueTypeException {
        if (this.isNull()) {
            /* String value expected, return "nullValue" instead */
            return "nullValue"; //$NON-NLS-1$
        }

        if (this.getType() != Type.STRING) {
            throw new StateValueTypeException();
        }
        return (String) this.getValue();
    }

    /**
     * @since 2.0
     */
    @Override
    public long unboxLong() throws StateValueTypeException {
        if (this.isNull()) {
            /* Long value expected, return "-1" instead */
            return -1;
        }

        if (this.getType() != Type.LONG) {
            throw new StateValueTypeException();
        }
        return (Long) this.getValue();
    }
}

Back to the top