Skip to content

Commit 7f57281

Browse files
committed
30: Create Java 9+ examples
Task-Url: #30
1 parent 2e65e9b commit 7f57281

File tree

21 files changed

+898
-2
lines changed

21 files changed

+898
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ sudo: false
22
language: java
33
jdk:
44
- openjdk11
5+
- openjdk14
56

67
after_success:
78
- mvn jacoco:report coveralls:jacoco -DsourceEncoding=UTF-8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
/.settings
3+
/.externalToolBuilders
4+
/bin/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
java functional
2+
==================
3+
4+
JSR 354 Java Functional Examples
5+
6+
same examples using Java 8 with streams, lambda with money-api
7+
8+
---------
9+
10+
- **CreateCurrencyUnit** – The currency unit, it is represented using the CurrencyUnit's interface, it's possible make a instance some forms as inform the currency code or using java.util.Locale of country of origin.
11+
12+
- **CreateMonetaryCurrency** – The monetary value, this class has a strong relationship with a currency. The interface is MonetaryAmount and some implementations are:
13+
Money: implementation that uses BigDecimal to represents the value.
14+
FastMoney: this implementation uses long instead of BigDecimal, it is faster than Money, about 15
15+
times, however has a limited decimals places, just five.
16+
17+
- **FormatExample** – format the money
18+
19+
- **MonetaryFilterOperations** - You may do selection to just one or more currency unit, beyond from a value (greater than, lesser, than, greater and equal than, between, etc.).
20+
21+
- **MonetaryReducerOperations** - Reducer operations such, the greatest value, lesser value, sum.
22+
23+
- **MonetarySorterOperations** - Operation to do sort in a list or stream, so it's possible sort by CurrencyUnit or by value, in asc or desc way, and you can combine, in other words, I may sort by currency unit in ascending way and descending by value.
24+
25+
- **MonetaryGroupOperations** - From Stream or list is possible create a summary (an object that contains informations as size of list, sum, lesser value, greater value and average), a map where the key is CurrencyUnit and the value is a list of MonetaryAmount and a map where the key is CurrencyUnit and the value is a summary of this currency unit.
26+
(currently **broken** because a vital class is no longer public, this example was disabled)
27+
28+
29+
- **SimpleOperations** – simples operations using MonetaryAmount, that included arithmetic operations such sum, subtract and comparative operations such lesser equal, greater equal, etc.
30+

console/functional-example/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>javamoney-examples-console</artifactId>
6+
<groupId>org.javamoney.examples.console</groupId>
7+
<version>1.2-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>functional-example</artifactId>
11+
<properties>
12+
<jdkVersion>1.8</jdkVersion>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.javamoney</groupId>
18+
<artifactId>moneta</artifactId>
19+
<version>${impl.version}</version>
20+
<type>${impl.type}</type>
21+
</dependency>
22+
</dependencies>
23+
<name>JavaMoney Console Examples Using functional in Java 8</name>
24+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2015-2019, Werner Keil, Anatole Tresch
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.functional;
17+
18+
import java.util.Locale;
19+
20+
import javax.money.CurrencyUnit;
21+
import javax.money.MonetaryAmount;
22+
import javax.money.Monetary;
23+
24+
import org.javamoney.moneta.Money;
25+
26+
public class BasicOperations {
27+
28+
public static void main(String[] args) {
29+
CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
30+
MonetaryAmount money = Money.of(120, dollar);
31+
MonetaryAmount money2 = Money.of(50, dollar);
32+
System.out.println(money.add(money2));
33+
System.out.println(money.subtract(money2));
34+
System.out.println(money.multiply(2));
35+
System.out.println(money.divide(2));
36+
System.out.println(money.isEqualTo(money2));
37+
System.out.println(money.isGreaterThan(money2));
38+
System.out.println(money.isGreaterThanOrEqualTo(money2));
39+
System.out.println(money.isGreaterThanOrEqualTo(money2));
40+
System.out.println(money.isLessThan(money2));
41+
System.out.println(money.isLessThanOrEqualTo(money2));
42+
System.out.println(money.isNegative());
43+
System.out.println(money.isNegativeOrZero());
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2015-2019, Werner Keil, Anatole Tresch
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.functional;
17+
18+
import java.util.Locale;
19+
20+
import javax.money.CurrencyUnit;
21+
import javax.money.Monetary;
22+
23+
public class CreateCurrencyUnit
24+
{
25+
public static void main( String[] args )
26+
{
27+
CurrencyUnit real = Monetary.getCurrency("BRL");
28+
CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
29+
System.out.println(real);
30+
System.out.println(dollar);
31+
}
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2015-2019, Werner Keil, Anatole Tresch
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.functional;
17+
18+
import java.util.Locale;
19+
20+
import javax.money.CurrencyUnit;
21+
import javax.money.MonetaryAmount;
22+
import javax.money.Monetary;
23+
24+
import org.javamoney.moneta.FastMoney;
25+
import org.javamoney.moneta.Money;
26+
27+
public class CreateMonetaryCurrency {
28+
29+
public static void main(String[] args) {
30+
CurrencyUnit real = Monetary.getCurrency("BRL");
31+
CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
32+
MonetaryAmount money = Money.of(120, real);
33+
MonetaryAmount fastMoney = FastMoney.of(80, dollar);
34+
System.out.println(money);
35+
System.out.println(fastMoney);
36+
}
37+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2015-2019, Werner Keil, Anatole Tresch
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.functional;
17+
18+
import java.util.Locale;
19+
20+
import javax.money.CurrencyUnit;
21+
import javax.money.MonetaryAmount;
22+
import javax.money.Monetary;
23+
import javax.money.convert.CurrencyConversion;
24+
import javax.money.convert.ExchangeRateProvider;
25+
import javax.money.convert.MonetaryConversions;
26+
27+
import org.javamoney.moneta.Money;
28+
29+
public class ExchangeExample {
30+
31+
public static void main(String[] args) {
32+
ExchangeRateProvider imfRateProvider = MonetaryConversions
33+
.getExchangeRateProvider("IMF");
34+
ExchangeRateProvider ecbRateProvider = MonetaryConversions
35+
.getExchangeRateProvider("ECB");
36+
37+
CurrencyUnit euro = Monetary.getCurrency("EUR");
38+
CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
39+
40+
CurrencyConversion ecbDollarConvertion = ecbRateProvider
41+
.getCurrencyConversion(dollar);
42+
43+
CurrencyConversion imfDollarConvertion = imfRateProvider
44+
.getCurrencyConversion(dollar);
45+
46+
MonetaryAmount money = Money.of(10, euro);
47+
System.out.println(money.with(ecbDollarConvertion));
48+
System.out.println(money.with(imfDollarConvertion));
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2015-2019, Werner Keil, Anatole Tresch
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.functional;
17+
18+
import java.util.Locale;
19+
20+
import javax.money.CurrencyUnit;
21+
import javax.money.MonetaryAmount;
22+
import javax.money.Monetary;
23+
import javax.money.format.AmountFormatQueryBuilder;
24+
import javax.money.format.MonetaryAmountFormat;
25+
import javax.money.format.MonetaryFormats;
26+
27+
import org.javamoney.moneta.Money;
28+
import org.javamoney.moneta.format.CurrencyStyle;
29+
30+
public class FormatExample {
31+
32+
public static void main(String[] args) {
33+
CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
34+
35+
MonetaryAmount monetaryAmount = Money.of(1202.12D, dollar);
36+
MonetaryAmountFormat germanFormat = MonetaryFormats.getAmountFormat(
37+
Locale.GERMANY);
38+
MonetaryAmountFormat usFormat = MonetaryFormats.getAmountFormat(
39+
Locale.US);
40+
MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(
41+
AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.SYMBOL).build());
42+
43+
System.out.println(germanFormat.format(monetaryAmount));//1.202,12 USD
44+
System.out.println(usFormat.format(monetaryAmount));//USD1,202.12
45+
System.out.println(customFormat.format(monetaryAmount));//$1,202.12
46+
}
47+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2015-2019, Werner Keil, Anatole Tresch
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.functional;
17+
18+
import java.math.BigDecimal;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Locale;
22+
import java.util.stream.Collectors;
23+
24+
import javax.money.CurrencyUnit;
25+
import javax.money.MonetaryAmount;
26+
import javax.money.Monetary;
27+
28+
import org.javamoney.moneta.Money;
29+
import org.javamoney.moneta.function.MonetaryFunctions;
30+
31+
public class MonetaryFilterOperations {
32+
private static CurrencyUnit DOLLAR = Monetary.getCurrency(Locale.US);
33+
private static CurrencyUnit EURO = Monetary.getCurrency("EUR");
34+
35+
public static void main(String[] args) {
36+
37+
MonetaryAmount money = Money.of(BigDecimal.valueOf(100D), DOLLAR);
38+
MonetaryAmount min = Money.of(BigDecimal.valueOf(6D), DOLLAR);
39+
MonetaryAmount max = Money.of(BigDecimal.valueOf(100D), DOLLAR);
40+
41+
List<MonetaryAmount> moneys = getMoneys();
42+
43+
List<MonetaryAmount> justDollar = moneys.stream()
44+
.filter((MonetaryFunctions.isCurrency(DOLLAR)))
45+
.collect(Collectors.toList());
46+
List<MonetaryAmount> notEuro = moneys.stream().filter((MonetaryFunctions.isCurrency(EURO))).collect(Collectors.toList());
47+
List<MonetaryAmount> dollarGreaterOneHundred = moneys.stream().filter((MonetaryFunctions.isCurrency(DOLLAR).and(MonetaryFunctions.isGreaterThan(money)))).collect(Collectors.toList());
48+
List<MonetaryAmount> dollarGreaterOneHundredDistinct = moneys.stream().distinct().filter((MonetaryFunctions.isCurrency(DOLLAR).and(MonetaryFunctions.isGreaterThan(money)))).collect(Collectors.toList());
49+
List<MonetaryAmount> between = moneys.stream().filter((MonetaryFunctions.isCurrency(DOLLAR).and(MonetaryFunctions.isBetween(min, max)))).collect(Collectors.toList());
50+
51+
System.out.println(justDollar);
52+
System.out.println(notEuro);
53+
System.out.println(dollarGreaterOneHundred);
54+
System.out.println(dollarGreaterOneHundredDistinct);
55+
System.out.println(between);
56+
}
57+
58+
private static List<MonetaryAmount> getMoneys() {
59+
List<MonetaryAmount> moneys = new ArrayList<>();
60+
moneys.add(Money.of(120, DOLLAR));
61+
moneys.add(Money.of(50, DOLLAR));
62+
moneys.add(Money.of(80, DOLLAR));
63+
moneys.add(Money.of(90, DOLLAR));
64+
moneys.add(Money.of(120, DOLLAR));
65+
66+
moneys.add(Money.of(120, EURO));
67+
moneys.add(Money.of(50, EURO));
68+
moneys.add(Money.of(80, EURO));
69+
moneys.add(Money.of(90, EURO));
70+
moneys.add(Money.of(120, EURO));
71+
return moneys;
72+
}
73+
}

0 commit comments

Comments
 (0)