Checking params for null.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/openide.util/src/org/openide/util/Enumerations.java Wed Jan 27 17:46:23 2010 -0500
1.3 @@ -0,0 +1,601 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
1.8 + *
1.9 + * The contents of this file are subject to the terms of either the GNU
1.10 + * General Public License Version 2 only ("GPL") or the Common
1.11 + * Development and Distribution License("CDDL") (collectively, the
1.12 + * "License"). You may not use this file except in compliance with the
1.13 + * License. You can obtain a copy of the License at
1.14 + * http://www.netbeans.org/cddl-gplv2.html
1.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 + * specific language governing permissions and limitations under the
1.17 + * License. When distributing the software, include this License Header
1.18 + * Notice in each file and include the License file at
1.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 + * particular file as subject to the "Classpath" exception as provided
1.21 + * by Sun in the GPL Version 2 section of the License file that
1.22 + * accompanied this code. If applicable, add the following below the
1.23 + * License Header, with the fields enclosed by brackets [] replaced by
1.24 + * your own identifying information:
1.25 + * "Portions Copyrighted [year] [name of copyright owner]"
1.26 + *
1.27 + * Contributor(s):
1.28 + *
1.29 + * The Original Software is NetBeans. The Initial Developer of the Original
1.30 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
1.31 + * Microsystems, Inc. All Rights Reserved.
1.32 + *
1.33 + * If you wish your version of this file to be governed by only the CDDL
1.34 + * or only the GPL Version 2, indicate your decision by adding
1.35 + * "[Contributor] elects to include this software in this distribution
1.36 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.37 + * single choice of license, a recipient has the option to distribute
1.38 + * your version of this file under either the CDDL, the GPL Version 2 or
1.39 + * to extend the choice of license to its licensees as provided above.
1.40 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.41 + * Version 2 license, then the option applies only if the new code is
1.42 + * made subject to such option by the copyright holder.
1.43 + */
1.44 +
1.45 +package org.openide.util;
1.46 +
1.47 +import java.util.ArrayList;
1.48 +import java.util.Arrays;
1.49 +import java.util.Collection;
1.50 +import java.util.Collections;
1.51 +import java.util.Enumeration;
1.52 +import java.util.HashSet;
1.53 +import java.util.Iterator;
1.54 +import java.util.NoSuchElementException;
1.55 +import java.util.Set;
1.56 +
1.57 +/**
1.58 + * Factory methods for various types of {@link Enumeration}.
1.59 + * Allows composition of existing enumerations, filtering their contents, and/or modifying them.
1.60 + * All of this is designed to be done lazily, i.e. elements created on demand.
1.61 + * @since 4.37
1.62 + * @author Jaroslav Tulach
1.63 + * @see NbCollections#checkedEnumerationByFilter
1.64 + * @see NbCollections#iterable(Enumeration)
1.65 + */
1.66 +public final class Enumerations extends Object {
1.67 + /** No instances */
1.68 + private Enumerations() {
1.69 + }
1.70 +
1.71 + /**
1.72 + * An empty enumeration.
1.73 + * Always returns <code>false</code> from
1.74 + * <code>empty().hasMoreElements()</code> and throws <code>NoSuchElementException</code>
1.75 + * from <code>empty().nextElement()</code>.
1.76 + * @return the enumeration
1.77 + */
1.78 + public static final <T> Enumeration<T> empty() {
1.79 + Collection<T> emptyL = Collections.emptyList();
1.80 + return Collections.enumeration(emptyL);
1.81 + }
1.82 +
1.83 + /**
1.84 + * Creates an enumeration with one element.
1.85 + * @param obj the element to be present in the enumeration.
1.86 + * @return enumeration
1.87 + */
1.88 + public static <T> Enumeration<T> singleton(T obj) {
1.89 + return Collections.enumeration(Collections.singleton(obj));
1.90 + }
1.91 +
1.92 + /**
1.93 + * Concatenates the content of two enumerations into one.
1.94 + * Until the
1.95 + * end of <code>en1</code> is reached its elements are being served.
1.96 + * As soon as the <code>en1</code> has no more elements, the content
1.97 + * of <code>en2</code> is being returned.
1.98 + *
1.99 + * @param en1 first enumeration
1.100 + * @param en2 second enumeration
1.101 + * @return enumeration
1.102 + */
1.103 + public static <T> Enumeration<T> concat(Enumeration<? extends T> en1, Enumeration<? extends T> en2) {
1.104 + ArrayList<Enumeration<? extends T>> two = new ArrayList<Enumeration<? extends T>>();
1.105 + two.add(en1);
1.106 + two.add(en2);
1.107 + return new SeqEn<T>(Collections.enumeration(two));
1.108 + }
1.109 +
1.110 + /**
1.111 + * Concatenates the content of many enumerations.
1.112 + * The input value
1.113 + * is enumeration of Enumeration elements and the result is composed
1.114 + * all their content. Each of the provided enumeration is fully read
1.115 + * and their content returned before the next enumeration is asked for
1.116 + * their elements.
1.117 + *
1.118 + * @param enumOfEnums Enumeration of Enumeration elements
1.119 + * @return enumeration
1.120 + */
1.121 + public static <T> Enumeration<T> concat(Enumeration<? extends Enumeration<? extends T>> enumOfEnums) {
1.122 + return new SeqEn<T>(enumOfEnums);
1.123 + }
1.124 +
1.125 + /**
1.126 + * Filters the input enumeration to new one that should contain
1.127 + * each of the provided elements just once.
1.128 + * The elements are compared
1.129 + * using their default <code>equals</code> and <code>hashCode</code> methods.
1.130 + *
1.131 + * @param en enumeration to filter
1.132 + * @return enumeration without duplicated items
1.133 + */
1.134 + public static <T> Enumeration<T> removeDuplicates(Enumeration<T> en) {
1.135 + class RDupls implements Processor<T,T> {
1.136 + private Set<T> set = new HashSet<T>();
1.137 +
1.138 + public T process(T o, Collection<T> nothing) {
1.139 + return set.add(o) ? o : null;
1.140 + }
1.141 + }
1.142 +
1.143 + return filter(en, new RDupls());
1.144 + }
1.145 +
1.146 + /**
1.147 + * Returns an enumeration that iterates over provided array.
1.148 + * @param arr the array of object
1.149 + * @return enumeration of those objects
1.150 + */
1.151 + public static <T> Enumeration<T> array(T... arr) {
1.152 + return Collections.enumeration(Arrays.asList(arr));
1.153 + }
1.154 +
1.155 + /**
1.156 + * Removes all <code>null</code>s from the input enumeration.
1.157 + * @param en enumeration that can contain nulls
1.158 + * @return new enumeration without null values
1.159 + */
1.160 + public static <T> Enumeration<T> removeNulls(Enumeration<T> en) {
1.161 + return filter(en, new RNulls<T>());
1.162 + }
1.163 +
1.164 + /**
1.165 + * For each element of the input enumeration <code>en</code> asks the
1.166 + * {@link Processor} to provide a replacement.
1.167 + * The <code>toAdd</code> argument of the processor is always null.
1.168 + * <p>
1.169 + * Example to convert any objects into strings:
1.170 + * <pre>
1.171 + * Processor convertToString = new Processor() {
1.172 + * public Object process(Object obj, Collection alwaysNull) {
1.173 + * return obj.toString(); // converts to string
1.174 + * }
1.175 + * };
1.176 + * Enumeration strings = Enumerations.convert(elems, convertToString);
1.177 + * </pre>
1.178 + *
1.179 + * @param en enumeration of any objects
1.180 + * @param processor a callback processor for the elements (its toAdd arguments is always null)
1.181 + * @return new enumeration where all elements has been processed
1.182 + */
1.183 + public static <T,R> Enumeration<R> convert(Enumeration<? extends T> en, Processor<T,R> processor) {
1.184 + return new AltEn<T,R>(en, processor);
1.185 + }
1.186 +
1.187 + /**
1.188 + * Filters some elements out from the input enumeration.
1.189 + * Just make the
1.190 + * {@link Processor} return <code>null</code>. Please notice the <code>toAdd</code>
1.191 + * argument of the processor is always <code>null</code>.
1.192 + * <p>
1.193 + * Example to remove all objects that are not strings:
1.194 + * <pre>
1.195 + * Processor onlyString = new Processor() {
1.196 + * public Object process(Object obj, Collection alwaysNull) {
1.197 + * if (obj instanceof String) {
1.198 + * return obj;
1.199 + * } else {
1.200 + * return null;
1.201 + * }
1.202 + * }
1.203 + * };
1.204 + * Enumeration strings = Enumerations.filter(elems, onlyString);
1.205 + * </pre>
1.206 + *
1.207 + * @param en enumeration of any objects
1.208 + * @param filter a callback processor for the elements (its toAdd arguments is always null)
1.209 + * @return new enumeration which does not include non-processed (returned null from processor) elements
1.210 + * @see NbCollections#checkedEnumerationByFilter
1.211 + */
1.212 + public static <T,R> Enumeration<R> filter(Enumeration<? extends T> en, Processor<T,R> filter) {
1.213 + Parameters.notNull("en", en);
1.214 + Parameters.notNull("filter", filter);
1.215 + return new FilEn<T,R>(en, filter);
1.216 + }
1.217 +
1.218 + /**
1.219 + * Support for breadth-first enumerating.
1.220 + * Before any element is returned
1.221 + * for the resulting enumeration it is processed in the {@link Processor} and
1.222 + * the processor is allowed to modify it and also add additional elements
1.223 + * at the (current) end of the <q>queue</q> by calling <code>toAdd.add</code>
1.224 + * or <code>toAdd.addAll</code>. No other methods can be called on the
1.225 + * provided <code>toAdd</code> collection.
1.226 + * <p>
1.227 + * Example of doing breadth-first walk through a tree:
1.228 + * <pre>
1.229 + * Processor queueSubnodes = new Processor() {
1.230 + * public Object process(Object obj, Collection toAdd) {
1.231 + * Node n = (Node)obj;
1.232 + * toAdd.addAll (n.getChildrenList());
1.233 + * return n;
1.234 + * }
1.235 + * };
1.236 + * Enumeration strings = Enumerations.queue(elems, queueSubnodes);
1.237 + * </pre>
1.238 + *
1.239 + * @param en initial content of the resulting enumeration
1.240 + * @param filter the processor that is called for each element and can
1.241 + * add and addAll elements to its toAdd Collection argument and
1.242 + * also change the value to be returned
1.243 + * @return enumeration with the initial and queued content (it can contain
1.244 + * <code>null</code> if the filter returned <code>null</code> from its
1.245 + * {@link Processor#process} method.
1.246 + */
1.247 + public static <T,R> Enumeration<R> queue(Enumeration<? extends T> en, Processor<T,R> filter) {
1.248 + QEn<T,R> q = new QEn<T,R>(filter);
1.249 +
1.250 + while (en.hasMoreElements()) {
1.251 + q.put(en.nextElement());
1.252 + }
1.253 +
1.254 + return q;
1.255 + }
1.256 +
1.257 + /**
1.258 + * Processor interface that can filter out objects from the enumeration,
1.259 + * change them or add aditional objects to the end of the current enumeration.
1.260 + */
1.261 + public static interface Processor<T,R> {
1.262 + /** @param original the object that is going to be returned from the enumeration right now
1.263 + * @return a replacement for this object
1.264 + * @param toAdd can be non-null if one can add new objects at the end of the enumeration
1.265 + */
1.266 + public R process(T original, Collection<T> toAdd);
1.267 + }
1.268 +
1.269 + /** Altering enumeration implementation */
1.270 + private static final class AltEn<T,R> extends Object implements Enumeration<R> {
1.271 + /** enumeration to filter */
1.272 + private Enumeration<? extends T> en;
1.273 +
1.274 + /** map to alter */
1.275 + private Processor<T,R> process;
1.276 +
1.277 + /**
1.278 + * @param en enumeration to filter
1.279 + */
1.280 + public AltEn(Enumeration<? extends T> en, Processor<T,R> process) {
1.281 + this.en = en;
1.282 + this.process = process;
1.283 + }
1.284 +
1.285 + /** @return true if there is more elements in the enumeration
1.286 + */
1.287 + public boolean hasMoreElements() {
1.288 + return en.hasMoreElements();
1.289 + }
1.290 +
1.291 + /** @return next object in the enumeration
1.292 + * @exception NoSuchElementException can be thrown if there is no next object
1.293 + * in the enumeration
1.294 + */
1.295 + public R nextElement() {
1.296 + return process.process(en.nextElement(), null);
1.297 + }
1.298 + }
1.299 + // end of AltEn
1.300 +
1.301 + /** Sequence of enumerations */
1.302 + private static final class SeqEn<T> extends Object implements Enumeration<T> {
1.303 + /** enumeration of Enumerations */
1.304 + private Enumeration<? extends Enumeration<? extends T>> en;
1.305 +
1.306 + /** current enumeration */
1.307 + private Enumeration<? extends T> current;
1.308 +
1.309 + /** is {@link #current} up-to-date and has more elements?
1.310 + * The combination <CODE>current == null</CODE> and
1.311 + * <CODE>checked == true means there are no more elements
1.312 + * in this enumeration.
1.313 + */
1.314 + private boolean checked = false;
1.315 +
1.316 + /** Constructs new enumeration from already existing. The elements
1.317 + * of <CODE>en</CODE> should be also enumerations. The resulting
1.318 + * enumeration contains elements of such enumerations.
1.319 + *
1.320 + * @param en enumeration of Enumerations that should be sequenced
1.321 + */
1.322 + public SeqEn(Enumeration<? extends Enumeration <? extends T>> en) {
1.323 + this.en = en;
1.324 + }
1.325 +
1.326 + /** Ensures that current enumeration is set. If there aren't more
1.327 + * elements in the Enumerations, sets the field <CODE>current</CODE> to null.
1.328 + */
1.329 + private void ensureCurrent() {
1.330 + while ((current == null) || !current.hasMoreElements()) {
1.331 + if (en.hasMoreElements()) {
1.332 + current = en.nextElement();
1.333 + } else {
1.334 + // no next valid enumeration
1.335 + current = null;
1.336 +
1.337 + return;
1.338 + }
1.339 + }
1.340 + }
1.341 +
1.342 + /** @return true if we have more elements */
1.343 + public boolean hasMoreElements() {
1.344 + if (!checked) {
1.345 + ensureCurrent();
1.346 + checked = true;
1.347 + }
1.348 +
1.349 + return current != null;
1.350 + }
1.351 +
1.352 + /** @return next element
1.353 + * @exception NoSuchElementException if there is no next element
1.354 + */
1.355 + public T nextElement() {
1.356 + if (!checked) {
1.357 + ensureCurrent();
1.358 + }
1.359 +
1.360 + if (current != null) {
1.361 + checked = false;
1.362 +
1.363 + return current.nextElement();
1.364 + } else {
1.365 + checked = true;
1.366 + throw new java.util.NoSuchElementException();
1.367 + }
1.368 + }
1.369 + }
1.370 + // end of SeqEn
1.371 +
1.372 + /** QueueEnumeration
1.373 + */
1.374 + private static class QEn<T,R> extends Object implements Enumeration<R> {
1.375 + /** next object to be returned */
1.376 + private ListItem<T> next = null;
1.377 +
1.378 + /** last object in the queue */
1.379 + private ListItem<T> last = null;
1.380 +
1.381 + /** processor to use */
1.382 + private Processor<T,R> processor;
1.383 +
1.384 + public QEn(Processor<T,R> p) {
1.385 + this.processor = p;
1.386 + }
1.387 +
1.388 + /** Put adds new object to the end of queue.
1.389 + * @param o the object to add
1.390 + */
1.391 + public void put(T o) {
1.392 + if (last != null) {
1.393 + ListItem<T> li = new ListItem<T>(o);
1.394 + last.next = li;
1.395 + last = li;
1.396 + } else {
1.397 + next = last = new ListItem<T>(o);
1.398 + }
1.399 + }
1.400 +
1.401 + /** Adds array of objects into the queue.
1.402 + * @param arr array of objects to put into the queue
1.403 + */
1.404 + public void put(Collection<? extends T> arr) {
1.405 + for (T e : arr) {
1.406 + put(e);
1.407 + }
1.408 + }
1.409 +
1.410 + /** Is there any next object?
1.411 + * @return true if there is next object, false otherwise
1.412 + */
1.413 + public boolean hasMoreElements() {
1.414 + return next != null;
1.415 + }
1.416 +
1.417 + /** @return next object in enumeration
1.418 + * @exception NoSuchElementException if there is no next object
1.419 + */
1.420 + public R nextElement() {
1.421 + if (next == null) {
1.422 + throw new NoSuchElementException();
1.423 + }
1.424 +
1.425 + T res = next.object;
1.426 +
1.427 + if ((next = next.next) == null) {
1.428 + last = null;
1.429 + }
1.430 +
1.431 + ;
1.432 +
1.433 + ToAdd<T,R> toAdd = new ToAdd<T,R>(this);
1.434 + R out = processor.process(res, toAdd);
1.435 + toAdd.finish();
1.436 +
1.437 + return out;
1.438 + }
1.439 +
1.440 + /** item in linked list of Objects */
1.441 + private static final class ListItem<T> {
1.442 + T object;
1.443 + ListItem<T> next;
1.444 +
1.445 + /** @param o the object for this item */
1.446 + ListItem(T o) {
1.447 + object = o;
1.448 + }
1.449 + }
1.450 +
1.451 + /** Temporary collection that supports only add and addAll operations*/
1.452 + private static final class ToAdd<T,R> extends Object implements Collection<T> {
1.453 + private QEn<T,R> q;
1.454 +
1.455 + public ToAdd(QEn<T,R> q) {
1.456 + this.q = q;
1.457 + }
1.458 +
1.459 + public void finish() {
1.460 + this.q = null;
1.461 + }
1.462 +
1.463 + public boolean add(T o) {
1.464 + q.put(o);
1.465 +
1.466 + return true;
1.467 + }
1.468 +
1.469 + public boolean addAll(Collection<? extends T> c) {
1.470 + q.put(c);
1.471 +
1.472 + return true;
1.473 + }
1.474 +
1.475 + private String msg() {
1.476 + return "Only add and addAll are implemented"; // NOI18N
1.477 + }
1.478 +
1.479 + public void clear() {
1.480 + throw new UnsupportedOperationException(msg());
1.481 + }
1.482 +
1.483 + public boolean contains(Object o) {
1.484 + throw new UnsupportedOperationException(msg());
1.485 + }
1.486 +
1.487 + public boolean containsAll(Collection c) {
1.488 + throw new UnsupportedOperationException(msg());
1.489 + }
1.490 +
1.491 + public boolean isEmpty() {
1.492 + throw new UnsupportedOperationException(msg());
1.493 + }
1.494 +
1.495 + public Iterator<T> iterator() {
1.496 + throw new UnsupportedOperationException(msg());
1.497 + }
1.498 +
1.499 + public boolean remove(Object o) {
1.500 + throw new UnsupportedOperationException(msg());
1.501 + }
1.502 +
1.503 + public boolean removeAll(Collection c) {
1.504 + throw new UnsupportedOperationException(msg());
1.505 + }
1.506 +
1.507 + public boolean retainAll(Collection c) {
1.508 + throw new UnsupportedOperationException(msg());
1.509 + }
1.510 +
1.511 + public int size() {
1.512 + throw new UnsupportedOperationException(msg());
1.513 + }
1.514 +
1.515 + public Object[] toArray() {
1.516 + throw new UnsupportedOperationException(msg());
1.517 + }
1.518 +
1.519 + public<X> X[] toArray(X[] a) {
1.520 + throw new UnsupportedOperationException(msg());
1.521 + }
1.522 + }
1.523 + // end of ToAdd
1.524 + }
1.525 + // end of QEn
1.526 +
1.527 + /** Filtering enumeration */
1.528 + private static final class FilEn<T,R> extends Object implements Enumeration<R> {
1.529 + /** marker object stating there is no nexte element prepared */
1.530 + private static final Object EMPTY = new Object();
1.531 +
1.532 + /** enumeration to filter */
1.533 + private Enumeration<? extends T> en;
1.534 +
1.535 + /** element to be returned next time or {@link #EMPTY} if there is
1.536 + * no such element prepared */
1.537 + private R next = empty();
1.538 +
1.539 + /** the set to use as filter */
1.540 + private Processor<T,R> filter;
1.541 +
1.542 + /**
1.543 + * @param en enumeration to filter
1.544 + */
1.545 + public FilEn(Enumeration<? extends T> en, Processor<T,R> filter) {
1.546 + this.en = en;
1.547 + this.filter = filter;
1.548 + }
1.549 +
1.550 + /** @return true if there is more elements in the enumeration
1.551 + */
1.552 + public boolean hasMoreElements() {
1.553 + if (next != empty()) {
1.554 + // there is a object already prepared
1.555 + return true;
1.556 + }
1.557 +
1.558 + while (en.hasMoreElements()) {
1.559 + // read next
1.560 + next = filter.process(en.nextElement(), null);
1.561 +
1.562 + if (next != null) {
1.563 + // if the object is accepted
1.564 + return true;
1.565 + }
1.566 +
1.567 + ;
1.568 + }
1.569 +
1.570 + next = empty();
1.571 +
1.572 + return false;
1.573 + }
1.574 +
1.575 + /** @return next object in the enumeration
1.576 + * @exception NoSuchElementException can be thrown if there is no next object
1.577 + * in the enumeration
1.578 + */
1.579 + public R nextElement() {
1.580 + if ((next == EMPTY) && !hasMoreElements()) {
1.581 + throw new NoSuchElementException();
1.582 + }
1.583 +
1.584 + R res = next;
1.585 + next = empty();
1.586 +
1.587 + return res;
1.588 + }
1.589 +
1.590 + @SuppressWarnings("unchecked")
1.591 + private R empty() {
1.592 + return (R)EMPTY;
1.593 + }
1.594 + }
1.595 + // end of FilEn
1.596 +
1.597 + /** Returns true from contains if object is not null */
1.598 + private static class RNulls<T> implements Processor<T,T> {
1.599 + public T process(T original, Collection<T> toAdd) {
1.600 + return original;
1.601 + }
1.602 + }
1.603 + // end of RNulls
1.604 +}