| Line | Hits | Source |
|---|---|---|
| 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 | */ | |
| 32 | 0 | public class IteratorFactory |
| 33 | { | |
| 34 | 0 | protected static Map collection_data = new HashMap(); |
| 35 | ||
| 36 | public static Iterator getIterator(Collection c) | |
| 37 | { | |
| 38 | 0 | IteratorData id = (IteratorData)collection_data.get(c); |
| 39 | 0 | if (id == null) |
| 40 | 0 | return c.iterator(); |
| 41 | ||
| 42 | 0 | return id.getBackingCollection().iterator(); |
| 43 | } | |
| 44 | ||
| 45 | public static Iterator getIterator(Collection c, Comparator comp, Predicate p) | |
| 46 | { | |
| 47 | 0 | IteratorData id = new IteratorData(c, comp, p, true); |
| 48 | 0 | return id.getBackingCollection().iterator(); |
| 49 | } | |
| 50 | ||
| 51 | public static Iterator getIterator(Collection c, Comparator comp) | |
| 52 | { | |
| 53 | 0 | IteratorData id = new IteratorData(c, comp, null, true); |
| 54 | 0 | return id.getBackingCollection().iterator(); |
| 55 | } | |
| 56 | ||
| 57 | public static Iterator getIterator(Collection c, Predicate p) | |
| 58 | { | |
| 59 | 0 | IteratorData id = new IteratorData(c, null, p, true); |
| 60 | 0 | return id.getBackingCollection().iterator(); |
| 61 | } | |
| 62 | ||
| 63 | public static void addCollection(Collection c, Comparator comp, Predicate p, boolean dynamic) | |
| 64 | { | |
| 65 | 0 | IteratorData id = new IteratorData(c, comp, p, dynamic); |
| 66 | 0 | collection_data.put(c, id); |
| 67 | 0 | } |
| 68 | ||
| 69 | public static void addCollection(Collection c, Comparator comp) | |
| 70 | { | |
| 71 | 0 | addCollection(c, comp, null, false); |
| 72 | 0 | } |
| 73 | ||
| 74 | public static void addCollection(Collection c, Predicate p) | |
| 75 | { | |
| 76 | 0 | addCollection(c, null, p, false); |
| 77 | 0 | } |
| 78 | ||
| 79 | public static void addCollection(Collection c, Comparator comp, Predicate p) | |
| 80 | { | |
| 81 | 0 | addCollection(c, comp, p, false); |
| 82 | 0 | } |
| 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 | { | |
| 97 | 0 | IteratorData id = (IteratorData)collection_data.get(c); |
| 98 | 0 | id.setDynamic(dynamic); |
| 99 | 0 | } |
| 100 | ||
| 101 | public static void setComparator(Collection c, Comparator comp) | |
| 102 | { | |
| 103 | 0 | IteratorData id = (IteratorData)collection_data.get(c); |
| 104 | 0 | id.setComparator(comp); |
| 105 | 0 | } |
| 106 | ||
| 107 | public static void setPredicate(Collection c, Predicate p) | |
| 108 | { | |
| 109 | 0 | IteratorData id = (IteratorData)collection_data.get(c); |
| 110 | 0 | id.setPredicate(p); |
| 111 | 0 | } |
| 112 | ||
| 113 | public static void clear() | |
| 114 | { | |
| 115 | 0 | collection_data.clear(); |
| 116 | 0 | } |
| 117 | ||
| 118 | public static void removeCollection(Collection c) | |
| 119 | { | |
| 120 | 0 | collection_data.remove(c); |
| 121 | 0 | } |
| 122 | ||
| 123 | 0 | 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. |
copyright © 2003, jcoverage ltd. all rights reserved. |