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

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>Iterator</code>s for specified <code>Collection</code>s,
28  * optionally filtered and sorted according to specified parameters.
29  *
30  * @author Joshua O'Madadhain
31  */
320public class IteratorFactory
33 {
340    protected static Map collection_data = new HashMap();
35  
36     public static Iterator getIterator(Collection c)
37     {
380        IteratorData id = (IteratorData)collection_data.get(c);
390        if (id == null)
400            return c.iterator();
41         
420        return id.getBackingCollection().iterator();
43     }
44  
45     public static Iterator getIterator(Collection c, Comparator comp, Predicate p)
46     {
470        IteratorData id = new IteratorData(c, comp, p, true);
480        return id.getBackingCollection().iterator();
49     }
50     
51     public static Iterator getIterator(Collection c, Comparator comp)
52     {
530        IteratorData id = new IteratorData(c, comp, null, true);
540        return id.getBackingCollection().iterator();
55     }
56     
57     public static Iterator getIterator(Collection c, Predicate p)
58     {
590        IteratorData id = new IteratorData(c, null, p, true);
600        return id.getBackingCollection().iterator();
61     }
62  
63     public static void addCollection(Collection c, Comparator comp, Predicate p, boolean dynamic)
64     {
650        IteratorData id = new IteratorData(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(boolean dynamic, Collection c)
96     {
970        IteratorData id = (IteratorData)collection_data.get(c);
980        id.setDynamic(dynamic);
990    }
100     
101     public static void setComparator(Collection c, Comparator comp)
102     {
1030        IteratorData id = (IteratorData)collection_data.get(c);
1040        id.setComparator(comp);
1050    }
106     
107     public static void setPredicate(Collection c, Predicate p)
108     {
1090        IteratorData id = (IteratorData)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 IteratorData
124     {
125         /**
126          * If <code>is_dynamic</code> is true, the collection used to backing the
127          * <code>Iterator</code> is automatically re-sorted
128          * and/or re-filtered each time an <code>Iterator</code> is requested.
129          * (This is done in case either the collection, the comparator,
130          * or the predicate has changed.)
131          * Otherwise, the collection is (re)built only when
132          * <code>buildIterator</code> is called.
133          */
134         protected boolean is_dynamic;
135  
136         protected Collection collection;
137         protected Comparator comp;
138         protected Collection backing_collection;
139         protected Predicate p;
140         
141         public IteratorData(Collection c, Comparator comp, Predicate p, boolean dynamic)
142         {
143             this.collection = c;
144             this.comp = comp;
145             this.p = p;
146             this.is_dynamic = dynamic;
147         }
148  
149         public void setComparator(Comparator comp)
150         {
151             if (! this.comp.equals(comp))
152                 this.backing_collection = null;
153             this.comp = comp;
154         }
155  
156         public void setPredicate(Predicate p)
157         {
158             if (! this.p.equals(p))
159                 this.backing_collection = null;
160             this.p = p;
161         }
162  
163         public void setDynamic(boolean dynamic)
164         {
165             if (! dynamic)
166                 this.backing_collection = null;
167             this.is_dynamic = dynamic;
168         }
169         
170         protected Collection getBackingCollection()
171         {
172             if (is_dynamic)
173                 return buildBackingCollection(collection, p, comp);
174             else
175             {
176                 if (backing_collection == null)
177                     this.backing_collection = buildBackingCollection(collection, p, comp);
178                      
179                 return backing_collection;
180             }
181         }
182  
183         protected List buildBackingCollection(Collection c, Predicate p, Comparator comp)
184         {
185             List to_iterate = new ArrayList(c.size());
186  
187             if (p != null && p != TruePredicate.getInstance())
188             {
189                 for (Iterator iter = c.iterator(); iter.hasNext(); )
190                 {
191                     Object o = iter.next();
192                     if (p.evaluate(o))
193                         to_iterate.add(o);
194                 }
195             }
196  
197             if (comp != null)
198                 Collections.sort(to_iterate, comp);
199  
200             return to_iterate;
201         }
202     }
203 }

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.