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; |
| 5 | import net.onrc.onos.core.registry.IdBlock; |
| 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 | |
| 20 | public IdBlockAllocatorBasedIntentIdGenerator(IdBlockAllocator allocator) { |
| 21 | this.allocator = checkNotNull(allocator); |
| 22 | this.idBlock = allocator.allocateUniqueIdBlock(); |
| 23 | } |
| 24 | |
| 25 | @Override |
| 26 | public synchronized IntentId getNewId() { |
| 27 | try { |
| 28 | return new IntentId(idBlock.getNextId()); |
| 29 | } catch (UnavailableIdException e) { |
| 30 | idBlock = allocator.allocateUniqueIdBlock(); |
| 31 | return new IntentId(idBlock.getNextId()); |
| 32 | } |
| 33 | } |
| 34 | } |