blob: 29f14a1b7e612d244a7501e0efa66d2ed92d728b [file] [log] [blame]
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07001package net.onrc.onos.core.newintent;
2
3import net.onrc.onos.api.newintent.IntentId;
4import net.onrc.onos.api.newintent.IntentIdGenerator;
Sho SHIMIZUfc932d52014-08-15 11:22:37 -07005import net.onrc.onos.core.util.IdBlock;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07006import net.onrc.onos.core.util.UnavailableIdException;
7import net.onrc.onos.core.util.IdBlockAllocator;
8
9import static com.google.common.base.Preconditions.checkNotNull;
10
11/**
12 * An implementation of {@link IntentIdGenerator},
13 * which uses {@link IdBlockAllocator#allocateUniqueIdBlock()}.
14 */
15public class IdBlockAllocatorBasedIntentIdGenerator implements IntentIdGenerator {
16
17 private final IdBlockAllocator allocator;
18 private IdBlock idBlock;
19
Sho SHIMIZU42b92812014-08-17 19:56:44 -070020 /**
21 * Constructs a {@link IdBlockAllocatorBasedIntentIdGenerator}.
22 *
23 * @param allocator ID block allocator, which is used in this class
24 */
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070025 public IdBlockAllocatorBasedIntentIdGenerator(IdBlockAllocator allocator) {
26 this.allocator = checkNotNull(allocator);
27 this.idBlock = allocator.allocateUniqueIdBlock();
28 }
29
30 @Override
31 public synchronized IntentId getNewId() {
32 try {
33 return new IntentId(idBlock.getNextId());
34 } catch (UnavailableIdException e) {
35 idBlock = allocator.allocateUniqueIdBlock();
36 return new IntentId(idBlock.getNextId());
37 }
38 }
39}