Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
48628c91
authored
Feb 18, 2019
by
Amir Aharon
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add Either for default handling errors
parent
5e1fd73c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
0 deletions
src/main/java/microservice/utils/CheckedFunction.java
src/main/java/microservice/utils/Either.java
src/main/java/microservice/utils/CheckedFunction.java
0 → 100644
View file @
48628c91
package
microservice
.
utils
;
@FunctionalInterface
public
interface
CheckedFunction
<
T
,
R
>
{
R
apply
(
T
t
)
throws
Exception
;
}
src/main/java/microservice/utils/Either.java
0 → 100644
View file @
48628c91
package
microservice
.
utils
;
import
com.google.gdata.util.common.base.Pair
;
import
java.util.Optional
;
import
java.util.function.Function
;
/**
* USE:
* myList.stream()
* .map(Either.lift(item -> doSomething(item))) // or liftWithValue
* .forEach(System.out::println);
* @param <L>
* @param <R>
*/
public
class
Either
<
L
,
R
>
{
private
final
L
left
;
private
final
R
right
;
private
Either
(
L
left
,
R
right
)
{
this
.
left
=
left
;
this
.
right
=
right
;
}
public
static
<
L
,
R
>
Either
<
L
,
R
>
Left
(
L
value
)
{
return
new
Either
(
value
,
null
);
}
public
static
<
L
,
R
>
Either
<
L
,
R
>
Right
(
R
value
)
{
return
new
Either
(
null
,
value
);
}
public
Optional
<
L
>
getLeft
()
{
return
Optional
.
ofNullable
(
left
);
}
public
Optional
<
R
>
getRight
()
{
return
Optional
.
ofNullable
(
right
);
}
public
boolean
isLeft
()
{
return
left
!=
null
;
}
public
boolean
isRight
()
{
return
right
!=
null
;
}
public
<
T
>
Optional
<
T
>
mapLeft
(
Function
<?
super
L
,
T
>
mapper
)
{
if
(
isLeft
())
{
return
Optional
.
of
(
mapper
.
apply
(
left
));
}
return
Optional
.
empty
();
}
public
<
T
>
Optional
<
T
>
mapRight
(
Function
<?
super
R
,
T
>
mapper
)
{
if
(
isRight
())
{
return
Optional
.
of
(
mapper
.
apply
(
right
));
}
return
Optional
.
empty
();
}
public
String
toString
()
{
if
(
isLeft
())
{
return
"Left("
+
left
+
")"
;
}
return
"Right("
+
right
+
")"
;
}
public
static
<
T
,
R
>
Function
<
T
,
Either
>
lift
(
CheckedFunction
<
T
,
R
>
function
)
{
return
t
->
{
try
{
return
Either
.
Right
(
function
.
apply
(
t
));
}
catch
(
Exception
ex
)
{
return
Either
.
Left
(
ex
);
}
};
}
public
static
<
T
,
R
>
Function
<
T
,
Either
>
liftWithValue
(
CheckedFunction
<
T
,
R
>
function
)
{
return
t
->
{
try
{
return
Either
.
Right
(
function
.
apply
(
t
));
}
catch
(
Exception
ex
)
{
return
Either
.
Left
(
Pair
.
of
(
ex
,
t
));
}
};
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment