1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.kryo;
15
16 import java.util.HashSet;
17 import java.util.Iterator;
18
19 import static ch.qos.logback.core.kryo.KryoCoreConstants.MAX_SERIALIZER_ID;
20
21
22
23
24
25 final class SetSerializerIdPool implements SerializerIdPool {
26
27 private final HashSet<Integer> free = new HashSet<>();
28 private int nextId = 1;
29
30 @Override
31 public Integer allocate() {
32 if (!free.isEmpty()) {
33 Iterator<Integer> it = free.iterator();
34 Integer id = it.next();
35 it.remove();
36 return id;
37 }
38 if (nextId > MAX_SERIALIZER_ID) {
39 throw new IllegalStateException(
40 "Serializer id exhausted (allowed 1.." + MAX_SERIALIZER_ID + ")");
41 }
42 return BoxedIds.of(nextId++);
43 }
44
45 @Override
46 public boolean hasFree() {
47 return !free.isEmpty();
48 }
49
50 @Override
51 public void recycle(Integer id) {
52 if (id != null) {
53 free.add(id);
54 }
55 }
56
57 @Override
58 public void unfree(Integer id) {
59 if (id != null) {
60 free.remove(id);
61 }
62 }
63 }