forked from jankotek/mapdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializer.java
More file actions
66 lines (47 loc) · 1.71 KB
/
Copy pathSerializer.java
File metadata and controls
66 lines (47 loc) · 1.71 KB
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
package org.mapdb.ser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapdb.io.DataInput2;
import org.mapdb.io.DataInput2ByteArray;
import org.mapdb.io.DataOutput2;
import org.mapdb.io.DataOutput2ByteArray;
import java.io.IOError;
import java.io.IOException;
import java.util.Comparator;
/** Turns object instance into binary form and vice versa */
public interface Serializer<K> extends Comparator<K> { //TODO deatach comparator from serializer???
void serialize(@NotNull DataOutput2 out, @NotNull K k);
K deserialize(@NotNull DataInput2 input);
@Nullable Class serializedType();
default boolean equals(@Nullable K k1, @Nullable K k2){
return k1==k2 || (k1!=null && k1.equals(k2));
}
default int hashCode(@NotNull K k){
return k.hashCode(); //TODO better hash
}
default int hashCode(@NotNull K k, int hashSeed){
return hashSeed + k.hashCode(); //TODO better mixing
}
default K deserialize(DataInput2 in, int i){
if(i!=-1)
throw new AssertionError(); //FIXME temp method for compatibility
return deserialize(in);
}
@Deprecated
default boolean isTrusted(){
return true;
}
default int compare(K k1, K k2){
return ((Comparable)k1).compareTo(k2); //TODO comparators
}
default int fixedSize() {
return -1;
}
/** Creates binary copy of given object. If the datatype is immutable the same instance might be returned */
default K clone(K value){
DataOutput2 out = new DataOutput2ByteArray();
serialize(out, value);
DataInput2 in2 = new DataInput2ByteArray(out.copyBytes());
return deserialize(in2);
}
}