01: /*
02: * Distributed as part of c3p0 v.0.9.1.2
03: *
04: * Copyright (C) 2005 Machinery For Change, Inc.
05: *
06: * Author: Steve Waldman <swaldman@mchange.com>
07: *
08: * This library is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU Lesser General Public License version 2.1, as
10: * published by the Free Software Foundation.
11: *
12: * This software 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
15: * GNU Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public License
18: * along with this software; see the file LICENSE. If not, write to the
19: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: */
22:
23: package com.mchange.v2.coalesce;
24:
25: public final class CoalescerFactory {
26: /**
27: * <p>Creates a "Coalescer" that coalesces Objects according to their
28: * equals() method. Given a set of n Objects among whom equals() would
29: * return true, calling coalescer.coalesce() in any order on any sequence
30: * of these Objects will always return a single "canonical" instance.</p>
31: *
32: * <p>This method creates a weak, synchronized coalesecer, safe for use
33: * by multiple Threads.</p>
34: */
35: public static Coalescer createCoalescer() {
36: return createCoalescer(true, true);
37: }
38:
39: /**
40: * <p>Creates a "Coalescer" that coalesces Objects according to their
41: * equals() method. Given a set of n Objects among whom equals() would
42: * return true, calling coalescer.coalesce() in any order on any sequence
43: * of these Objects will always return a single "canonical" instance.</p>
44: *
45: * @param weak if true, the Coalescer will use WeakReferences to hold
46: * its canonical instances, allowing them to be garbage
47: * collected if they are nowhere in use.
48: *
49: * @param synced if true, access to the Coalescer will be automatically
50: * synchronized. if set to false, then users must manually
51: * synchronize access.
52: */
53: public static Coalescer createCoalescer(boolean weak, boolean synced) {
54: return createCoalescer(null, weak, synced);
55: }
56:
57: /**
58: * <p>Creates a "Coalescer" that coalesces Objects according to the
59: * checkCoalesce() method of a "CoalesceChecker". Given a set of
60: * n Objects among whom calling cc.checkCoalesce() on any pair would
61: * return true, calling coalescer.coalesce() in any order on any sequence
62: * of these Objects will always return a single "canonical" instance.
63: * This allows one to define immutable value Objects whose equals()
64: * method is a mere identity test -- one can use a Coalescer in a
65: * factory method to ensure that no two instances with the same values
66: * are made available to clients.</p>
67: *
68: * @param cc CoalesceChecker that will be used to determine whether two
69: * objects are equivalent and can be coalesced. [If cc is null, then two
70: * objects will be coalesced iff o1.equals( o2 ).]
71: *
72: * @param weak if true, the Coalescer will use WeakReferences to hold
73: * its canonical instances, allowing them to be garbage
74: * collected if they are nowhere in use.
75: *
76: * @param synced if true, access to the Coalescer will be automatically
77: * synchronized. if set to false, then users must manually
78: * synchronize access.
79: */
80: public static Coalescer createCoalescer(CoalesceChecker cc,
81: boolean weak, boolean synced) {
82: Coalescer out;
83: if (cc == null) {
84: out = (weak ? (Coalescer) new WeakEqualsCoalescer()
85: : (Coalescer) new StrongEqualsCoalescer());
86: } else {
87: out = (weak ? (Coalescer) new WeakCcCoalescer(cc)
88: : (Coalescer) new StrongCcCoalescer(cc));
89: }
90: return (synced ? new SyncedCoalescer(out) : out);
91: }
92:
93: }
|