// 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 }
// Convert converts a given quantity to the Context's default. func (ctx Context) Convert(q us.Quantity) us.Quantity { return q.Convert(ctx.Unit) }