Exemplo n.º 1
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
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
func (h *Resource) outOfBounds(q us.Quantity) bool {
	return us.Less(q, h.min) || us.More(q, h.max)
}