summaryrefslogtreecommitdiff
path: root/java/src/Ice/Optional.java
blob: 1b44f4148a2470db09c36345613377d5001a262a (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package Ice;

/**
 * Generic class for optional parameters.
 **/
public class Optional<T>
{
    /**
     * The value defaults to unset.
     **/
    public Optional()
    {
        _isSet = false;
    }

    /**
     * Sets the value to the given argument.
     *
     * @param v The initial value.
     **/
    public Optional(T v)
    {
        _value = v;
        _isSet = true;
    }

    /**
     * Sets the value to a shallow copy of the given optional.
     *
     * @param opt The source value.
     **/
    public Optional(Optional<T> opt)
    {
        _value = opt._value;
        _isSet = opt._isSet;
    }

    /**
     * Obtains the current value.
     *
     * @return The current value.
     * @throws IllegalStateException If the value is not set.
     **/
    public T get()
    {
        if(!_isSet)
        {
            throw new IllegalStateException("no value is set");
        }
        return _value;
    }

    /**
     * Sets the value to the given argument.
     *
     * @param v The new value.
     **/
    public void set(T v)
    {
        _value = v;
        _isSet = true;
    }

    /**
     * If the given argument is set, this optional is set to a shallow copy of the argument,
     * otherwise this optional is unset.
     *
     * @param opt The source value.
     **/
    public void set(Optional<T> opt)
    {
        _value = opt._value;
        _isSet = opt._isSet;
    }

    /**
     * Determines whether the value is set.
     *
     * @return True if the value is set, false otherwise.
     **/
    public boolean isSet()
    {
        return _isSet;
    }

    /**
     * Unsets this value.
     **/
    public void clear()
    {
        _isSet = false;
        _value = null;
    }

    /**
     * Helper function for creating Optional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new Optional instance set to the given value.
     **/
    public static <T> Optional<T> O(T v)
    {
        return new Optional<T>(v);
    }

    /**
     * Helper function for creating BooleanOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new BooleanOptional instance set to the given value.
     **/
    public static BooleanOptional O(boolean v)
    {
        return new BooleanOptional(v);
    }

    /**
     * Helper function for creating ByteOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new ByteOptional instance set to the given value.
     **/
    public static ByteOptional O(byte v)
    {
        return new ByteOptional(v);
    }

    /**
     * Helper function for creating ShortOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new ShortOptional instance set to the given value.
     **/
    public static ShortOptional O(short v)
    {
        return new ShortOptional(v);
    }

    /**
     * Helper function for creating IntOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new IntOptional instance set to the given value.
     **/
    public static IntOptional O(int v)
    {
        return new IntOptional(v);
    }

    /**
     * Helper function for creating LongOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new LongOptional instance set to the given value.
     **/
    public static LongOptional O(long v)
    {
        return new LongOptional(v);
    }

    /**
     * Helper function for creating FloatOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new FloatOptional instance set to the given value.
     **/
    public static FloatOptional O(float v)
    {
        return new FloatOptional(v);
    }

    /**
     * Helper function for creating DoubleOptional instances.
     *
     * @param v The initial value of the Optional.
     *
     * @return A new DoubleOptional instance set to the given value.
     **/
    public static DoubleOptional O(double v)
    {
        return new DoubleOptional(v);
    }

    private T _value;
    private boolean _isSet;
}