| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| EnumUtils |
|
| 2.6666666666666665;2.667 |
| 1 | /* | |
| 2 | * $Source$ | |
| 3 | * $Revision$ | |
| 4 | * | |
| 5 | * Copyright (C) 2000 William Chesters | |
| 6 | * | |
| 7 | * Part of Melati (http://melati.org), a framework for the rapid | |
| 8 | * development of clean, maintainable web applications. | |
| 9 | * | |
| 10 | * Melati is free software; Permission is granted to copy, distribute | |
| 11 | * and/or modify this software under the terms either: | |
| 12 | * | |
| 13 | * a) the GNU General Public License as published by the Free Software | |
| 14 | * Foundation; either version 2 of the License, or (at your option) | |
| 15 | * any later version, | |
| 16 | * | |
| 17 | * or | |
| 18 | * | |
| 19 | * b) any version of the Melati Software License, as published | |
| 20 | * at http://melati.org | |
| 21 | * | |
| 22 | * You should have received a copy of the GNU General Public License and | |
| 23 | * the Melati Software License along with this program; | |
| 24 | * if not, write to the Free Software Foundation, Inc., | |
| 25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the | |
| 26 | * GNU General Public License and visit http://melati.org to obtain the | |
| 27 | * Melati Software License. | |
| 28 | * | |
| 29 | * Feel free to contact the Developers of Melati (http://melati.org), | |
| 30 | * if you would like to work out a different arrangement than the options | |
| 31 | * outlined here. It is our intention to allow Melati to be used by as | |
| 32 | * wide an audience as possible. | |
| 33 | * | |
| 34 | * This program is distributed in the hope that it will be useful, | |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 37 | * GNU General Public License for more details. | |
| 38 | * | |
| 39 | * Contact details for copyright holder: | |
| 40 | * | |
| 41 | * William Chesters <williamc At paneris.org> | |
| 42 | * http://paneris.org/~williamc | |
| 43 | * Obrechtstraat 114, 2517VX Den Haag, The Netherlands | |
| 44 | */ | |
| 45 | ||
| 46 | package org.melati.poem.util; | |
| 47 | ||
| 48 | import java.util.ArrayList; | |
| 49 | import java.util.List; | |
| 50 | import java.util.Vector; | |
| 51 | import java.util.Enumeration; | |
| 52 | ||
| 53 | /** | |
| 54 | * An assortment of useful operations on <code>Enumeration</code>s. | |
| 55 | */ | |
| 56 | public final class EnumUtils { | |
| 57 | 0 | private EnumUtils() {} |
| 58 | ||
| 59 | /** | |
| 60 | * Skip a specified number of Elements in an Enumeration. | |
| 61 | * @param e the Enumeration to skip Elements of | |
| 62 | * @param n the number of Elements to skip | |
| 63 | * @return the number of Elements actually skipped, may be less than asked | |
| 64 | */ | |
| 65 | public static <T extends Object> int skip(Enumeration<T> e, int n) { | |
| 66 | 2 | int c = 0; |
| 67 | 2 | if (e instanceof SkipEnumeration<?>) { |
| 68 | 1 | SkipEnumeration<T> s = (SkipEnumeration<T>)e; |
| 69 | 2 | while (c < n && s.hasMoreElements()) { |
| 70 | 1 | s.skip(); |
| 71 | 1 | ++c; |
| 72 | } | |
| 73 | 1 | } |
| 74 | else | |
| 75 | 3 | while (c < n && e.hasMoreElements()) { |
| 76 | 2 | e.nextElement(); |
| 77 | 2 | ++c; |
| 78 | } | |
| 79 | ||
| 80 | 2 | return c; |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Create a Vector of the first n Elements of an Enumeration. | |
| 85 | * If the number of elements in the Enumeration is less than | |
| 86 | * n then the remaining elements of the Vector will be null. | |
| 87 | * @param e the input Enumeration | |
| 88 | * @param n the number of Elements to include | |
| 89 | * @return a new Vector of the initial Elements | |
| 90 | */ | |
| 91 | public static <T extends Object> Vector<T> initial(Enumeration<T> e, int n) { | |
| 92 | 0 | Vector<T> v = new Vector<T>(n); |
| 93 | ||
| 94 | 0 | while (n > 0 && e.hasMoreElements()) { |
| 95 | 0 | v.addElement(e.nextElement()); |
| 96 | 0 | --n; |
| 97 | } | |
| 98 | ||
| 99 | 0 | return v; |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Join two Enumerations into a single one. | |
| 104 | * @param a head Enumeration | |
| 105 | * @param b tail Enumeration | |
| 106 | * @return a new enumeration which is a concatenation of A and B | |
| 107 | */ | |
| 108 | public static <T extends Object> Enumeration<T> join(Enumeration<T> a, Enumeration<T> b) { | |
| 109 | 1 | Vector<T> aVector = vectorOf(a); |
| 110 | 4 | while (b.hasMoreElements()) |
| 111 | 3 | aVector.addElement(b.nextElement()); |
| 112 | 1 | return aVector.elements(); |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * Create a Vector from an Enumeration. | |
| 117 | * @param e the source Enumeration | |
| 118 | * @param roughSize starting size of the Vector | |
| 119 | * @return a Vector of the Elements of the input Enumeration | |
| 120 | */ | |
| 121 | public static <T extends Object> Vector<T> vectorOf(Enumeration<T> e, int roughSize) { | |
| 122 | 478 | Vector<T> v = new Vector<T>(roughSize); |
| 123 | ||
| 124 | 881 | while (e.hasMoreElements()) |
| 125 | 405 | v.addElement(e.nextElement()); |
| 126 | ||
| 127 | 476 | return v; |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Create a Vector from an Enumeration, supplying an | |
| 132 | * initial size of 20. | |
| 133 | * @param e the source Enumeration | |
| 134 | * @return a Vector with size at least 20 of the Elements of the input Enumeration | |
| 135 | */ | |
| 136 | public static <T extends Object> Vector<T> vectorOf(Enumeration<T> e) { | |
| 137 | 478 | return vectorOf(e, 20); |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Concatenate an Enumeration, specifying the separator. | |
| 142 | * @param sep Separator string | |
| 143 | * @param e Enumeration to be concatenated | |
| 144 | * @return A String representation of the Enumeration | |
| 145 | */ | |
| 146 | public static String concatenated(String sep, Enumeration<?> e) { | |
| 147 | 414 | StringBuffer b = new StringBuffer(); |
| 148 | ||
| 149 | 414 | if (e.hasMoreElements()) |
| 150 | 113 | b.append(String.valueOf(e.nextElement())); |
| 151 | ||
| 152 | 442 | while (e.hasMoreElements()) { |
| 153 | 28 | b.append(sep); |
| 154 | 28 | b.append(String.valueOf(e.nextElement())); |
| 155 | } | |
| 156 | ||
| 157 | 414 | return b.toString(); |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * Whether the Enumeration contain an Object. | |
| 162 | * @param e an Enumeration to look in | |
| 163 | * @param o the Object to look for | |
| 164 | * @return true if the Object occurs in the Enumeration | |
| 165 | */ | |
| 166 | public static boolean contains(Enumeration<?> e, Object o) { | |
| 167 | 9 | while (e.hasMoreElements()) |
| 168 | 6 | if (e.nextElement().equals(o)) |
| 169 | 2 | return true; |
| 170 | ||
| 171 | 3 | return false; |
| 172 | } | |
| 173 | ||
| 174 | /** @return a List from an Enumeration */ | |
| 175 | public static <T> List<T> list(Enumeration<T> enumeration) { | |
| 176 | 4 | ArrayList<T> them = new ArrayList<T>(); |
| 177 | 108 | while(enumeration.hasMoreElements()) |
| 178 | 104 | them.add(enumeration.nextElement()); |
| 179 | 4 | return them; |
| 180 | } | |
| 181 | } |