Skip to content

Commit

Permalink
Split optional.go into more files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfourny committed Mar 22, 2024
1 parent c9ccac7 commit 386f341
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 123 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions pkg/opt/none.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package opt

// None represents the absence of a value.
type None[V any] struct {
}

// Assert that None[V] implements Optional[V]
var _ Optional[any] = None[any]{}

func (n None[V]) Present() bool {
return false
}

func (n None[V]) Get() (V, bool) {
var zero V
return zero, false
}

func (n None[V]) GetOrZero() V {
var zero V
return zero
}

func (n None[V]) GetOrDefault(defaultValue V) V {
return defaultValue
}

func (n None[V]) GetOrFunc(f func() V) V {
return f()
}

func (n None[V]) Filter(_ func(V) bool) Optional[V] {
return n
}

func (n None[V]) IfPresent(_ func(V)) bool {
return false
}

func (n None[V]) IfPresentElse(_ func(V), f func()) bool {
f()
return false
}

func (n None[V]) String() string {
return "None"
}
157 changes: 34 additions & 123 deletions pkg/opt/optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@ package opt

import "fmt"

// Optional is a generic type that takes one type parameter V and represents a value that may or may not be present.
// It is similar to Java's Optional type.
type Optional[V any] interface {
fmt.Stringer

// Present returns true if the Optional contains a value, false otherwise.
Present() bool

// Get returns the value contained in the Optional and an indicator of whether the Optional is empty.
// If the Optional is empty, the value returned is the zero value of type V.
Get() (V, bool)

// GetOrZero returns the value contained in the Optional, or the zero value of type V if the Optional is empty.
GetOrZero() V

// GetOrDefault returns the value contained in the Optional, or the provided default value if the Optional is empty.
GetOrDefault(defaultValue V) V

// GetOrFunc returns the value contained in the Optional, or the result of calling the provided function if the Optional is empty.
GetOrFunc(func() V) V

// IfPresent calls the provided function with the value contained in the Optional if the Optional is not empty.
// Returns true if the function was called, false otherwise.
IfPresent(func(V)) bool

// IfPresentElse calls the first function with the value contained in the Optional if the Optional is not empty, or the second function otherwise.
// Returns true if the first function was called, false otherwise.
IfPresentElse(func(V), func()) bool

// Filter returns an Optional containing the value contained in the Optional if the provided predicate returns true for that value.
// If the Optional is empty, an empty Optional is returned.
Filter(func(V) bool) Optional[V]
}

// Empty returns an empty Optional (None).
func Empty[V any]() Optional[V] {
return None[V]{}
Expand Down Expand Up @@ -103,126 +137,3 @@ func MaybeMap[V, U any](o Optional[V], mapper func(V) (U, bool)) Optional[U] {
}
return Empty[U]()
}

// Optional is a generic type that takes one type parameter V and represents a value that may or may not be present.
// It is similar to Java's Optional type.
type Optional[V any] interface {
fmt.Stringer

// Present returns true if the Optional contains a value, false otherwise.
Present() bool

// Get returns the value contained in the Optional and an indicator of whether the Optional is empty.
// If the Optional is empty, the value returned is the zero value of type V.
Get() (V, bool)

// GetOrZero returns the value contained in the Optional, or the zero value of type V if the Optional is empty.
GetOrZero() V

// GetOrDefault returns the value contained in the Optional, or the provided default value if the Optional is empty.
GetOrDefault(defaultValue V) V

// GetOrFunc returns the value contained in the Optional, or the result of calling the provided function if the Optional is empty.
GetOrFunc(func() V) V

// IfPresent calls the provided function with the value contained in the Optional if the Optional is not empty.
// Returns true if the function was called, false otherwise.
IfPresent(func(V)) bool

// IfPresentElse calls the first function with the value contained in the Optional if the Optional is not empty, or the second function otherwise.
// Returns true if the first function was called, false otherwise.
IfPresentElse(func(V), func()) bool

// Filter returns an Optional containing the value contained in the Optional if the provided predicate returns true for that value.
// If the Optional is empty, an empty Optional is returned.
Filter(func(V) bool) Optional[V]
}

// None is an Optional that represents the absence of a value.
type None[V any] struct {
}

func (n None[V]) Present() bool {
return false
}

func (n None[V]) Get() (V, bool) {
var zero V
return zero, false
}

func (n None[V]) GetOrZero() V {
var zero V
return zero
}

func (n None[V]) GetOrDefault(defaultValue V) V {
return defaultValue
}

func (n None[V]) GetOrFunc(f func() V) V {
return f()
}

func (n None[V]) Filter(_ func(V) bool) Optional[V] {
return n
}

func (n None[V]) IfPresent(_ func(V)) bool {
return false
}

func (n None[V]) IfPresentElse(_ func(V), f func()) bool {
f()
return false
}

func (n None[V]) String() string {
return "None"
}

// Some is an Optional that represents the presence of a value.
type Some[V any] struct {
Value V
}

func (s Some[V]) Present() bool {
return true
}

func (s Some[V]) Get() (V, bool) {
return s.Value, true
}

func (s Some[V]) GetOrZero() V {
return s.Value
}

func (s Some[V]) GetOrDefault(_ V) V {
return s.Value
}

func (s Some[V]) GetOrFunc(_ func() V) V {
return s.Value
}

func (s Some[V]) Filter(f func(V) bool) Optional[V] {
if f(s.Value) {
return s
}
return Empty[V]()
}

func (s Some[V]) IfPresent(f func(V)) bool {
f(s.Value)
return true
}

func (s Some[V]) IfPresentElse(f func(V), _ func()) bool {
f(s.Value)
return true
}

func (s Some[V]) String() string {
return fmt.Sprintf("Some(%#v)", s.Value)
}
52 changes: 52 additions & 0 deletions pkg/opt/some.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package opt

import "fmt"

// Some represents the presence of a value.
type Some[V any] struct {
Value V
}

// Assert that Some[V] implements Optional[V]
var _ Optional[any] = Some[any]{}

func (s Some[V]) Present() bool {
return true
}

func (s Some[V]) Get() (V, bool) {
return s.Value, true
}

func (s Some[V]) GetOrZero() V {
return s.Value
}

func (s Some[V]) GetOrDefault(_ V) V {
return s.Value
}

func (s Some[V]) GetOrFunc(_ func() V) V {
return s.Value
}

func (s Some[V]) Filter(f func(V) bool) Optional[V] {
if f(s.Value) {
return s
}
return Empty[V]()
}

func (s Some[V]) IfPresent(f func(V)) bool {
f(s.Value)
return true
}

func (s Some[V]) IfPresentElse(f func(V), _ func()) bool {
f(s.Value)
return true
}

func (s Some[V]) String() string {
return fmt.Sprintf("Some(%#v)", s.Value)
}

0 comments on commit 386f341

Please sign in to comment.