/*
 * Copyright 1997-2018 Optimatika
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.ojalgo.finance.data;

import java.time.LocalDate;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

import org.ojalgo.array.Primitive64Array;
import org.ojalgo.series.BasicSeries;
import org.ojalgo.type.CalendarDate;
import org.ojalgo.type.CalendarDateUnit;

public final class SourceCache {

    private static final class Value {

        final BasicSeries<LocalDate, Double> series;
        CalendarDate updated = new CalendarDate();
        CalendarDate used = new CalendarDate();

        Value(final String name) {

            super();

            series = BasicSeries.LOCAL_DATE.build(Primitive64Array.FACTORY);
            series.name(name);
        }

    }

    private static final Timer TIMER = new Timer("SourceCache-Daemon", true);

    private final Map<FinanceData, SourceCache.Value> myCache = new ConcurrentHashMap<>();
    private final CalendarDate.Resolution myRefreshInterval;

    public SourceCache(final CalendarDateUnit refreshInterval) {

        super();

        myRefreshInterval = refreshInterval;

        TIMER.schedule(new TimerTask() {

            @Override
            public void run() {
                SourceCache.this.cleanUp();
            }

        }, 0L, refreshInterval.toDurationInMillis());

    }

    public synchronized BasicSeries<LocalDate, Double> get(final FinanceData key) {

        final CalendarDate now = new CalendarDate();

        Value value = myCache.get(key);

        if (value == null) {

            value = new SourceCache.Value(key.getSymbol());

            myCache.put(key, value);

            this.update(value, key, now);

        } else if ((now.millis - value.updated.millis) > myRefreshInterval.toDurationInMillis()) {

            this.update(value, key, now);
        }

        value.used = now;

        return value.series;
    }

    private void cleanUp() {

        final CalendarDate now = new CalendarDate();

        for (final Entry<FinanceData, SourceCache.Value> entry : myCache.entrySet()) {
            if ((now.millis - entry.getValue().used.millis) > myRefreshInterval.toDurationInMillis()) {
                entry.getValue().series.clear();
                myCache.remove(entry.getKey());
            }
        }
    }

    private void update(final Value cacheValue, final FinanceData cacheKey, final CalendarDate now) {
        BasicSeries<LocalDate, Double> priceSeries = cacheKey.getPriceSeries();
        for (Entry<LocalDate, Double> entry : priceSeries.entrySet()) {
            cacheValue.series.put(entry.getKey(), entry.getValue());
        }
        cacheValue.updated = now;
    }
}

@@ -1687,33 +1687,19 @@
 used = n
-ew CalendarDate()
+ull
 ;%0A%0A     
@@ -2043,24 +2043,113 @@
 ashMap%3C%3E();%0A
+    private final Map%3CFinanceData, FinanceData%3E myFallback = new ConcurrentHashMap%3C%3E();%0A%0A
     private 
@@ -2718,68 +2718,33 @@
 che.
-get(key);%0A%0A        if (value == null) %7B%0A%0A            value =
+computeIfAbsent(key, k -%3E
  new
@@ -2763,18 +2763,16 @@
 .Value(k
-ey
 .getSymb
@@ -2776,16 +2776,17 @@
 ymbol())
+)
 ;%0A%0A     
@@ -2792,98 +2792,35 @@
     
-    myCache.put(key, value);%0A%0A            this.update(value, key, now);%0A%0A        %7D else if
+if ((value.used == null) %7C%7C
  ((n
@@ -2894,20 +2894,20 @@
 illis())
+)
  %7B%0A
-%0A
         
@@ -2943,32 +2943,176 @@
 ow);%0A        %7D%0A%0A
+        if ((value.series.size() %3C= 1) && myFallback.containsKey(key)) %7B%0A            return this.get(myFallback.get(key));%0A        %7D else %7B%0A    
         value.us
@@ -3121,17 +3121,20 @@
  = now;%0A
-%0A
+    
         
@@ -3150,24 +3150,344 @@
 lue.series;%0A
+        %7D%0A    %7D%0A%0A    public synchronized void register(FinanceData primary, FinanceData secondary) %7B%0A%0A        myCache.computeIfAbsent(primary, k -%3E new SourceCache.Value(k.getSymbol()));%0A%0A        myCache.computeIfAbsent(secondary, k -%3E new SourceCache.Value(k.getSymbol()));%0A%0A        myFallback.put(primary, secondary);%0A
     %7D%0A%0A    p
@@ -3668,42 +3668,146 @@
     
-if ((now.millis - entry.getValue()
+FinanceData key = entry.getKey();%0A            Value value = entry.getValue();%0A            if ((value.used == null) %7C%7C ((now.millis - value
 .use
@@ -3857,16 +3857,17 @@
 illis())
+)
  %7B%0A     
@@ -3877,32 +3877,21 @@
         
-entry.getV
+v
 alue
-()
 .series.
@@ -3930,30 +3930,19 @@
 .remove(
-entry.getKey()
+key
 );%0A     

Update SourceCache.java