Coverage details for edu.uci.ics.jung.utils.CollectionFactory

LineHitsSource
1 /*
2  * Created on Apr 18, 2006
3  *
4  * Copyright (c) 2006, the JUNG Project and the Regents of the University
5  * of California
6  * All rights reserved.
7  *
8  * This software is open-source under the BSD license; see either
9  * "license.txt" or
10  * http://jung.sourceforge.net/license.txt for a description.
11  */
12 package edu.uci.ics.jung.utils;
13  
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22  
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.functors.TruePredicate;
25  
26 /**
27  * Generates <code>Collection</code>s based on input <code>Collection</code> instances,
28  * optionally filtered and sorted according to specified parameters.
29  *
30  * @author Joshua O'Madadhain
31  */
320public class CollectionFactory
33 {
340    protected static Map collection_data = new HashMap();
35  
36     public static Collection getCollection(Collection c)
37     {
380        CollectionData cd = (CollectionData)collection_data.get(c);
390        if (cd == null)
400            return c;
41         
420        return cd.getBackingCollection();
43     }
44  
45     public static Collection getCollection(Collection c, Comparator comp, Predicate p)
46     {
470        CollectionData cd = new CollectionData(c, comp, p, true);
480        return cd.getBackingCollection();
49     }
50     
51     public static Collection getCollection(Collection c, Comparator comp)
52     {
530        CollectionData cd = new CollectionData(c, comp, null, true);
540        return cd.getBackingCollection();
55     }
56     
57     public static Collection getCollection(Collection c, Predicate p)
58     {
590        CollectionData id = new CollectionData(c, null, p, true);
600        return id.getBackingCollection();
61     }
62  
63     public static void addCollection(Collection c, Comparator comp, Predicate p, boolean dynamic)
64     {
650        CollectionData id = new CollectionData(c, comp, p, dynamic);
660        collection_data.put(c, id);
670    }
68  
69     public static void addCollection(Collection c, Comparator comp)
70     {
710        addCollection(c, comp, null, false);
720    }
73     
74     public static void addCollection(Collection c, Predicate p)
75     {
760        addCollection(c, null, p, false);
770    }
78  
79     public static void addCollection(Collection c, Comparator comp, Predicate p)
80     {
810        addCollection(c, comp, p, false);
820    }
83     
84     /**
85      * If <code>dynamic</code> is true, the collection <code>c</code> backing the
86      * <code>Iterator</code> is automatically rebuilt-sorted
87      * and/or re-filtered each time <code>getIterator(c)</code> is called.
88      * (This is done in case either the collection, the comparator,
89      * or the predicate has changed.)
90      * Otherwise, the collection is (re)built only when
91      * <code>buildIterator</code> is called.
92      *
93      *
94      */
95     public static void setDynamic(Collection c, boolean dynamic)
96     {
970        CollectionData id = (CollectionData)collection_data.get(c);
980        id.setDynamic(dynamic);
990    }
100     
101     public static void setComparator(Collection c, Comparator comp)
102     {
1030        CollectionData id = (CollectionData)collection_data.get(c);
1040        id.setComparator(comp);
1050    }
106     
107     public static void setPredicate(Collection c, Predicate p)
108     {
1090        CollectionData id = (CollectionData)collection_data.get(c);
1100        id.setPredicate(p);
1110    }
112  
113     public static void clear()
114     {
1150        collection_data.clear();
1160    }
117     
118     public static void removeCollection(Collection c)
119     {
1200        collection_data.remove(c);
1210    }
122     
1230    protected static class CollectionData
124     {
125         /**
126          * If <code>is_dynamic</code> is true, the backing collection is automatically re-sorted
127          * and/or re-filtered each time an <code>Iterator</code> is requested.
128          * (This is done in case either the collection, the comparator,
129          * or the predicate has changed.)
130          * Otherwise, the collection is (re)built only when
131          * <code>buildBackingCollection</code> is called.
132          */
133         protected boolean is_dynamic;
134  
135         protected Collection collection;
136         protected Comparator comp;
137         protected Collection backing_collection;
138         protected Predicate p;
139         
140         public CollectionData(Collection c, Comparator comp, Predicate p, boolean dynamic)
141         {
142             this.collection = c;
143             this.comp = comp;
144             this.p = p;
145             this.is_dynamic = dynamic;
146         }
147  
148         public void setComparator(Comparator comp)
149         {
150             if (! this.comp.equals(comp))
151                 this.backing_collection = null;
152             this.comp = comp;
153         }
154  
155         public void setPredicate(Predicate p)
156         {
157             if (! this.p.equals(p))
158                 this.backing_collection = null;
159             this.p = p;
160         }
161  
162         public void setDynamic(boolean dynamic)
163         {
164             if (! dynamic)
165                 this.backing_collection = null;
166             this.is_dynamic = dynamic;
167         }
168         
169         protected Collection getBackingCollection()
170         {
171             if (is_dynamic)
172                 return buildBackingCollection(collection, p, comp);
173             else
174             {
175                 if (backing_collection == null)
176                     this.backing_collection = buildBackingCollection(collection, p, comp);
177                      
178                 return backing_collection;
179             }
180         }
181  
182         protected List buildBackingCollection(Collection c, Predicate p, Comparator comp)
183         {
184             List new_backing_collection = new ArrayList(c.size());
185  
186             if (p != null && p != TruePredicate.getInstance())
187             {
188                 for (Iterator iter = c.iterator(); iter.hasNext(); )
189                 {
190                     Object o = iter.next();
191                     if (p.evaluate(o))
192                         new_backing_collection.add(o);
193                 }
194             }
195  
196             if (comp != null)
197                 Collections.sort(new_backing_collection, comp);
198  
199             return new_backing_collection;
200         }
201     }
202 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.