// 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 }
// 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 }
func (h *Resource) outOfBounds(q us.Quantity) bool { return us.Less(q, h.min) || us.More(q, h.max) }