| 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 or later
5 * as published by the Free Software Foundation. This program is distributed
6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 * the 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 * © CrossWire Bible Society, 2005 - 2016
18 *
19 */
20 package org.crosswire.common.activate;
21
22 import java.util.HashSet;
23 import java.util.Set;
24
25 /**
26 * Manager for instances of Activatable.
27 *
28 * Activator should be used to manage all activate()ions and deactivate()ions so
29 * that it can keep a track of exactly what is active and what can be
30 * deactivated is save memory.
31 *
32 * @see gnu.lgpl.License The GNU Lesser General Public License for details.
33 * @author Joe Walker
34 */
35 public final class Activator {
36 /**
37 * Prevent instantiation
38 */
39 private Activator() {
40 // singleton - no set-up needed
41 }
42
43 /**
44 * Check that a subject is activated and call activate() if not.
45 *
46 * @param subject
47 * The thing to activate
48 */
49 public static void activate(Activatable subject) {
50 if (!activated.contains(subject) && subject != null) {
51 subject.activate(lock);
52 activated.add(subject);
53 }
54 }
55
56 /**
57 * If we need to tighten things up a bit we can save memory with this
58 *
59 * @param amount the amount by which to to reduce memory
60 */
61 public static void reduceMemoryUsage(Kill amount) {
62 amount.reduceMemoryUsage();
63 }
64
65 /**
66 * Deactivate an Activatable object. It is safe to activate() something and
67 * then forget to deactivate() it since we keep a track of activated objects
68 * and will automatically deactivate() when needed, so this method should
69 * only be used when we are sure that something will not be needed again.
70 *
71 * @param subject
72 * The thing to deactivate
73 */
74 public static void deactivate(Activatable subject) {
75 if (activated.contains(subject) && subject != null) {
76 subject.deactivate(lock);
77 activated.remove(subject);
78 }
79 }
80
81 public static void deactivateAll() {
82 for (Activatable item : activated) {
83 deactivate(item);
84 }
85 }
86
87 /**
88 * The list of things that we have activated
89 */
90 private static Set<Activatable> activated = new HashSet<Activatable>();
91
92 /**
93 * The object we use to prevent others from
94 */
95 private static Lock lock = new Lock();
96 }
97