package org.osbl.authorization;
import org.hibernate.util.EmptyIterator;
import java.security.PermissionCollection;
import java.security.Permission;
import java.util.*;
public class UnionPermissionCollection
extends PermissionCollection
{
List<PermissionCollection> permissionCollections = new ArrayList<PermissionCollection>();
public void add(Permission permission) {
throw new UnsupportedOperationException();
}
public boolean implies(Permission permission) {
for (PermissionCollection permissionCollection : permissionCollections) {
if (permissionCollection.implies(permission))
return true;
}
return false;
}
public Enumeration<Permission> elements() {
List<Enumeration> iterators = new ArrayList<Enumeration>(permissionCollections.size());
for (PermissionCollection permissionCollection : permissionCollections) {
iterators.add(permissionCollection.elements());
}
return new JoinedIterator(iterators);
}
public void addPermissionCollection(PermissionCollection permissionCollection) {
permissionCollections.add(permissionCollection);
}
public static class JoinedIterator implements Iterator, Enumeration {
// wrapped iterators
private Iterator[] iterators;
// index of current iterator in the wrapped iterators array
private int currentIteratorIndex;
// the current iterator
private Iterator currentIterator;
// the last used iterator
private Iterator lastUsedIterator;
public JoinedIterator(List iterators) {
this( (Iterator[]) iterators.toArray(new Iterator[iterators.size()]) );
}
public JoinedIterator(Iterator[] iterators) {
if( iterators==null )
throw new NullPointerException("Unexpected NULL iterators argument");
this.iterators = iterators;
}
public JoinedIterator(Iterator first, Iterator second) {
this( new Iterator[] { first, second } );
}
public boolean hasNext() {
updateCurrentIterator();
return currentIterator.hasNext();
}
public Object next() {
updateCurrentIterator();
return currentIterator.next();
}
public void remove() {
updateCurrentIterator();
lastUsedIterator.remove();
}
// call this before any Iterator method to make sure that the current Iterator
// is not exhausted
protected void updateCurrentIterator() {
if (currentIterator == null) {
if( iterators.length==0 ) {
currentIterator = EmptyIterator.INSTANCE;
}
else {
currentIterator = iterators[0];
}
// set last used iterator here, in case the user calls remove
// before calling hasNext() or next() (although they shouldn't)
lastUsedIterator = currentIterator;
}
while (! currentIterator.hasNext() && currentIteratorIndex < iterators.length - 1) {
currentIteratorIndex++;
currentIterator = iterators[currentIteratorIndex];
}
}
public boolean hasMoreElements() {
return hasNext();
}
public Object nextElement() {
return next();
}
}
public static class IteratorEnumeration<T> implements Enumeration<T> {
private Iterator<T> iterator;
public IteratorEnumeration(Iterator<T> iterator){
this.iterator = iterator;
}
public boolean hasMoreElements(){
return iterator.hasNext();
}
public T nextElement() throws NoSuchElementException {
return iterator.next();
}
}
}
|