| Activator.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: Activator.java 2053 2010-12-10 19:34:53Z dmsmith $
21 */
22 package org.crosswire.common.activate;
23
24 import java.util.HashSet;
25 import java.util.Set;
26
27 /**
28 * Manager for instances of Activatable.
29 *
30 * Activator should be used to manage all activate()ions and deactivate()ions so
31 * that it can keep a track of exactly what is active and what can be
32 * deactivate()d is save memory.
33 *
34 * @see gnu.lgpl.License for license details.<br>
35 * The copyright to this program is held by it's authors.
36 * @author Joe Walker [joe at eireneh dot com]
37 */
38 public final class Activator {
39 /**
40 * Prevent instantiation
41 */
42 private Activator() {
43 // singleton - no set-up needed
44 }
45
46 /**
47 * Check that a subject is activated and call activate() if not.
48 *
49 * @param subject
50 * The thing to activate
51 */
52 public static void activate(Activatable subject) {
53 if (!activated.contains(subject) && subject != null) {
54 subject.activate(lock);
55 activated.add(subject);
56 }
57 }
58
59 /**
60 * If we need to tighten things up a bit we can save memory with this
61 */
62 public static void reduceMemoryUsage(Kill amount) {
63 amount.reduceMemoryUsage();
64 }
65
66 /**
67 * Deactivate an Activatable object. It is safe to activate() something and
68 * then forget to deactivate() it since we keep a track of activated objects
69 * and will automatically deactivate() when needed, so this method should
70 * only be used when we are sure that something will not be needed again.
71 *
72 * @param subject
73 * The thing to deactivate
74 */
75 public static void deactivate(Activatable subject) {
76 if (activated.contains(subject) && subject != null) {
77 subject.deactivate(lock);
78 activated.remove(subject);
79 }
80 }
81
82 public static void deactivateAll() {
83 for (Activatable item : activated) {
84 deactivate(item);
85 }
86 }
87
88 /**
89 * The list of things that we have activated
90 */
91 private static Set<Activatable> activated = new HashSet<Activatable>();
92
93 /**
94 * The object we use to prevent others from
95 */
96 private static Lock lock = new Lock();
97 }
98