Skip to content

Commit 0efaee6

Browse files
committed
1 parent 90ea43c commit 0efaee6

File tree

6 files changed

+660
-1
lines changed

6 files changed

+660
-1
lines changed

lib-incubator/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
/.resourceCache/

lib-incubator/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
<groupId>org.javamoney.shelter</groupId>
6+
<artifactId>javamoney-shelter</artifactId>
7+
<version>0.5-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>javamoney-lib-incubator</artifactId>
10+
<name>JavaMoney Library Incubator</name>
11+
12+
<properties>
13+
<jsr.version>1.0</jsr.version>
14+
<ri.version>1.0</ri.version>
15+
<!-- dependency versions -->
16+
<testng.version>6.8.5</testng.version>
17+
<additionalparam>-Xdoclint:none</additionalparam>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.testng</groupId>
24+
<artifactId>testng</artifactId>
25+
<version>${testng.version}</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
</dependencyManagement>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>javax.money</groupId>
34+
<artifactId>money-api</artifactId>
35+
<version>${jsr.version}</version>
36+
<scope>compile</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.javamoney</groupId>
40+
<artifactId>moneta</artifactId>
41+
<version>${ri.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>javax.annotation</groupId>
45+
<artifactId>javax.annotation-api</artifactId>
46+
<version>1.2</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>4.8.2</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.testng</groupId>
57+
<artifactId>testng</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
</project>
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright (c) 2015, Werner Keil and others by the @author tag.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.javamoney.lib.incubator.convert;
16+
17+
import java.time.LocalDate;
18+
import java.time.temporal.ChronoUnit;
19+
import java.util.ArrayList;
20+
import java.util.Comparator;
21+
import java.util.List;
22+
import java.util.Objects;
23+
import java.util.function.Predicate;
24+
import java.util.stream.Stream;
25+
26+
import javax.money.CurrencyUnit;
27+
import javax.money.convert.ConversionQuery;
28+
import javax.money.convert.ConversionQueryBuilder;
29+
/**
30+
* Class builder to find exchange rate from historical.
31+
* @see {@link HistoricConversionQueryBuilder#of(CurrencyUnit)}
32+
* @author Otavio Santana
33+
*/
34+
public final class HistoricConversionQueryBuilder {
35+
36+
private final ConversionQueryBuilder conversionQueryBuilder;
37+
38+
private HistoricConversionQueryBuilder(ConversionQueryBuilder conversionQuery) {
39+
this.conversionQueryBuilder = conversionQuery;
40+
}
41+
42+
/**
43+
* Create a {@link HistoricConversionQueryBuilder} from currency
44+
* @param currencyUnit to be used in term currency.
45+
* @return a HistoricConversionQuery from currency
46+
* @throws NullPointerException when currency is null
47+
*/
48+
public static HistoricConversionQueryBuilder of(CurrencyUnit currencyUnit) {
49+
Objects.requireNonNull(currencyUnit, "Currency is required");
50+
return new HistoricConversionQueryBuilder(ConversionQueryBuilder.of()
51+
.setTermCurrency(currencyUnit));
52+
}
53+
54+
/**
55+
* Set a specify day on {@link HistoricConversionQueryBuilder}
56+
* @param localDate
57+
* @return this
58+
* @throws NullPointerException when {@link LocalDate} is null
59+
*/
60+
public HistoricConversionQueryWithDayBuilder withDay(LocalDate localDate) {
61+
Objects.requireNonNull(localDate);
62+
conversionQueryBuilder.set(LocalDate.class, localDate);
63+
64+
return new HistoricConversionQueryWithDayBuilder(conversionQueryBuilder);
65+
}
66+
67+
/**
68+
*Set days on {@link HistoricConversionQueryBuilder} to be used on ExchangeRateProvider,
69+
*these parameters will sort to most recent to be more priority than other.
70+
* @param localDates
71+
* @return this
72+
* @throws IllegalArgumentException when is empty or the parameter has an null value
73+
*/
74+
@SafeVarargs
75+
public final HistoricConversionQueryWithDayBuilder withDays(LocalDate... localDates) {
76+
Objects.requireNonNull(localDates);
77+
if(localDates.length == 0) {
78+
throw new IllegalArgumentException("LocalDates are required");
79+
}
80+
81+
if(Stream.of(localDates).anyMatch(Predicate.isEqual(null))) {
82+
throw new IllegalArgumentException("LocalDates cannot be null");
83+
}
84+
Comparator<LocalDate> comparator = Comparator.naturalOrder();
85+
LocalDate[] sortedDates = Stream.of(localDates).sorted(comparator.reversed()).toArray(LocalDate[]::new);
86+
conversionQueryBuilder.set(LocalDate[].class, sortedDates);
87+
88+
return new HistoricConversionQueryWithDayBuilder(conversionQueryBuilder);
89+
}
90+
91+
/**
92+
*Set days on {@link HistoricConversionQueryBuilder} to be used on ExchangeRateProvider,
93+
*these parameters, different of {@link HistoricConversionQueryBuilder#withDays(LocalDate...)}, consider the order already defined.
94+
* @param localDates
95+
* @return this
96+
* @throws IllegalArgumentException when is empty or the parameter has an null value
97+
*/
98+
@SafeVarargs
99+
public final HistoricConversionQueryWithDayBuilder withDaysPriorityDefined(LocalDate... localDates) {
100+
Objects.requireNonNull(localDates);
101+
if(localDates.length == 0) {
102+
throw new IllegalArgumentException("LocalDates are required");
103+
}
104+
105+
if(Stream.of(localDates).anyMatch(Predicate.isEqual(null))) {
106+
throw new IllegalArgumentException("LocalDates cannot be null");
107+
}
108+
conversionQueryBuilder.set(LocalDate[].class, localDates);
109+
110+
return new HistoricConversionQueryWithDayBuilder(conversionQueryBuilder);
111+
}
112+
113+
/**
114+
* Set the period of days on {@link HistoricConversionQueryBuilder}
115+
* to be used on ExchangeRateProvider,
116+
* @param begin
117+
* @param end
118+
* @return this;
119+
* <p>Example:</p>
120+
* <pre>
121+
* {@code
122+
*LocalDate today = LocalDate.parse("2015-04-03");
123+
*LocalDate yesterday = today.minusDays(1);
124+
*LocalDate tomorrow = today.plusDays(1);
125+
*ConversionQuery query = HistoricConversionQueryBuilder.of(real).onDaysBetween(yesterday, tomorrow).build();//the query with new LocalDate[] {tomorrow, today, yesterday}
126+
* }
127+
* </pre>
128+
* @throws NullPointerException when either begin or end is null
129+
* @throws IllegalArgumentException when the begin is bigger than end
130+
*/
131+
public final HistoricConversionQueryWithDayBuilder withDaysBetween(LocalDate begin, LocalDate end) {
132+
Objects.requireNonNull(begin);
133+
Objects.requireNonNull(end);
134+
if(end.isBefore(begin)) {
135+
throw new IllegalArgumentException("The end period should be bigger than the begin period.");
136+
}
137+
138+
int days = (int) ChronoUnit.DAYS.between(begin, end);
139+
140+
List<LocalDate> dates = new ArrayList<>();
141+
for(int index = days; index >= 0; index--) {
142+
dates.add(begin.plusDays(index));
143+
}
144+
conversionQueryBuilder.set(LocalDate[].class, dates.toArray(new LocalDate[dates.size()]));
145+
146+
return new HistoricConversionQueryWithDayBuilder(conversionQueryBuilder);
147+
}
148+
149+
/**
150+
* Create the {@link ConversionQuery} just with {@link CurrencyUnit}, to term currency, already defined.
151+
* @return the conversion query
152+
*/
153+
public ConversionQuery build() {
154+
return conversionQueryBuilder.build();
155+
}
156+
157+
@Override
158+
public String toString() {
159+
StringBuilder sb = new StringBuilder();
160+
sb.append(HistoricConversionQueryBuilder.class.getName())
161+
.append('{').append(" conversionQueryBuilder: ")
162+
.append(conversionQueryBuilder).append('}');
163+
return sb.toString();
164+
}
165+
166+
public class HistoricConversionQueryWithDayBuilder {
167+
168+
private final ConversionQueryBuilder conversionQueryBuilder;
169+
170+
HistoricConversionQueryWithDayBuilder(
171+
ConversionQueryBuilder conversionQueryBuilder) {
172+
this.conversionQueryBuilder = conversionQueryBuilder;
173+
}
174+
175+
/**
176+
* Create the {@link ConversionQuery} with {@link LocalDate} and {@link CurrencyUnit} to term currency already defined.
177+
* @return the conversion query
178+
*/
179+
public ConversionQuery build() {
180+
return conversionQueryBuilder.build();
181+
}
182+
183+
@Override
184+
public String toString() {
185+
StringBuilder sb = new StringBuilder();
186+
sb.append(HistoricConversionQueryWithDayBuilder.class.getName())
187+
.append('{').append(" conversionQueryBuilder: ")
188+
.append(conversionQueryBuilder).append('}');
189+
return sb.toString();
190+
}
191+
192+
}
193+
194+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package org.javamoney.lib.incubator.convert;
2+
3+
import static org.testng.Assert.assertEquals;
4+
5+
import java.time.LocalDate;
6+
7+
import javax.money.CurrencyUnit;
8+
import javax.money.Monetary;
9+
import javax.money.convert.ConversionQuery;
10+
11+
import org.javamoney.lib.incubator.convert.HistoricConversionQueryBuilder;
12+
import org.testng.annotations.Test;
13+
14+
public final class HistoricConversionQueryTest {
15+
16+
@Test(expectedExceptions = NullPointerException.class)
17+
public void shouldReturnErrorWhendCurrencyIsNull() {
18+
HistoricConversionQueryBuilder.of(null);
19+
}
20+
21+
@Test
22+
public void shouldCreateUsingCurrency() {
23+
CurrencyUnit real = Monetary.getCurrency("BRL");
24+
ConversionQuery query = HistoricConversionQueryBuilder.of(real).build();
25+
assertEquals(query.getCurrency(), real);
26+
}
27+
28+
@Test(expectedExceptions = NullPointerException.class)
29+
public void shouldReturnErrorWhenCreateWithDayUsingLocalTimeNull() {
30+
CurrencyUnit real = Monetary.getCurrency("BRL");
31+
HistoricConversionQueryBuilder.of(real).withDay(null).build();
32+
}
33+
34+
@Test
35+
public void shouldCreateUsingLocalTime() {
36+
CurrencyUnit real = Monetary.getCurrency("BRL");
37+
LocalDate localDate = LocalDate.now();
38+
ConversionQuery query = HistoricConversionQueryBuilder.of(real).withDay(localDate).build();
39+
assertEquals(query.getCurrency(), real);
40+
assertEquals(query.get(LocalDate.class), localDate);
41+
}
42+
43+
44+
@Test(expectedExceptions = IllegalArgumentException.class)
45+
public void shouldReturnErrorWhenCreateWithDaysUsingLocalTimeNull() {
46+
CurrencyUnit real = Monetary.getCurrency("BRL");
47+
LocalDate localDate = null;
48+
HistoricConversionQueryBuilder.of(real).withDays(localDate).build();
49+
}
50+
51+
@Test(expectedExceptions = IllegalArgumentException.class)
52+
public void shouldReturnErrorWhenCreateWithDaysEmptyParameters() {
53+
CurrencyUnit real = Monetary.getCurrency("BRL");
54+
HistoricConversionQueryBuilder.of(real).withDays().build();
55+
}
56+
57+
@Test
58+
public void shouldCreateUsingDays() {
59+
CurrencyUnit real = Monetary.getCurrency("BRL");
60+
LocalDate today = LocalDate.now();
61+
LocalDate yesterday = today.minusDays(1);
62+
LocalDate tomorrow = today.plusDays(1);
63+
ConversionQuery query = HistoricConversionQueryBuilder.of(real).withDays(today, yesterday, tomorrow).build();
64+
assertEquals(query.getCurrency(), real);
65+
assertEquals(query.get(LocalDate[].class), new LocalDate[] { tomorrow, today, yesterday });
66+
}
67+
//
68+
@Test(expectedExceptions = IllegalArgumentException.class)
69+
public void shouldReturnErrorWhenCreateWithDaysPriorityDefinedUsingLocalTimeNull() {
70+
CurrencyUnit real = Monetary.getCurrency("BRL");
71+
LocalDate localDate = null;
72+
HistoricConversionQueryBuilder.of(real).withDaysPriorityDefined(localDate).build();
73+
}
74+
75+
@Test(expectedExceptions = IllegalArgumentException.class)
76+
public void shouldReturnErrorWhenCreateWithDaysPriorityDefinedEmptyParameters() {
77+
CurrencyUnit real = Monetary.getCurrency("BRL");
78+
HistoricConversionQueryBuilder.of(real).withDaysPriorityDefined().build();
79+
}
80+
81+
@Test
82+
public void shouldCreateWithDaysPriorityDefined() {
83+
CurrencyUnit real = Monetary.getCurrency("BRL");
84+
LocalDate today = LocalDate.now();
85+
LocalDate yesterday = today.minusDays(1);
86+
LocalDate tomorrow = today.plusDays(1);
87+
ConversionQuery query = HistoricConversionQueryBuilder.of(real).withDaysPriorityDefined(today, yesterday, tomorrow).build();
88+
assertEquals(query.getCurrency(), real);
89+
assertEquals(query.get(LocalDate[].class), new LocalDate[] { today, yesterday, tomorrow });
90+
}
91+
92+
93+
@Test(expectedExceptions = NullPointerException.class)
94+
public void shouldReturnErrorWhenCreateWithDaysBetweenBeginDateIsNull() {
95+
CurrencyUnit real = Monetary.getCurrency("BRL");
96+
HistoricConversionQueryBuilder.of(real).withDaysBetween(null, LocalDate.now()).build();
97+
}
98+
99+
@Test(expectedExceptions = NullPointerException.class)
100+
public void shouldReturnErrorWhenCreateWithDaysBetweenEndDateIsNull() {
101+
CurrencyUnit real = Monetary.getCurrency("BRL");
102+
HistoricConversionQueryBuilder.of(real).withDaysBetween(LocalDate.now(), null).build();
103+
}
104+
105+
@Test(expectedExceptions = IllegalArgumentException.class)
106+
public void shouldReturnErrorWhenCreateWithDaysBetweenMistakePeriod() {
107+
CurrencyUnit real = Monetary.getCurrency("BRL");
108+
HistoricConversionQueryBuilder.of(real).withDaysBetween(LocalDate.now(), LocalDate.now().minusDays(2)).build();
109+
}
110+
111+
@Test
112+
public void shouldReturnCreateWithDaysBetween() {
113+
CurrencyUnit real = Monetary.getCurrency("BRL");
114+
LocalDate today = LocalDate.now();
115+
LocalDate yesterday = today.minusDays(1);
116+
LocalDate tomorrow = today.plusDays(1);
117+
ConversionQuery query = HistoricConversionQueryBuilder.of(real).withDaysBetween(yesterday, tomorrow).build();
118+
assertEquals(query.getCurrency(), real);
119+
assertEquals(query.get(LocalDate[].class), new LocalDate[] { tomorrow, today, yesterday });
120+
}
121+
}

0 commit comments

Comments
 (0)