01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2007, Geotools Project Managment Committee (PMC)
05: * (C) 2007, Geomatys
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: */
17: package org.geotools.image.io;
18:
19: import java.awt.image.ColorModel;
20: import java.lang.ref.ReferenceQueue;
21: import java.lang.ref.WeakReference;
22: import java.util.Set;
23:
24: /**
25: * Allows garbage-collection of {@link Palette} after their index color model has been
26: * garbage collected.
27: *
28: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/PaletteDisposer.java $
29: * @version $Id: PaletteDisposer.java 25587 2007-05-19 18:51:57Z desruisseaux $
30: * @author Martin Desruisseaux
31: */
32: final class PaletteDisposer extends Thread {
33: /**
34: * The reference queue.
35: */
36: private static final ReferenceQueue/*<ColorModel>*/queue = new ReferenceQueue();
37:
38: /**
39: * A weak reference to a color model created by a palette.
40: */
41: static final class Reference extends WeakReference/*<ColorModel>*/{
42: /**
43: * Starts the disposer thread when the {@link Reference} are about to be created.
44: */
45: static {
46: new PaletteDisposer().start();
47: }
48:
49: /**
50: * The palette that created the color model.
51: */
52: final Palette palette;
53:
54: /**
55: * Creates the weak reference for the specified color model.
56: */
57: public Reference(final Palette palette, final ColorModel colors) {
58: super (colors, queue);
59: this .palette = palette;
60: final Set protectedPalettes = palette.factory.protectedPalettes;
61: synchronized (protectedPalettes) {
62: protectedPalettes.add(palette);
63: }
64: }
65: }
66:
67: /**
68: * Creates a new disposer thread.
69: */
70: private PaletteDisposer() {
71: super ("PaletteDisposer");
72: setDaemon(true);
73: }
74:
75: /**
76: * Removes the palette from the set of protected ones.
77: */
78: //@Override
79: public void run() {
80: while (true) {
81: final Reference ref;
82: try {
83: ref = (Reference) queue.remove();
84: } catch (InterruptedException e) {
85: continue;
86: }
87: final Set protectedPalettes = ref.palette.factory.protectedPalettes;
88: synchronized (protectedPalettes) {
89: protectedPalettes.remove(ref.palette);
90: }
91: }
92: }
93: }
|