Counters

package
v0.2.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package counter provides a set of interfaces and methods for managing counters.

Package counter provides a set of interfaces and methods for managing counters.

Package counter provides a set of interfaces and methods for managing counters.

Index

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.
	IsDone() bool

	// Advance advances the counter by one step. It panics if the counter
	// is already at its limit.
	Advance() error

	// Retreat retreats the counter by one step. It panics if the counter
	// is already at its minimum limit.
	Retreat() error

	// GetRetreatCount returns the number of times the counter has been retreated.
	GetRetreatCount() int

	// GetDistance returns the distance from the initial count to the current count.
	GetDistance() int

	// GetCurrentCount returns the current count.
	GetCurrentCount() int

	// GetInitialCount returns the initial count when the counter was first created.
	GetInitialCount() int

	// String returns a string representation of the counter.
	fmt.Stringer

	// DeepCopy creates a deep copy of the counter.
	DeepCopy() Counter

	// Reset resets the counter to its initial state.
	Reset()
}

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. It panics with an error of type *ers.ErrInvalidParameter if the starting count is less than 0.

Parameters:

  • startingCount is the initial value of the counter.

Returns:

  • The newly created DownCounter.

func (*DownCounter) Advance added in v0.2.6

func (c *DownCounter) Advance() error

Advance decrements the current count of the DownCounter by one. It panics with an error of type *ers.ErrCallFailed if the current count is already at or below zero.

func (*DownCounter) GetCurrentCount added in v0.2.6

func (c *DownCounter) GetCurrentCount() int

GetCurrentCount returns the current count of the DownCounter.

Returns:

  • An integer representing the current count.

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:

  • An integer representing the current count.

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:

  • An integer representing the initial count.

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:

  • An integer representing the number of times the counter 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:

  • true if the current count is less than or equal to 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. It panics with an error of type *ers.ErrCallFailed if the starting count or 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.

Returns:

  • A string representing the DownCounter.

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

func NewUpCounter(upperLimit int) (UpCounter, error)

NewUpCounter creates a new UpCounter with the specified upper limit. It panics with an error of type *ers.ErrInvalidParameter if the upper limit is less than 0.

Parameters:

  • upperLimit is the maximum limit that the counter can reach.

Returns:

  • The newly created UpCounter.

func (*UpCounter) Advance added in v0.2.6

func (c *UpCounter) Advance() error

Advance increments the current count of the UpCounter by one. It panics with an error of type *ers.ErrCallFailed if the current count is already at or beyond the upper limit.

func (*UpCounter) GetCurrentCount added in v0.2.6

func (c *UpCounter) GetCurrentCount() int

GetCurrentCount returns the current count of the UpCounter.

Returns:

  • An integer representing the current count.

func (*UpCounter) GetDistance added in v0.2.6

func (c *UpCounter) GetDistance() int

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:

  • An integer representing the distance.

func (*UpCounter) GetInitialCount added in v0.2.6

func (c *UpCounter) GetInitialCount() int

GetInitialCount returns the upper limit of the UpCounter, which is the initial count.

Returns:

  • An integer representing the initial count.

func (*UpCounter) GetRetreatCount added in v0.2.6

func (c *UpCounter) GetRetreatCount() int

GetRetreatCount returns the number of times the UpCounter has been retreated.

Returns:

  • An integer representing the retreat count.

func (*UpCounter) IsDone

func (c *UpCounter) IsDone() bool

IsDone checks if the UpCounter has reached its upper limit.

Returns:

  • true if the current count is greater than or equal to the 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

func (c *UpCounter) Retreat() error

Retreat increments the retreat count and, as a result, decrements the upper limit of the UpCounter by one. It panics with an error of type *ers.ErrCallFailed if the current count is already at or beyond the upper limit.

func (*UpCounter) String

func (c *UpCounter) String() string

String returns a string representation of the UpCounter. The string includes the upper limit, current count, retreat count, and whether the counter is done.

Returns:

  • A string representing the UpCounter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL