-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
PersistentDataContainer.java
144 lines (126 loc) · 5.3 KB
/
PersistentDataContainer.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
package com.bgsoftware.superiorskyblock.api.persistence;
import javax.annotation.Nullable;
import java.util.function.BiConsumer;
public interface PersistentDataContainer {
/**
* Check if the provided key has a matching metadata value.
*
* @param key The key to check.
*/
boolean has(String key);
/**
* Check if the provided key has a matching metadata value of the provided type.
*
* @param key The key to check.
* @param type The type to check.
*/
<T> boolean hasKeyOfType(String key, PersistentDataType<T> type);
/**
* Store a metadata value matching the provided key and type.
*
* @param key The key to store.
* @param type The type of the metadata value.
* @param value The metadata value to store.
* @return The old metadata value that was stored matching the key, if exists.
* @throws IllegalArgumentException If the old metadata value is not of type {@param type}.
* @throws IllegalStateException If {@param type} doesn't have a valid serializer available.
*/
@Nullable
<T> T put(String key, PersistentDataType<T> type, T value) throws IllegalArgumentException, IllegalStateException;
/**
* Store a metadata value matching the provided key and type.
*
* @param key The key to store.
* @param type The type of the metadata.
* @param value The metadata value to store.
* @param returnType The type of the old metadata value.
* @return The old metadata value that was stored matching the key, if exists.
* @throws IllegalArgumentException If the old metadata value is not of type {@param returnType}.
* @throws IllegalStateException If {@param type} doesn't have a valid serializer available.
*/
@Nullable
<T, R> R put(String key, PersistentDataType<T> type, T value, PersistentDataType<R> returnType) throws IllegalArgumentException, IllegalStateException;
/**
* Remove a metadata value matching the provided key.
*
* @param key The key to remove.
* @return The old metadata value that was stored matching the key, if exists.
*/
@Nullable
Object remove(String key);
/**
* Remove a metadata value matching the provided key and type.
* If the metadata value doesn't match the {@param type}, it will not get removed.
*
* @param key The key to remove.
* @param type The type of the metadata value to remove.
* @return The old metadata value that was stored matching the key, if exists.
* If the metadata value does not match the current type, null will be returned.
*/
@Nullable
<T> T removeKeyOfType(String key, PersistentDataType<T> type);
/**
* Get a metadata value matching the provided key and type.
*
* @param key The key to fetch.
* @param type The type of the metadata value to fetch.
* @return The metadata value that is stored matching the key, if exists.
* @throws IllegalArgumentException If the metadata value is not of type {@param type}.
*/
@Nullable
<T> T get(String key, PersistentDataType<T> type) throws IllegalArgumentException;
/**
* Get a metadata value matching the provided key.
*
* @param key The key to fetch.
* @return The metadata value that is stored matching the key, if exists.
*/
@Nullable
Object get(String key);
/**
* Get a metadata value matching the provided key and type.
*
* @param key The key to fetch.
* @param type The type of the metadata value to fetch.
* @param def Value to return in case there is no metadata value matching the provided key.
* @return The metadata value that is stored matching the key, or {@param def} otherwise.
* @throws IllegalArgumentException If the metadata value is not of type {@param type}.
*/
<T> T getOrDefault(String key, PersistentDataType<T> type, T def) throws IllegalArgumentException;
/**
* Get a metadata value matching the provided key.
*
* @param key The key to fetch.
* @param def Value to return in case there is no metadata value matching the provided key.
* @return The metadata value that is stored matching the key, or {@param def} otherwise.
*/
Object getOrDefault(String key, Object def);
/**
* Check whether the container is empty.
*/
boolean isEmpty();
/**
* Get the size of the container.
*/
int size();
/**
* Iterate through all the data of the container.
*
* @param action The action to perform for each key and value pair.
*/
void forEach(BiConsumer<String, Object> action);
/**
* Get the serialized contents of the container as a bytes array.
* The format of the serialized data may be different depending on the implementation of the container.
* The serialized data must be loaded without any errors using {@link #load(byte[])}.
*/
byte[] serialize();
/**
* Load contents from the serialized data into the container.
* The format of the serialized data may be different depending on the implementation of the container.
*
* @param data The serialized data.
* @throws IllegalArgumentException If the given data cannot be serialized correctly.
*/
void load(byte[] data) throws IllegalArgumentException;
}