Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
| 3 | import net.onrc.onos.api.newintent.IntentId; |
| 4 | import net.onrc.onos.api.newintent.IntentIdGenerator; |
Sho SHIMIZU | fc932d5 | 2014-08-15 11:22:37 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.IdBlock; |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.util.UnavailableIdException; |
| 7 | import net.onrc.onos.core.util.IdBlockAllocator; |
| 8 | |
| 9 | import static com.google.common.base.Preconditions.checkNotNull; |
| 10 | |
| 11 | /** |
| 12 | * An implementation of {@link IntentIdGenerator}, |
| 13 | * which uses {@link IdBlockAllocator#allocateUniqueIdBlock()}. |
| 14 | */ |
| 15 | public class IdBlockAllocatorBasedIntentIdGenerator implements IntentIdGenerator { |
| 16 | |
| 17 | private final IdBlockAllocator allocator; |
| 18 | private IdBlock idBlock; |
| 19 | |
Sho SHIMIZU | 42b9281 | 2014-08-17 19:56:44 -0700 | [diff] [blame^] | 20 | /** |
| 21 | * Constructs a {@link IdBlockAllocatorBasedIntentIdGenerator}. |
| 22 | * |
| 23 | * @param allocator ID block allocator, which is used in this class |
| 24 | */ |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 25 | 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 | } |