| 1 | |
package org.webslinger.commons.vfs.util; |
| 2 | |
|
| 3 | |
import java.util.Arrays; |
| 4 | |
import java.util.Collection; |
| 5 | |
import java.util.HashSet; |
| 6 | |
import java.util.Iterator; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.NoSuchElementException; |
| 9 | |
import java.util.Set; |
| 10 | |
|
| 11 | |
import org.apache.commons.vfs.FileObject; |
| 12 | |
|
| 13 | |
public abstract class FileAttributeCollection<F extends FileObject, T> extends FileAttributeBaseWrapper<F> implements Collection<T>, IteratorOwner { |
| 14 | |
protected FileAttributeMap<F> fam; |
| 15 | |
protected int serialNumber; |
| 16 | |
protected IteratorRemover remover; |
| 17 | |
|
| 18 | |
protected FileAttributeCollection(FileAttributeMap<F> fam, IteratorRemover remover) { |
| 19 | 28 | super(fam); |
| 20 | 28 | this.fam = fam; |
| 21 | 28 | this.remover = remover; |
| 22 | 28 | } |
| 23 | |
|
| 24 | |
public void clear() { |
| 25 | 0 | fam.clear(); |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
public void modified() { |
| 29 | 0 | serialNumber++; |
| 30 | 0 | } |
| 31 | |
|
| 32 | |
public int getSerialNumber() { |
| 33 | 154 | return serialNumber; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public boolean containsAll(Collection<?> c) { |
| 37 | 0 | Iterator<?> it = c.iterator(); |
| 38 | |
try { |
| 39 | |
while (true) { |
| 40 | 0 | if (!contains(it.next())) return false; |
| 41 | |
} |
| 42 | 0 | } catch (NoSuchElementException e) { |
| 43 | 0 | return true; |
| 44 | |
} |
| 45 | |
} |
| 46 | |
|
| 47 | |
public Iterator<T> iterator() { |
| 48 | 28 | return new IteratorWrapper<T>(((List<T>) Arrays.asList(toArray())).iterator(), remover, this); |
| 49 | |
} |
| 50 | |
|
| 51 | |
public boolean remove(Object o) { |
| 52 | 0 | boolean result = remover.remove(o); |
| 53 | 0 | if (result) serialNumber++; |
| 54 | 0 | return result; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public boolean removeAll(Collection c) { |
| 58 | 0 | Iterator it = c.iterator(); |
| 59 | 0 | int localNumber = serialNumber; |
| 60 | |
try { |
| 61 | |
while (true) { |
| 62 | 0 | remove(it.next()); |
| 63 | |
} |
| 64 | 0 | } catch (NoSuchElementException e) { |
| 65 | 0 | return localNumber != serialNumber; |
| 66 | |
} |
| 67 | |
} |
| 68 | |
|
| 69 | |
public boolean retainAll(Collection<?> c) { |
| 70 | 0 | Set<?> toRetain = new HashSet<Object>(c); |
| 71 | 0 | int localNumber = serialNumber; |
| 72 | 0 | for (Object item: toArray()) { |
| 73 | 0 | if (!toRetain.contains(item)) { |
| 74 | 0 | remove(item); |
| 75 | |
} |
| 76 | |
} |
| 77 | 0 | return localNumber != serialNumber; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public int size() { |
| 81 | |
try { |
| 82 | 28 | return resolver.getAttributeNames(getFile()).length; |
| 83 | 0 | } catch (Exception e) { |
| 84 | 0 | return 0; |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | |
public boolean equals(Object other) { |
| 89 | 0 | if (!(other instanceof FileAttributeCollection)) return false; |
| 90 | 0 | FileAttributeCollection fac = (FileAttributeCollection) other; |
| 91 | 0 | return resolver.equals(fac.resolver) && getClass().equals(fac.getClass()); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public int hashCode() { |
| 95 | 0 | return getClass().hashCode() ^ resolver.hashCode(); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public boolean isEmpty() { |
| 99 | 0 | return size() == 0; |
| 100 | |
} |
| 101 | |
|
| 102 | |
public Object[] toArray() { |
| 103 | 28 | return toArray(new Object[size()]); |
| 104 | |
} |
| 105 | |
} |
| 106 | |
|