001/*
002  Copyright (c) 2012, 2021, Werner Keil and others by the @author tag.
003
004  Licensed under the Apache License, Version 2.0 (the "License"); you may not
005  use this file except in compliance with the License. You may obtain a copy of
006  the License at
007
008  http://www.apache.org/licenses/LICENSE-2.0
009
010  Unless required by applicable law or agreed to in writing, software
011  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013  License for the specific language governing permissions and limitations under
014  the License.
015 */
016package org.javamoney.moneta.convert.imf;
017
018import java.io.InputStream;
019import java.time.YearMonth;
020import java.util.ArrayList;
021import java.util.EnumMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Objects;
025import java.util.concurrent.ExecutorService;
026import java.util.concurrent.Executors;
027import java.util.concurrent.Future;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031import org.javamoney.moneta.convert.imf.IMFRemoteSearchCallable.IMFRemoteSearchResult;
032
033public enum IMFRemoteSearch {
034        INSTANCE;
035
036        private static final Logger LOG = Logger.getLogger(IMFRemoteSearch.class.getName());
037
038        private final ExecutorService executor = Executors.newCachedThreadPool();
039
040        public Map<IMFHistoricalType, InputStream> getResources(YearMonth yearMonth, String userAgent) {
041                Objects.requireNonNull(yearMonth);
042                if(userAgent==null){
043                        userAgent = IMFAbstractRateProvider.DEFAULT_USER_AGENT;
044                }
045
046                Map<IMFHistoricalType, InputStream> map = new EnumMap<>(IMFHistoricalType.class);
047                                try {
048                        List<Future<IMFRemoteSearchResult>> results = new ArrayList<>(2);
049                        for (IMFHistoricalType type : IMFHistoricalType.values()) {
050                                results.add(executor.submit(new IMFRemoteSearchCallable(type,
051                                                yearMonth, userAgent)));
052                        }
053
054                        for (Future<IMFRemoteSearchResult> result : results) {
055                                IMFRemoteSearchResult imfRemoteSearchResult = result.get();
056                                if (Objects.nonNull(imfRemoteSearchResult)) {
057                                        map.put(imfRemoteSearchResult.getType(),
058                                                        imfRemoteSearchResult.getStream());
059                                }
060                        }
061                } catch (Exception exception) {
062                        LOG.log(Level.WARNING, "Failed to load resource input for find resource from date " + yearMonth, exception);
063                }
064                return map;
065        }
066}