Skip to content

Commit

Permalink
package refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Jan 10, 2017
1 parent 959f6d3 commit 4238afe
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Folders
_obj
_test
_bin
_build

# Architecture specific extensions/prefixes
*.[568vq]
Expand All @@ -22,3 +24,9 @@ _testmain.go
*.exe
*.test
*.prof

# OS X Stuff
.DS_Store

# Coverage
*.coverprofile
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: go

go:
- 1.6
- 1.7

install:
- go get github.com/mattn/goveralls

script: make test

after_script:
- goveralls -coverprofile=sequence.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN

env:
global:
- secure: aULdMQtdcXQvqhk8dTSrIW8isqLiBbOR91e2d8FuIrL2doRCRvnB3NazYIlhR26Ud5p8BpKV76uMtFdOX/dKLEAW6Ws46DHjKaKIkxmu/WuZSHAg7amQmKHA/jA895FEEA0aMrMr3TxPq7gVG8RLS5LGUUavSrynkr/dXcSXEgI=

notifications:
email:
recipients:
- benjamin@bengfort.com
on_success: change
on_failure: always
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Shell to use with Make
SHELL := /bin/bash

# Export targets not associated with files.
.PHONY: fmt test citest clean

# Format the Go source code
fmt:
@echo "Formatting the source"
-gofmt -w .

# Target for simple testing on the command line
test:
go test -v -coverprofile=sequence.coverprofile github.com/bbengfort/sequence

# Clean build files
clean:
@echo "Cleaning up the project source."
-go clean
-find . -name "*.coverprofile" -print0 | xargs -0 rm -rf
-rm -rf site
-rm -rf _bin
-rm -rf _build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Sequences maintain their state and you can check if they've been started:

counter.IsStarted()

You can also reseet sequences to return them to a starting value:
You can also reset sequences to return them to a starting value:

counter.Restart()

Expand Down
15 changes: 8 additions & 7 deletions sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ import (

const maxuint64 = ^uint64(0) - 1

// Returns a new sequence from the package
// New returns a new default sequence (infinite increment by 1 from 1).
func New() *Sequence {
return &Sequence{0, 1, 1, maxuint64}
}

//=============================================================================

// Implements an AutoIncrement counter class similar to PostgreSQL's sequence.
// Sequence implements an AutoIncrement counter class similar to the
// PostgreSQL sequence object.
type Sequence struct {
current uint64 // The current value of the sequence
increment uint64 // The value to increment by (usually 1)
minvalue uint64 // The minimum value of the counter (usually 1)
maxvalue uint64 // The max value of the counter (usually bounded by type)
}

// What you can expect an Incrementer to do (and Sequences do this)
// Incrementer defines the interface for sequence-like objects.
type Incrementer interface {
Init(params ...uint64) // Initialize the Incrementer with values
Next() (uint64, error) // Get the next value in the sequence and update
Expand All @@ -33,7 +34,7 @@ type Incrementer interface {

//=============================================================================

// Initialize a sequence with uint64 params, ordered similarly to the struct
// Init a sequence with uint64 params, ordered similarly to the struct
func (s *Sequence) Init(params ...uint64) {
if len(params) > 0 {
s.current = params[0]
Expand All @@ -60,7 +61,7 @@ func (s *Sequence) Init(params ...uint64) {
}
}

// Update the sequence and return the next value
// Next updates the sequence and return the next value
func (s *Sequence) Next() (uint64, error) {
s.current += s.increment

Expand All @@ -82,7 +83,7 @@ func (s *Sequence) Restart() {
s.current = s.minvalue - s.increment
}

// Returns the current value of the sequence
// Current returns the current value of the sequence
func (s *Sequence) Current() (uint64, error) {
if !s.IsStarted() {
return 0, errors.New("Sequence is unstarted")
Expand All @@ -91,7 +92,7 @@ func (s *Sequence) Current() (uint64, error) {
return s.current, nil
}

// Returns the state of the sequence (started or unstarted)
// IsStarted returns the state of the sequence (started or unstarted)
func (s *Sequence) IsStarted() bool {
return !(s.current < s.minvalue) && s.current < s.maxvalue
}
Expand Down

0 comments on commit 4238afe

Please sign in to comment.