Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / persistence / CacheImagePlus.java
blob6811e80df68ff13ddb90a2136cf6e635a87dd202
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2007 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.persistence;
25 import ij.ImagePlus;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Iterator;
30 import java.util.ArrayList;
31 import java.util.Collection;
33 public class CacheImagePlus {
35 /** An access-ordered list of id keys and ImagePlus values. */
36 private final LinkedHashMap<Long,ImagePlus> cache;
38 public CacheImagePlus(int initial_size) {
39 this.cache = new LinkedHashMap<Long,ImagePlus>(initial_size, 0.75f, true);
42 /** No duplicates allowed: if the id exists it's sended to the end and the image is first flushed (if different), then updated with the new one provided. */
43 public final void put(final long id, final ImagePlus imp) {
44 cache.put(id, imp);
47 /** A call to this method puts the element at the end of the list, and returns it. Returns null if not found. */
48 public final ImagePlus get(final long id) {
49 return cache.get(id);
51 /** Remove the Image if found and returns it, without flushing it. Returns null if not found. */
52 public final ImagePlus remove(final long id) {
53 return cache.remove(id);
55 /** Removes and flushes all images, and shrinks arrays. */
56 public final Collection<ImagePlus> removeAll() {
57 final Collection<ImagePlus> all = cache.values();
58 cache.clear();
59 return all;
61 /** Get the id that refers to the given ImagePlus, if any, or Long.MIN_VALUE if not found. */
62 public final long getId(final ImagePlus imp) {
63 if (null == imp) return Long.MIN_VALUE;
64 for (Iterator<Map.Entry<Long,ImagePlus>> it = cache.entrySet().iterator(); it.hasNext(); ) {
65 Map.Entry<Long,ImagePlus> e = it.next();
66 if (e.getValue() == imp) return e.getKey();
68 return Long.MIN_VALUE;
71 public final ImagePlus removeFirst() {
72 if (cache.size() > 0) {
73 final Iterator<ImagePlus> it = cache.values().iterator();
74 final ImagePlus imp = it.next();
75 it.remove();
76 return imp;
78 return null;
81 public final int size() { return cache.size(); }
83 public void debug() {
84 System.out.println("Cache size: " + cache.size());