001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.jcs.jcache.extras.loader;
020
021import org.apache.commons.jcs.jcache.extras.closeable.Closeables;
022
023import javax.cache.configuration.Factory;
024import javax.cache.integration.CacheLoader;
025import javax.cache.integration.CacheLoaderException;
026import java.io.Closeable;
027import java.io.IOException;
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.HashMap;
031import java.util.Map;
032
033public class CompositeCacheLoader<K, V> implements CacheLoader<K, V>, Closeable, Factory<CacheLoader<K, V>>
034{
035    private final CacheLoader<K, V>[] delegates;
036
037    public CompositeCacheLoader(final CacheLoader<K, V>... delegates)
038    {
039        this.delegates = delegates;
040    }
041
042    @Override
043    public V load(final K key) throws CacheLoaderException
044    {
045        for (final CacheLoader<K, V> delegate : delegates)
046        {
047            final V v = delegate.load(key);
048            if (v != null)
049            {
050                return v;
051            }
052        }
053        return null;
054    }
055
056    @Override
057    public Map<K, V> loadAll(final Iterable<? extends K> keys) throws CacheLoaderException
058    {
059        final Collection<K> list = new ArrayList<K>();
060        for (final K k : keys)
061        {
062            list.add(k);
063        }
064
065        final Map<K, V> result = new HashMap<K, V>();
066        for (final CacheLoader<K, V> delegate : delegates)
067        {
068            final Map<K, V> v = delegate.loadAll(list);
069            if (v != null)
070            {
071                result.putAll(v);
072                list.removeAll(v.keySet());
073                if (list.isEmpty())
074                {
075                    return v;
076                }
077            }
078        }
079
080        return result;
081    }
082
083    @Override
084    public void close() throws IOException
085    {
086        Closeables.close(delegates);
087    }
088
089    @Override
090    public CacheLoader<K, V> create()
091    {
092        return this;
093    }
094}