Beispiel #1
0
// Min sets a new minimum balance. Returns true for success, false for incompatible unit
// or in case the min is more than the current balance.
func (h *Resource) Min(min us.Quantity) bool {
	if !us.AreCompatible(h.balance, min) || us.More(min, h.balance) {
		return false
	}
	h.min = min
	return true
}
Beispiel #2
0
// Set the Resource to the given value. The value should be between the min
// and max of the Resource. Return true for success, false for incompatible unit
// or out of bounds.
func (h *Resource) Set(q us.Quantity) bool {
	if !us.AreCompatible(h.balance, q) || h.outOfBounds(q) {
		return false
	}
	h.balance = q
	return true
}
Beispiel #3
0
// Max sets a new maximum balance. Returns true for success, false for incompatible unit
// or in case the max is less than the current balance.
func (h *Resource) Max(max us.Quantity) bool {
	if !us.AreCompatible(h.balance, max) || us.Less(max, h.balance) {
		return false
	}
	h.max = max
	return true
}
Beispiel #4
0
// Withdraw subtracts the given amount from the Resource.
// Return true for success, false for incompatible unit or out of bounds
func (h *Resource) Withdraw(q us.Quantity) bool {
	if !us.AreCompatible(h.balance, q) {
		return false
	}
	n := us.Subtract(h.balance, q)
	if h.outOfBounds(n) {
		return false
	}
	h.balance = n
	return true
}
Beispiel #5
0
// Deposit adds the Measurement to the Resource. Return true for success, false for
// incompatible unit or out of bounds.
func (h *Resource) Deposit(q us.Quantity) bool {
	if !us.AreCompatible(h.balance, q) {
		return false
	}
	n := us.Add(h.balance, q)
	if h.outOfBounds(n) {
		return false
	}
	h.balance = n
	return true
}
Beispiel #6
0
// New creates a new Resource with the given minimum and maximum values.
// min should be less than max and the units should be compatible.
// The initial balance value is set to min. A Context name can be provided, or ""
// if no Context is required.
func New(min us.Quantity, max us.Quantity, c string) *Resource {
	var ctx *context.Context
	if c != "" {
		ctx = context.Ctx(c)
	} else {
		ctx, _ = context.DefineContext("", min.Symbol(), us.DefaultFormat)
	}
	if us.AreCompatible(min, max) && us.Less(min, max) {
		return &Resource{ctx.Convert(min), ctx.Convert(max), min, ctx}
	}
	return nil
}