001package org.kuali.common.util.properties;
002
003import java.util.Properties;
004
005import org.kuali.common.util.Assert;
006import org.kuali.common.util.cache.Cache;
007import org.kuali.common.util.property.ImmutableProperties;
008
009public final class CachingLoader implements PropertiesLoader {
010
011        private final Cache<String, Properties> cache;
012        private final LocationLoader loader;
013
014        public CachingLoader(Location location, Cache<String, Properties> cache) {
015                this(location, location.getValue(), cache);
016        }
017
018        public CachingLoader(Location location, String value, Cache<String, Properties> cache) {
019                Assert.noNulls(cache);
020                this.cache = cache;
021                this.loader = new LocationLoader(location, value);
022        }
023
024        @Override
025        public Properties load() {
026
027                if (!loader.getLocation().isCacheable()) {
028                        return loader.load();
029                }
030
031                Properties properties = cache.get(loader.getValue());
032                if (properties == null) {
033                        properties = new ImmutableProperties(loader.load());
034                        this.cache.put(loader.getValue(), properties);
035                }
036                return properties;
037
038        }
039
040}