| 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>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 | */ | |
| 32 | 0 | public class CollectionFactory |
| 33 | { | |
| 34 | 0 | protected static Map collection_data = new HashMap(); |
| 35 | ||
| 36 | public static Collection getCollection(Collection c) | |
| 37 | { | |
| 38 | 0 | CollectionData cd = (CollectionData)collection_data.get(c); |
| 39 | 0 | if (cd == null) |
| 40 | 0 | return c; |
| 41 | ||
| 42 | 0 | return cd.getBackingCollection(); |
| 43 | } | |
| 44 | ||
| 45 | public static Collection getCollection(Collection c, Comparator comp, Predicate p) | |
| 46 | { | |
| 47 | 0 | CollectionData cd = new CollectionData(c, comp, p, true); |
| 48 | 0 | return cd.getBackingCollection(); |
| 49 | } | |
| 50 | ||
| 51 | public static Collection getCollection(Collection c, Comparator comp) | |
| 52 | { | |
| 53 | 0 | CollectionData cd = new CollectionData(c, comp, null, true); |
| 54 | 0 | return cd.getBackingCollection(); |
| 55 | } | |
| 56 | ||
| 57 | public static Collection getCollection(Collection c, Predicate p) | |
| 58 | { | |
| 59 | 0 | CollectionData id = new CollectionData(c, null, p, true); |
| 60 | 0 | return id.getBackingCollection(); |
| 61 | } | |
| 62 | ||
| 63 | public static void addCollection(Collection c, Comparator comp, Predicate p, boolean dynamic) | |
| 64 | { | |
| 65 | 0 | CollectionData id = new CollectionData(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(Collection c, boolean dynamic) | |
| 96 | { | |
| 97 | 0 | CollectionData id = (CollectionData)collection_data.get(c); |
| 98 | 0 | id.setDynamic(dynamic); |
| 99 | 0 | } |
| 100 | ||
| 101 | public static void setComparator(Collection c, Comparator comp) | |
| 102 | { | |
| 103 | 0 | CollectionData id = (CollectionData)collection_data.get(c); |
| 104 | 0 | id.setComparator(comp); |
| 105 | 0 | } |
| 106 | ||
| 107 | public static void setPredicate(Collection c, Predicate p) | |
| 108 | { | |
| 109 | 0 | CollectionData id = (CollectionData)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 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. |
copyright © 2003, jcoverage ltd. all rights reserved. |