-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTransparencyArchive.java
More file actions
53 lines (51 loc) · 1.71 KB
/
TransparencyArchive.java
File metadata and controls
53 lines (51 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
// Part of SourceAFIS Transparency API: https://sourceafis.machinezoo.com/transparency/
package com.machinezoo.sourceafis.transparency;
import java.util.*;
public interface TransparencyArchive {
/*
* Canonical order corresponding to the usual logging order. Unknown keys are at the end, sorted alphabetically.
*/
List<TransparencyKey<?>> keys();
<T> List<TransparencyRecord<T>> enumerate(TransparencyKey<T> key);
/*
* Same order as keys(). Canonical serialization format.
*/
default List<TransparencyRecord<?>> toList() {
var dump = new ArrayList<TransparencyRecord<?>>();
for (var key : keys())
dump.addAll(enumerate(key));
return dump;
}
static TransparencyArchive from(Collection<TransparencyRecord<?>> collection) {
return new TransparencyBuffer()
.append(collection)
.toArchive();
}
default <T> Optional<TransparencyRecord<T>> get(TransparencyKey<T> key, int offset) {
var list = enumerate(key);
if (offset < 0 || offset >= list.size())
return Optional.empty();
return Optional.of(list.get(offset));
}
default <T> Optional<TransparencyRecord<T>> get(TransparencyKey<T> key) {
return get(key, 0);
}
default int count(TransparencyKey<?> key) {
return enumerate(key).size();
}
default boolean contains(TransparencyKey<?> key) {
return !enumerate(key).isEmpty();
}
default Optional<byte[]> read(TransparencyKey<?> key, int offset) {
return get(key, offset).map(r -> r.data());
}
default Optional<byte[]> read(TransparencyKey<?> key) {
return read(key, 0);
}
default <T> Optional<T> deserialize(TransparencyKey<T> key, int offset) {
return get(key, offset).map(r -> r.deserialize());
}
default <T> Optional<T> deserialize(TransparencyKey<T> key) {
return deserialize(key, 0);
}
}