-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.go
executable file
·101 lines (87 loc) · 3.12 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package bitty
/*
Copyright 2020 IBM
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Symbolic enables Unit to provide a standard, exponent, and symbol
type Symbolic interface {
// Standard returns the standard for the unit as defined by the SI brochure,
// 9th Edition, page 145:
// https://www.bipm.org/utils/common/pdf/si-brochure/SI-Brochure-9.pdf
Standard() UnitStandard
// Exponent returns the supported exponent as an int
// To calculate the value from the standard, symbol, and size the formulas
// are:
// - Given the Standard is SI, value as v is equal to (10^e)size
// - Given the Standard is IEC, value as v is equal to (2^(e*10))size
Exponent() int
// Symbol returns the supported symbol as a UnitSymbol
Symbol() UnitSymbol
}
// Sizer enables Unit to get a size measured by bit, byte, or arbitrary data
// Unit kind
type Sizer interface {
// Size returns the size of the Unit
Size() float64
// BitSize returns the size of the Unit measured in bits
BitSize() float64
// ByteSize returns the size of the Unit measured in bytes
ByteSize() float64
// SizeInUnit returns the size of the Unit measured in an arbitrary
// UnitSymbol from Bit up to YiB or YB
SizeInUnit(UnitSymbol) float64
}
// Calculator enables Units to be calculated against each other
// All returns are diminshing or increasing UnitSymbol measurements as defined
// by the SI and IEC
type Calculator interface {
// Add attempts to add one Unit to another
Add(Unit) Unit
// Subtract attempts to subtract one Unit from another
Subtract(Unit) Unit
// Multiply attempts to multiply one Unit by another
Multiply(Unit) Unit
// Divide attempts to divide one Unit from another
Divide(Unit) Unit
}
// Unit enables Unit kinds to interact with each other
type Unit interface {
Symbolic
Sizer
Calculator
}
// BaseUnitSymbolPair represents the bit and byte pairs
type BaseUnitSymbolPair struct {
standard *UnitStandard
}
// NewBaseUnitSymbolPair takes a UnitStandard and returns a new UnitSymbolPair
func NewBaseUnitSymbolPair(std UnitStandard) UnitSymbolPair {
return &BaseUnitSymbolPair{standard: &std}
}
// Standard returns the UnitStandard of a BaseUnitSymbolPair if it exists or SI
func (b *BaseUnitSymbolPair) Standard() UnitStandard {
if b.standard == nil {
return SI
}
return *b.standard
}
// Exponent returns the exponent of a BaseUnitSymbolPair: 0
func (b *BaseUnitSymbolPair) Exponent() int {
return 0
}
// Least returns the least UnitSymbol of a BaseUnitSymbolPair: a Bit
func (b *BaseUnitSymbolPair) Least() UnitSymbol {
return Bit
}
// Greatest returns the greatest UnitSymbol of a BaseUnitSymbolPair: a Byte
func (b *BaseUnitSymbolPair) Greatest() UnitSymbol {
return Byte
}