1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.datastructures.cache;
14 import java.lang.ref.SoftReference;
17 * Caches the value provided by provider until the value has been garbage collected.
19 * Cached IProvider provides the same instance as long as it is in memory.
21 * Cached version is equal to original in hashCode/equals wise.
23 * @author Toni Kalajainen
25 public class SoftCachedProvider<V> implements IProvider<V> {
27 public static final <V> IProvider<V> cache(IProvider<V> provider)
29 if (provider instanceof SoftCachedProvider<?>)
31 return new SoftCachedProvider<V>(provider);
34 IProvider<V> provider;
37 SoftCachedProvider(IProvider<V> orig)
43 public synchronized V get() throws ProvisionException {
46 if (x==null) ref=null;
50 ref = new SoftReference<V>(x);
55 public int hashCode() {
56 return provider.hashCode();
60 public boolean equals(Object obj) {
61 return provider.equals(obj);