Toshio Koide | 3c84631 | 2014-08-21 19:47:15 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowmanager; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import net.onrc.onos.api.flowmanager.FlowBatchId; |
| 5 | import net.onrc.onos.core.util.IdBlock; |
| 6 | import net.onrc.onos.core.util.IdBlockAllocator; |
| 7 | import net.onrc.onos.core.util.UnavailableIdException; |
| 8 | |
| 9 | /** |
| 10 | * Generates a global unique FlowBatchId using |
| 11 | * {@link IdBlockAllocator#allocateUniqueIdBlock()}. |
| 12 | */ |
| 13 | public class FlowBatchIdGeneratorWithIdBlockAllocator { |
| 14 | private final IdBlockAllocator allocator; |
| 15 | private IdBlock idBlock; |
| 16 | |
| 17 | /** |
| 18 | * Creates a FlowBatchId generator instance using specified ID block allocator. |
| 19 | * |
| 20 | * @param allocator the ID block allocator to be used |
| 21 | */ |
| 22 | public FlowBatchIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) { |
| 23 | this.allocator = checkNotNull(allocator); |
| 24 | this.idBlock = allocator.allocateUniqueIdBlock(); |
| 25 | } |
| 26 | |
| 27 | public synchronized FlowBatchId getNewId() { |
| 28 | try { |
| 29 | return new FlowBatchId(idBlock.getNextId()); |
| 30 | } catch (UnavailableIdException e) { |
| 31 | idBlock = allocator.allocateUniqueIdBlock(); |
| 32 | return new FlowBatchId(idBlock.getNextId()); |
| 33 | } |
| 34 | } |
| 35 | } |