-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathEither.java
More file actions
116 lines (101 loc) · 2.77 KB
/
Either.java
File metadata and controls
116 lines (101 loc) · 2.77 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.laytonsmith.PureUtilities;
import java.util.Objects;
import java.util.Optional;
/**
* The {@code Either} class can contain neither of, or one of either the specified types of values.
* @param <L> The first possible type.
* @param <R> The second possible type.
*/
public final class Either<L, R> {
/**
* Creates a new Either object with the first possible type.
* @param <L> The first possible type, which is the type of the input parameter.
* @param <R> The other possible type, which this value will not contain.
* @param value The value to store.
* @return A new Either object.
*/
public static <L, R> Either<L, R> left(L value) {
return new Either<>(Optional.of(value), Optional.empty());
}
/**
* Creates a new Either object with the second possible type.
* @param <L> The other possible type, which this value will not contain.
* @param <R> The second possible type, which is the type of the input parameter.
* @param value The value to store.
* @return A new Either object.
*/
public static <L, R> Either<L, R> right(R value) {
return new Either<>(Optional.empty(), Optional.of(value));
}
/**
* Creates a new Either object, which does not contain any value.
* @param <L> The first possible type.
* @param <R> The second possible type.
* @return A new, empty, Either object.
*/
public static <L, R> Either<L, R> neither() {
return new Either<>(Optional.empty(), Optional.empty());
}
private final Optional<L> left;
private final Optional<R> right;
private Either(Optional<L> left, Optional<R> right) {
this.left = left;
this.right = right;
}
/**
* Returns the left value. May be empty, even if the other value is also empty.
* @return
*/
public Optional<L> getLeft() {
return this.left;
}
/**
* Returns the left value. May be empty, even if the other value is also empty.
* @return
*/
public Optional<R> getRight() {
return this.right;
}
/**
* Returns if there was a left side.
* @return
*/
public boolean hasLeft() {
return this.left.isPresent();
}
/**
* Returns if there was a right side.
* @return
*/
public boolean hasRight() {
return this.right.isPresent();
}
@Override
public int hashCode() {
int hash = 3;
hash = 19 * hash + Objects.hashCode(this.left);
hash = 19 * hash + Objects.hashCode(this.right);
return hash;
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
final Either<?, ?> other = (Either<?, ?>) obj;
if(!Objects.equals(this.left, other.left)) {
return false;
}
return Objects.equals(this.right, other.right);
}
@Override
public String toString() {
return "Either{" + "left=" + left + ", right=" + right + '}';
}
}