Coverage Report - org.owasp.dependencycheck.org.apache.tools.ant.util.VectorSet
 
Classes in this File Line Coverage Branch Coverage Complexity
VectorSet
14%
15/105
7%
3/42
1.962
 
 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.Collection;
 21  
 import java.util.HashSet;
 22  
 import java.util.LinkedList;
 23  
 import java.util.Set;
 24  
 import java.util.Vector;
 25  
 
 26  
 /**
 27  
  * Subclass of Vector that won't store duplicate entries and shows
 28  
  * HashSet's constant time performance characteristics for the
 29  
  * contains method.
 30  
  *
 31  
  * <p>This is not a general purpose class but has been written because
 32  
  * the protected members of {@link
 33  
  * org.apache.tools.ant.DirectoryScanner DirectoryScanner} prohibited
 34  
  * later revisions from using a more efficient collection.</p>
 35  
  *
 36  
  * <p>Methods are synchronized to keep Vector's contract.</p>
 37  
  *
 38  
  * @since Ant 1.8.0
 39  
  */
 40  
 public final class VectorSet<E> extends Vector<E> {
 41  
     private static final long serialVersionUID = 1L;
 42  
 
 43  8
     private final HashSet<E> set = new HashSet<E>();
 44  
 
 45  16
     public VectorSet() { super(); }
 46  
 
 47  0
     public VectorSet(int initialCapacity) { super(initialCapacity); }
 48  
 
 49  
     public VectorSet(int initialCapacity, int capacityIncrement) {
 50  0
         super(initialCapacity, capacityIncrement);
 51  0
     }
 52  
 
 53  0
     public VectorSet(Collection<? extends E> c) {
 54  0
         if (c != null) {
 55  0
             for (E e : c) {
 56  0
                 add(e);
 57  0
             }
 58  
         }
 59  0
     }
 60  
 
 61  
     public synchronized boolean add(E o) {
 62  19
         if (!set.contains(o)) {
 63  19
             doAdd(size(), o);
 64  19
             return true;
 65  
         }
 66  0
         return false;
 67  
     }
 68  
 
 69  
     /**
 70  
      * This implementation may not add the element at the given index
 71  
      * if it is already contained in the collection.
 72  
      */
 73  
     public void add(int index, E o) {
 74  0
         doAdd(index, o);
 75  0
     }
 76  
 
 77  
     private synchronized void doAdd(int index, E o) {
 78  
         // Vector.add seems to delegate to insertElementAt, but this
 79  
         // is not documented so we may better implement it ourselves
 80  20
         if (set.add(o)) {
 81  20
             int count = size();
 82  20
             ensureCapacity(count + 1);
 83  20
             if (index != count) {
 84  0
                 System.arraycopy(elementData, index, elementData, index + 1,
 85  
                                  count - index);
 86  
             }
 87  20
             elementData[index] = o;
 88  20
             elementCount++;
 89  
         }
 90  20
     }
 91  
 
 92  
     public synchronized void addElement(E o) {
 93  1
         doAdd(size(), o);
 94  1
     }
 95  
 
 96  
     public synchronized boolean addAll(Collection<? extends E> c) {
 97  0
         boolean changed = false;
 98  0
         for (E e : c) {
 99  0
             changed |= add(e);
 100  0
         }
 101  0
         return changed;
 102  
     }
 103  
 
 104  
     /**
 105  
      * This implementation may not add all elements at the given index
 106  
      * if any of them are already contained in the collection.
 107  
      */
 108  
     public synchronized boolean addAll(int index, Collection<? extends E> c) {
 109  0
         LinkedList toAdd = new LinkedList();
 110  0
         for (E e : c) {
 111  0
             if (set.add(e)) {
 112  0
                 toAdd.add(e);
 113  
             }
 114  0
         }
 115  0
         if (toAdd.isEmpty()) {
 116  0
             return false;
 117  
         }
 118  0
         int count = size();
 119  0
         ensureCapacity(count + toAdd.size());
 120  0
         if (index != count) {
 121  0
             System.arraycopy(elementData, index, elementData, index + toAdd.size(),
 122  
                              count - index);
 123  
         }
 124  0
         for (Object o : toAdd) {
 125  0
             elementData[index++] = o;
 126  0
         }
 127  0
         elementCount += toAdd.size();
 128  0
         return true;
 129  
     }
 130  
 
 131  
     public synchronized void clear() {
 132  0
         super.clear();
 133  0
         set.clear();
 134  0
     }
 135  
 
 136  
     public Object clone() {
 137  
         @SuppressWarnings("unchecked")
 138  0
         final VectorSet<E> vs = (VectorSet<E>) super.clone();
 139  0
         vs.set.addAll(set);
 140  0
         return vs;
 141  
     }
 142  
 
 143  
     public synchronized boolean contains(Object o) {
 144  57
         return set.contains(o);
 145  
     }
 146  
 
 147  
     public synchronized boolean containsAll(Collection<?> c) {
 148  0
         return set.containsAll(c);
 149  
     }
 150  
 
 151  
     public void insertElementAt(E o, int index) {
 152  0
         doAdd(index, o);
 153  0
     }
 154  
 
 155  
     public synchronized E remove(int index) {
 156  0
         E o = get(index);
 157  0
         remove(o);
 158  0
         return o;
 159  
     }
 160  
 
 161  
     public boolean remove(Object o) {
 162  0
         return doRemove(o);
 163  
     }
 164  
 
 165  
     private synchronized boolean doRemove(Object o) {
 166  
         // again, remove seems to delegate to removeElement, but we
 167  
         // shouldn't trust it
 168  0
         if (set.remove(o)) {
 169  0
             int index = indexOf(o);
 170  0
             if (index < elementData.length - 1) {
 171  0
                 System.arraycopy(elementData, index + 1, elementData, index,
 172  
                                  elementData.length - index - 1);
 173  
             }
 174  0
             elementCount--;
 175  0
             return true;
 176  
         }
 177  0
         return false;
 178  
     }
 179  
 
 180  
     public synchronized boolean removeAll(Collection<?> c) {
 181  0
         boolean changed = false;
 182  0
         for (Object o : c) {
 183  0
             changed |= remove(o);
 184  0
         }
 185  0
         return changed;
 186  
     }
 187  
 
 188  
     public synchronized void removeAllElements() {
 189  0
         set.clear();
 190  0
         super.removeAllElements();
 191  0
     }
 192  
 
 193  
     public boolean removeElement(Object o) {
 194  0
         return doRemove(o);
 195  
     }
 196  
 
 197  
     public synchronized void removeElementAt(int index) {
 198  0
         remove(get(index));
 199  0
     }
 200  
 
 201  
     public synchronized void removeRange(final int fromIndex, int toIndex) {
 202  0
         while (toIndex > fromIndex) {
 203  0
             remove(--toIndex);
 204  
         }
 205  0
     }
 206  
 
 207  
     public synchronized boolean retainAll(Collection<?> c) {
 208  0
         if (!(c instanceof Set)) {
 209  0
             c = new HashSet<Object>(c);
 210  
         }
 211  0
         LinkedList<E> l = new LinkedList<E>();
 212  0
         for (E o : this) {
 213  0
             if (!c.contains(o)) {
 214  0
                 l.addLast(o);
 215  
             }
 216  0
         }
 217  0
         if (!l.isEmpty()) {
 218  0
             removeAll(l);
 219  0
             return true;
 220  
         }
 221  0
         return false;
 222  
     }
 223  
 
 224  
     public synchronized E set(int index, E o) {
 225  0
         E orig = get(index);
 226  0
         if (set.add(o)) {
 227  0
             elementData[index] = o;
 228  0
             set.remove(orig);
 229  
         } else {
 230  0
             int oldIndexOfO = indexOf(o);
 231  0
             remove(o);
 232  0
             remove(orig);
 233  0
             add(oldIndexOfO > index ? index : index - 1, o);
 234  
         }
 235  0
         return orig;
 236  
     }
 237  
 
 238  
     public void setElementAt(E o, int index) {
 239  0
         set(index, o);
 240  0
     }
 241  
 
 242  
 }