private ProgressEventDispatcher(IEventHandlers eventHandlers, Allocation allocation) { this.eventHandlers = eventHandlers; this.allocation = allocation; remainingAllocationUnits = allocation.GetAllocationUnits(); }
/** * Helper method for {@link #updateProgress(Allocation, long)}. Subtract {@code units} from {@code * indexedRemainingUnits}. Updates {@link IndexedRemainingUnits} for parent {@link Allocation}s if * remaining units becomes 0. This method is <em>not</em> thread-safe for the {@code * indexedRemainingUnits} and should be called atomically relative to the {@code * indexedRemainingUnits}. * * @param indexedRemainingUnits the {@link IndexedRemainingUnits} to update progress for * @param units the units of progress */ private void UpdateIndexedRemainingUnits( IndexedRemainingUnits indexedRemainingUnits, long units) { if (units == 0) { return; } Allocation allocation = indexedRemainingUnits.allocation; long newUnits = indexedRemainingUnits.remainingUnits.AddAndGet(-units); if (newUnits < 0L) { throw new InvalidOperationException( "Progress exceeds max for '" + allocation.GetDescription() + "': " + -newUnits + " more beyond " + allocation.GetAllocationUnits()); } // Updates the parent allocations if this allocation completed. if (newUnits == 0L) { allocation .GetParent() .IfPresent( parentAllocation => UpdateIndexedRemainingUnits( Preconditions.CheckNotNull(completionMap[parentAllocation]), 1L)); } }
public IndexedRemainingUnits(Allocation allocation) { this.allocation = allocation; remainingUnits = new AtomicLong(allocation.GetAllocationUnits()); }