Skip to content

Commit 6b24e40

Browse files
committed
2 parents 2024abc + 0efaee6 commit 6b24e40

File tree

8 files changed

+662
-19
lines changed

8 files changed

+662
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.settings/
33
.idea/
44
*.iml
5-
5+
.project
6+
.classpath

.project

Lines changed: 0 additions & 17 deletions
This file was deleted.

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+
}

0 commit comments

Comments
 (0)