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