Coverage Report - org.owasp.dependencycheck.org.apache.tools.ant.util.CollectionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionUtils
16%
8/48
13%
5/38
2.45
CollectionUtils$1
0%
0/2
N/A
2.45
CollectionUtils$2
0%
0/3
N/A
2.45
CollectionUtils$CompoundEnumeration
0%
0/4
0%
0/6
2.45
CollectionUtils$EmptyEnumeration
0%
0/1
N/A
2.45
 
 1  
 /*
 2  
  *  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  *  contributor license agreements.  See the NOTICE file distributed with
 4  
  *  this work for additional information regarding copyright ownership.
 5  
  *  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  *  (the "License"); you may not use this file except in compliance with
 7  
  *  the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  *  Unless required by applicable law or agreed to in writing, software
 12  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  *  See the License for the specific language governing permissions and
 15  
  *  limitations under the License.
 16  
  *
 17  
  */
 18  
 package org.owasp.dependencycheck.org.apache.tools.ant.util;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.Dictionary;
 24  
 import java.util.Enumeration;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.NoSuchElementException;
 28  
 import java.util.Vector;
 29  
 
 30  
 // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
 31  
 
 32  
 /**
 33  
  * A set of helper methods related to collection manipulation.
 34  
  *
 35  
  * @since Ant 1.5
 36  
  */
 37  
 public class CollectionUtils {
 38  
 
 39  
     /**
 40  
      * Collections.emptyList() is Java5+.
 41  
      */
 42  
     @SuppressWarnings("rawtypes")
 43  
     @Deprecated
 44  1
     public static final List EMPTY_LIST = Collections.EMPTY_LIST;
 45  
 
 46  
     /**
 47  
      * Please use Vector.equals() or List.equals().
 48  
      * @param v1 the first vector.
 49  
      * @param v2 the second vector.
 50  
      * @return true if the vectors are equal.
 51  
      * @since Ant 1.5
 52  
      * @deprecated since 1.6.x.
 53  
      */
 54  
     public static boolean equals(Vector<?> v1, Vector<?> v2) {
 55  0
         if (v1 == v2) {
 56  0
             return true;
 57  
         }
 58  
 
 59  0
         if (v1 == null || v2 == null) {
 60  0
             return false;
 61  
         }
 62  
 
 63  0
         return v1.equals(v2);
 64  
     }
 65  
 
 66  
     /**
 67  
      * Dictionary does not have an equals.
 68  
      * Please use  Map.equals().
 69  
      *
 70  
      * <p>Follows the equals contract of Java 2's Map.</p>
 71  
      * @param d1 the first directory.
 72  
      * @param d2 the second directory.
 73  
      * @return true if the directories are equal.
 74  
      * @since Ant 1.5
 75  
      * @deprecated since 1.6.x.
 76  
      */
 77  
     public static boolean equals(Dictionary<?, ?> d1, Dictionary<?, ?> d2) {
 78  0
         if (d1 == d2) {
 79  0
             return true;
 80  
         }
 81  
 
 82  0
         if (d1 == null || d2 == null) {
 83  0
             return false;
 84  
         }
 85  
 
 86  0
         if (d1.size() != d2.size()) {
 87  0
             return false;
 88  
         }
 89  
 
 90  0
         Enumeration<?> e1 = d1.keys();
 91  0
         while (e1.hasMoreElements()) {
 92  0
             Object key = e1.nextElement();
 93  0
             Object value1 = d1.get(key);
 94  0
             Object value2 = d2.get(key);
 95  0
             if (value2 == null || !value1.equals(value2)) {
 96  0
                 return false;
 97  
             }
 98  0
         }
 99  
 
 100  
         // don't need the opposite check as the Dictionaries have the
 101  
         // same size, so we've also covered all keys of d2 already.
 102  
 
 103  0
         return true;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Creates a comma separated list of all values held in the given
 108  
      * collection.
 109  
      *
 110  
      * @since Ant 1.8.0
 111  
      */
 112  
     public static String flattenToString(Collection<?> c) {
 113  0
         final StringBuilder sb = new StringBuilder();
 114  0
         for (Object o : c) {
 115  0
             if (sb.length() != 0) {
 116  0
                 sb.append(",");
 117  
             }
 118  0
             sb.append(o);
 119  0
         }
 120  0
         return sb.toString();
 121  
     }
 122  
 
 123  
     /**
 124  
      * Dictionary does not know the putAll method. Please use Map.putAll().
 125  
      * @param m1 the to directory.
 126  
      * @param m2 the from directory.
 127  
      * @since Ant 1.6
 128  
      * @deprecated since 1.6.x.
 129  
      */
 130  
     public static <K, V> void putAll(Dictionary<? super K, ? super V> m1, Dictionary<? extends K, ? extends V> m2) {
 131  0
         for (Enumeration<? extends K> it = m2.keys(); it.hasMoreElements();) {
 132  0
             K key = it.nextElement();
 133  0
             m1.put(key, m2.get(key));
 134  0
         }
 135  0
     }
 136  
 
 137  
     /**
 138  
      * An empty enumeration.
 139  
      * @since Ant 1.6
 140  
      */
 141  
     public static final class EmptyEnumeration<E> implements Enumeration<E> {
 142  
         /** Constructor for the EmptyEnumeration */
 143  
         public EmptyEnumeration() {
 144  
         }
 145  
 
 146  
         /**
 147  
          * @return false always.
 148  
          */
 149  
         public boolean hasMoreElements() {
 150  
             return false;
 151  
         }
 152  
 
 153  
         /**
 154  
          * @return nothing.
 155  
          * @throws NoSuchElementException always.
 156  
          */
 157  
         public E nextElement() throws NoSuchElementException {
 158  0
             throw new NoSuchElementException();
 159  
         }
 160  
     }
 161  
 
 162  
     /**
 163  
      * Append one enumeration to another.
 164  
      * Elements are evaluated lazily.
 165  
      * @param e1 the first enumeration.
 166  
      * @param e2 the subsequent enumeration.
 167  
      * @return an enumeration representing e1 followed by e2.
 168  
      * @since Ant 1.6.3
 169  
      */
 170  
     public static <E> Enumeration<E> append(Enumeration<E> e1, Enumeration<E> e2) {
 171  0
         return new CompoundEnumeration<E>(e1, e2);
 172  
     }
 173  
 
 174  
     /**
 175  
      * Adapt the specified Iterator to the Enumeration interface.
 176  
      * @param iter the Iterator to adapt.
 177  
      * @return an Enumeration.
 178  
      */
 179  
     public static <E> Enumeration<E> asEnumeration(final Iterator<E> iter) {
 180  0
         return new Enumeration<E>() {
 181  
             public boolean hasMoreElements() {
 182  0
                 return iter.hasNext();
 183  
             }
 184  
             public E nextElement() {
 185  0
                 return iter.next();
 186  
             }
 187  
         };
 188  
     }
 189  
 
 190  
     /**
 191  
      * Adapt the specified Enumeration to the Iterator interface.
 192  
      * @param e the Enumeration to adapt.
 193  
      * @return an Iterator.
 194  
      */
 195  
     public static <E> Iterator<E> asIterator(final Enumeration<E> e) {
 196  0
         return new Iterator<E>() {
 197  
             public boolean hasNext() {
 198  0
                 return e.hasMoreElements();
 199  
             }
 200  
             public E next() {
 201  0
                 return e.nextElement();
 202  
             }
 203  
             public void remove() {
 204  0
                 throw new UnsupportedOperationException();
 205  
             }
 206  
         };
 207  
     }
 208  
 
 209  
     /**
 210  
      * Returns a collection containing all elements of the iterator.
 211  
      *
 212  
      * @since Ant 1.8.0
 213  
      */
 214  
     public static <T> Collection<T> asCollection(final Iterator<? extends T> iter) {
 215  0
         List<T> l = new ArrayList<T>();
 216  0
         while (iter.hasNext()) {
 217  0
             l.add(iter.next());
 218  
         }
 219  0
         return l;
 220  
     }
 221  
 
 222  
     private static final class CompoundEnumeration<E> implements Enumeration<E> {
 223  
 
 224  
         private final Enumeration<E> e1, e2;
 225  
 
 226  
         public CompoundEnumeration(Enumeration<E> e1, Enumeration<E> e2) {
 227  
             this.e1 = e1;
 228  
             this.e2 = e2;
 229  
         }
 230  
 
 231  
         public boolean hasMoreElements() {
 232  0
             return e1.hasMoreElements() || e2.hasMoreElements();
 233  
         }
 234  
 
 235  
         public E nextElement() throws NoSuchElementException {
 236  0
             if (e1.hasMoreElements()) {
 237  0
                 return e1.nextElement();
 238  
             } else {
 239  0
                 return e2.nextElement();
 240  
             }
 241  
         }
 242  
 
 243  
     }
 244  
 
 245  
     /**
 246  
      * Counts how often the given Object occurs in the given
 247  
      * collection using equals() for comparison.
 248  
      *
 249  
      * @since Ant 1.8.0
 250  
      */
 251  
     public static int frequency(Collection<?> c, Object o) {
 252  
         // same as Collections.frequency introduced with JDK 1.5
 253  3
         int freq = 0;
 254  3
         if (c != null) {
 255  3
             for (Iterator<?> i = c.iterator(); i.hasNext(); ) {
 256  18
                 Object test = i.next();
 257  18
                 if (o == null ? test == null : o.equals(test)) {
 258  0
                     freq++;
 259  
                 }
 260  18
             }
 261  
         }
 262  3
         return freq;
 263  
     }
 264  
 
 265  
 }