KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > coalesce > CoalescerFactory


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * 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
24 package com.mchange.v2.coalesce;
25
26 public final class CoalescerFactory
27 {
28     /**
29      * <p>Creates a "Coalescer" that coalesces Objects according to their
30      * equals() method. Given a set of n Objects among whom equals() would
31      * return true, calling coalescer.coalesce() in any order on any sequence
32      * of these Objects will always return a single "canonical" instance.</p>
33      *
34      * <p>This method creates a weak, synchronized coalesecer, safe for use
35      * by multiple Threads.</p>
36      */

37     public static Coalescer createCoalescer()
38     { return createCoalescer( true, true ); }
39
40     /**
41      * <p>Creates a "Coalescer" that coalesces Objects according to their
42      * equals() method. Given a set of n Objects among whom equals() would
43      * return true, calling coalescer.coalesce() in any order on any sequence
44      * of these Objects will always return a single "canonical" instance.</p>
45      *
46      * @param weak if true, the Coalescer will use WeakReferences to hold
47      * its canonical instances, allowing them to be garbage
48      * collected if they are nowhere in use.
49      *
50      * @param synced if true, access to the Coalescer will be automatically
51      * synchronized. if set to false, then users must manually
52      * synchronize access.
53      */

54     public static Coalescer createCoalescer( boolean weak, boolean synced )
55     { return createCoalescer( null, weak, synced ); }
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, boolean weak, boolean synced )
81     {
82     Coalescer out;
83     if ( cc == null )
84         {
85         out = ( weak ?
86             (Coalescer) new WeakEqualsCoalescer() :
87             (Coalescer) new StrongEqualsCoalescer() );
88         }
89     else
90         {
91         out = ( weak ?
92             (Coalescer) new WeakCcCoalescer( cc ) :
93             (Coalescer) new StrongCcCoalescer( cc ) );
94         }
95     return ( synced ? new SyncedCoalescer( out ) : out );
96     }
97
98 }
99
Popular Tags