001/**
002 * Copyright (c) 2012, 2015, Credit Suisse (Anatole Tresch), 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.internal.convert;
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.internal.convert.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) {
041                Objects.requireNonNull(yearMonth);
042
043                Map<IMFHistoricalType, InputStream> map = new EnumMap<>(IMFHistoricalType.class);
044                try {
045                        List<Future<IMFRemoteSearchResult>> results = new ArrayList<>(2);
046                        for (IMFHistoricalType type : IMFHistoricalType.values()) {
047                                results.add(executor.submit(new IMFRemoteSearchCallable(type,
048                                                yearMonth)));
049                        }
050
051                        for (Future<IMFRemoteSearchResult> result : results) {
052                                IMFRemoteSearchResult imfRemoteSearchResult = result.get();
053                                if (Objects.nonNull(imfRemoteSearchResult)) {
054                                        map.put(imfRemoteSearchResult.getType(),
055                                                        imfRemoteSearchResult.getStream());
056                                }
057                        }
058                } catch (Exception exception) {
059                        LOG.log(Level.INFO, "Failed to load resource input for find resource from date " + yearMonth, exception);
060                }
061                return map;
062        }
063
064
065
066}