Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ecomod
zmt-jscience
Commits
daa9c139
Commit
daa9c139
authored
Feb 25, 2016
by
mey
Browse files
AmountCloseTo.java: added factory method with custom error
showing unit of first item in description
parent
d22aff50
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/test/java/org/hamcrest/AmountCloseTo.java
View file @
daa9c139
...
...
@@ -2,34 +2,55 @@ package org.hamcrest;
import
static
org
.
hamcrest
.
Matchers
.
closeTo
;
import
org.hamcrest.Description
;
import
org.hamcrest.Factory
;
import
org.hamcrest.Matchers
;
import
org.hamcrest.TypeSafeMatcher
;
import
javax.measure.unit.Unit
;
import
org.hamcrest.number.IsCloseTo
;
import
org.jscience.physics.amount.Amount
;
/**
* {@code IsCloseTo} implementation for {@link Amount}.
*
* @see IsCloseTo
* @author mey
*
*/
public
class
AmountCloseTo
extends
TypeSafeMatcher
<
Amount
<?>>
{
private
static
final
double
DEFAULT_ERROR
=
1
E
-
14
d
;
private
final
Amount
<?>
amount
;
private
static
final
double
MAX_ERROR
=
1
E
-
14
d
;
private
final
double
error
;
AmountCloseTo
(
Class
<?>
expectedType
,
Amount
<?>
amount
)
{
public
AmountCloseTo
(
Class
<?>
expectedType
,
Amount
<?>
amount
,
double
error
)
{
super
(
expectedType
);
this
.
amount
=
amount
;
this
.
error
=
error
;
}
@Override
public
void
describeTo
(
Description
description
)
{
closeTo
(
amount
.
getEstimatedValue
(),
MAX_ERROR
).
describeTo
(
description
);
closeTo
(
amount
.
getEstimatedValue
(),
error
).
describeTo
(
description
);
description
.
appendText
(
" "
).
appendValue
(
amount
.
getUnit
());
}
@Override
protected
boolean
matchesSafely
(
Amount
<?>
item
)
{
return
Matchers
.
closeTo
(
amount
.
getEstimatedValue
(),
MAX_ERROR
)
.
matches
(
item
.
to
(
amount
.
getUnit
()).
getEstimatedValue
());
Unit
<?>
unit
=
amount
.
getUnit
();
return
Matchers
.
closeTo
(
amount
.
getEstimatedValue
(),
error
)
.
matches
(
item
.
to
(
unit
).
getEstimatedValue
());
}
/**
*
* @param amount
* @return {@link Matcher} with default error
*/
@Factory
public
static
Matcher
<
Amount
<?>>
amountCloseTo
(
final
Amount
<?>
amount
)
{
return
amountCloseTo
(
amount
,
DEFAULT_ERROR
);
}
@Factory
public
static
org
.
hamcrest
.
Matcher
<
Amount
<?>>
amountCloseTo
(
final
Amount
<?>
amount
)
{
return
new
AmountCloseTo
(
Amount
.
class
,
amount
);
public
static
Matcher
<
Amount
<?>>
amountCloseTo
(
final
Amount
<?>
amount
,
double
error
)
{
return
new
AmountCloseTo
(
Amount
.
class
,
amount
,
error
);
}
}
\ No newline at end of file
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