/
Memento.java
230 lines (210 loc) · 5.21 KB
/
Memento.java
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*******************************************************************************
* Copyright (c) 2015 BestSolution.at and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v.2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
*******************************************************************************/
package org.eclipse.fx.core;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* A simple storage API for states that need to be stored
*
* @since 2.0
*/
public interface Memento {
/**
* Store a string value
*
* @param key
* the key
* @param value
* the value
*/
public void put(@NonNull String key, String value);
/**
* Store a boolean value
*
* @param key
* the key
* @param value
* the value
*/
public void put(@NonNull String key, boolean value);
/**
* Store an integer value
*
* @param key
* the key
* @param value
* the value
*/
public void put(@NonNull String key, int value);
/**
* Store a double value
*
* @param key
* the key
* @param value
* the value
*/
public void put(@NonNull String key, double value);
/**
* Restore an object serialized with the given serializer
*
* @param key
* the key
* @param value
* the value
* @param serializer
* the id of the serializer
*/
public void put(@NonNull String key, Object value, String serializer);
/**
* Remove the given key
*
* @param key
* the key
*/
public void remove(@NonNull String key);
/**
* Check if the given key exists
*
* @param key
* the key
* @return <code>true</code> if exists else <code>false</code>
*/
public boolean exists(@NonNull String key);
/**
* Retrieve the value for the given key
*
* @param key
* the key
* @param defaultValue
* the default value if the key does not exists, is <code>null</code>
* or not a {@link String}
* @return the value or the default value provided
*/
public @Nullable String get(@NonNull String key, @Nullable String defaultValue);
/**
* Retrieve the value for the given key
*
* @param key
* the key
* @param defaultValue
* the default value of the key does not exits, is <code>null</code>
* or not a boolean
* @return the value or the default value provided
*/
public boolean get(@NonNull String key, boolean defaultValue);
/**
* Retrieve the value for the given key
*
* @param key
* the value
* @param defaultValue
* the default value of the key does not exits, is <code>null</code>
* or not an int
* @return the value or the default value provided
*/
public int get(@NonNull String key, int defaultValue);
/**
* Retrieve the value for the given key
*
* @param key
* the key
* @param defaultValue
* the default value if the key does not exits, is <code>null</code>
* or not a double
* @return the value or the default value provided
*/
public double get(@NonNull String key, double defaultValue);
/**
* Retrieve the deserialized object
*
* @param key
* the key
* @param clazz
* the type
* @param defaultValue
* the default value if the key does not exits, is <code>null</code>
* or can not be deserialized
* @return the value or default value provided
*/
public <O> @Nullable O get(String key, Class<O> clazz, @Nullable O defaultValue);
/**
* Retrieve the deserialized object as an optional
*
* @param key
* the key
* @param clazz
* the type
* @return the value or default value provided
* @since 2.2
*/
public default <@Nullable O> Optional<O> get(String key, Class<O> clazz) {
O o = get(key, clazz, null);
return Optional.ofNullable(o);
}
/**
* Get the default boolean value for the key
*
* @param key
* the key
* @return the default
* @since 3.2
*/
public default boolean getDefaultBoolean(String key) {
return false;
}
/**
* Get the default integer value for the key
*
* @param key
* the key
* @return the value
* @since 3.2
*/
public default int getDefaultInteger(String key) {
return 0;
}
/**
* Get the default double value for the key
*
* @param key
* the key
* @return the value
*/
public default double getDefaultDouble(String key) {
return 0.0;
}
/**
* Get the default String value for the key
*
* @param key
* the key
* @return the default value
*/
public default String getDefaultString(String key) {
return ""; //$NON-NLS-1$
}
/**
* Get the default Object value for the key
*
* @param key
* the key
* @param clazz
* the type
* @return the value
*/
public default <@Nullable O> O getDefault(String key, Class<O> clazz) {
return null;
}
}