Documentation ¶
Overview ¶
Package Counters provides a set of interfaces and methods for managing counters.
Index ¶
- type Counter
- type DownCounter
- func (c *DownCounter) Advance() error
- func (c *DownCounter) Copy() uc.Copier
- func (c *DownCounter) Equals(other uc.Equaler) bool
- func (c *DownCounter) GetCurrentCount() int
- func (c *DownCounter) GetDistance() int
- func (c *DownCounter) GetInitialCount() int
- func (c *DownCounter) GetRetreatCount() int
- func (c *DownCounter) IsDone() bool
- func (c *DownCounter) Reset()
- func (c *DownCounter) Retreat() error
- func (c *DownCounter) String() string
- type ErrCurrentCountAboveUpperLimit
- type ErrCurrentCountBelowZero
- type UpCounter
- func (c *UpCounter) Advance() error
- func (c *UpCounter) Copy() uc.Copier
- func (c *UpCounter) Equals(other uc.Equaler) bool
- func (c *UpCounter) GetCurrentCount() int
- func (c *UpCounter) GetDistance() int
- func (c *UpCounter) GetInitialCount() int
- func (c *UpCounter) GetRetreatCount() int
- func (c *UpCounter) IsDone() bool
- func (c *UpCounter) Reset()
- func (c *UpCounter) Retreat() error
- func (c *UpCounter) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Counter ¶ added in v0.2.6
type Counter interface { // IsDone checks if the counter has reached its limit. // // Returns: // - bool: true if the counter has reached its limit, false otherwise. IsDone() bool // Advance advances the counter by one step. // // Returns: // - error: An error if the counter is already at its maximum limit. Advance() error // Retreat retreats the counter by one step. // // Returns: // - error: An error if the counter is already at its minimum limit. Retreat() error // GetRetreatCount returns the number of times the counter has been // retreated. // // Returns: // - int: The number of times the counter has been retreated. GetRetreatCount() int // GetDistance returns the distance from the initial count to the current // count. // // Returns: // - int: The distance from the initial count to the current count. GetDistance() int // GetCurrentCount returns the current count. // // Returns: // - int: The current count. GetCurrentCount() int // GetInitialCount returns the initial count when the counter was first // created. // // Returns: // - int: The initial count. GetInitialCount() int // Reset resets the counter to its initial state. Reset() uc.Objecter }
Counter interface defines the methods that a counter must implement. A counter is a tool that can be advanced or retreated, and it keeps track of its state.
type DownCounter ¶ added in v0.2.6
type DownCounter struct {
// contains filtered or unexported fields
}
DownCounter represents a counter that decrements downwards until it reaches zero.
func NewDownCounter ¶ added in v0.2.6
func NewDownCounter(startingCount int) (*DownCounter, error)
NewDownCounter creates a new DownCounter with the specified starting count.
Parameters:
- startingCount: The initial value of the counter.
Returns:
- *DownCounter: A pointer to the new DownCounter.
- error: An error of type *uc.ErrInvalidParameter if the starting count is less than zero.
func (*DownCounter) Advance ¶ added in v0.2.6
func (c *DownCounter) Advance() error
Advance decrements the current count of the DownCounter by one.
Returns:
- error: An error of type *ErrCurrentCountBelowZero if the current count is already at or below zero.
func (*DownCounter) Copy ¶ added in v0.2.15
func (c *DownCounter) Copy() uc.Copier
Copy creates a shallow copy of the DownCounter.
Returns:
- uc.Copier: A shallow copy of the DownCounter.
func (*DownCounter) Equals ¶ added in v0.3.2
func (c *DownCounter) Equals(other uc.Equaler) bool
Equals implements common.Objecter.
func (*DownCounter) GetCurrentCount ¶ added in v0.2.6
func (c *DownCounter) GetCurrentCount() int
GetCurrentCount returns the current count of the DownCounter.
Returns:
- int: The current count of the DownCounter.
func (*DownCounter) GetDistance ¶ added in v0.2.6
func (c *DownCounter) GetDistance() int
GetDistance returns the current count of the DownCounter. This is equivalent to the distance from zero, as the DownCounter decrements towards zero.
Returns:
- int: The current count of the DownCounter.
func (*DownCounter) GetInitialCount ¶ added in v0.2.6
func (c *DownCounter) GetInitialCount() int
GetInitialCount returns the starting count of the DownCounter, which is the initial count.
Returns:
- int: The starting count of the DownCounter.
func (*DownCounter) GetRetreatCount ¶ added in v0.2.6
func (c *DownCounter) GetRetreatCount() int
GetRetreatCount returns the number of times the DownCounter has been retreated.
Returns:
- int: The number of times the DownCounter has been retreated.
func (*DownCounter) IsDone ¶ added in v0.2.6
func (c *DownCounter) IsDone() bool
IsDone checks if the DownCounter has reached zero.
Returns:
- bool: true if the counter has reached zero, false otherwise.
func (*DownCounter) Reset ¶ added in v0.2.9
func (c *DownCounter) Reset()
Reset resets the DownCounter to its initial state.
func (*DownCounter) Retreat ¶ added in v0.2.6
func (c *DownCounter) Retreat() error
Retreat increments the retrat count of the DownCounter by one and, as a result, decrements the current count by one.
Returns:
- error: An error of type *ErrCurrentCountBelowZero if the current count is already at or below zero.
func (*DownCounter) String ¶ added in v0.2.6
func (c *DownCounter) String() string
String returns a string representation of the DownCounter. The string includes the starting count, current count, retreat count, and whether the counter is done.
This is used for debugging and logging purposes.
Returns:
- string: A string representation of the DownCounter.
type ErrCurrentCountAboveUpperLimit ¶ added in v0.2.26
type ErrCurrentCountAboveUpperLimit struct{}
ErrCurrentCountAboveUpperLimit represents an error where the current count is already at or beyond the upper limit.
func NewErrCurrentCountAboveUpperLimit ¶ added in v0.2.26
func NewErrCurrentCountAboveUpperLimit() *ErrCurrentCountAboveUpperLimit
NewErrCurrentCountAboveUpperLimit creates a new ErrCurrentCountAboveUpperLimit error.
Returns:
- *ErrCurrentCountAboveUpperLimit: A pointer to the new error.
func (*ErrCurrentCountAboveUpperLimit) Error ¶ added in v0.2.26
func (e *ErrCurrentCountAboveUpperLimit) Error() string
Error is a method of the error interface.
Returns:
- string: The error message.
type ErrCurrentCountBelowZero ¶ added in v0.2.26
type ErrCurrentCountBelowZero struct{}
ErrCurrentCountBelowZero represents an error where the current count is already at or below zero.
func NewErrCurrentCountBelowZero ¶ added in v0.2.26
func NewErrCurrentCountBelowZero() *ErrCurrentCountBelowZero
NewErrCurrentCountBelowZero creates a new ErrCurrentCountBelowZero error.
Returns:
- *ErrCurrentCountBelowZero: A pointer to the new error.
func (*ErrCurrentCountBelowZero) Error ¶ added in v0.2.26
func (e *ErrCurrentCountBelowZero) Error() string
Error is a method of the error interface that returns the error message.
Returns:
- string: The error message.
type UpCounter ¶
type UpCounter struct {
// contains filtered or unexported fields
}
UpCounter represents a counter that increments upwards until it reaches an upper limit.
func NewUpCounter ¶
NewUpCounter creates a new UpCounter with the specified upper limit.
Parameters:
- upperLimit: The maximum limit that the counter can reach.
Returns:
- *UpCounter: A pointer to the newly created UpCounter.
- error: An error of type *uc.ErrInvalidParameter if the upper limit is less than zero.
func (*UpCounter) Advance ¶ added in v0.2.6
Advance increments the current count of the UpCounter by one.
Returns:
- error: An error of type *ErrCurrentCountAboveUpperLimit if the current count is already at or beyond the upper limit.
func (*UpCounter) Copy ¶ added in v0.2.15
Copy creates a shallow copy of the UpCounter.
Returns:
- uc.Copier: A shallow copy of the UpCounter.
func (*UpCounter) GetCurrentCount ¶ added in v0.2.6
GetCurrentCount returns the current count of the UpCounter.
Returns:
- int: The current count of the UpCounter.
func (*UpCounter) GetDistance ¶ added in v0.2.6
GetDistance calculates the distance between the current count and the upper limit of the UpCounter, that is, the number of times the counter can still be advanced before reaching the upper limit.
Returns:
- int: The distance between the current count and the upper limit.
func (*UpCounter) GetInitialCount ¶ added in v0.2.6
GetInitialCount returns the upper limit of the UpCounter, which is the initial count.
Returns:
- int: The upper limit of the UpCounter.
func (*UpCounter) GetRetreatCount ¶ added in v0.2.6
GetRetreatCount returns the number of times the UpCounter has been retreated.
Returns:
- int: The number of times the counter has been retreated.
func (*UpCounter) IsDone ¶
IsDone checks if the UpCounter has reached its upper limit.
Returns:
- bool: true if the counter has reached its upper limit, false otherwise.
func (*UpCounter) Reset ¶ added in v0.2.9
func (c *UpCounter) Reset()
Reset resets the UpCounter to its initial state.
func (*UpCounter) Retreat ¶ added in v0.2.6
Retreat increments the retreat count and, as a result, decrements the upper limit of the UpCounter by one.
Returns:
- error: An error of type *ErrCurrentCountAboveUpperLimit if the current count is already at or beyond the upper limit.