FileDocCategorySizeDatePackage
AssertionSet.javaAPI DocExample15652Tue May 29 16:56:34 BST 2007com.sun.xml.ws.policy

AssertionSet

public final class AssertionSet extends Object implements Comparable, Iterable
The AssertionSet is a set of assertions. It represents a single policy alternative.
author
Fabian Ritzmann, Marek Potociar

Fields Summary
private static final AssertionSet
EMPTY_ASSERTION_SET
private static final Comparator
ASSERTION_COMPARATOR
The comparator comapres policy assertions according to their publicly accessible attributes, in the following order of attributes: 1. namespace (not null String) 2. local name (not null String) 3. value (String): null < "" < "not empty" 4. has nested assertions (boolean): false < true 5. has nested policy (boolean): false < true 6. hashCode comparison
private final List
assertions
private final Set
vocabulary
private final Collection
immutableVocabulary
Constructors Summary
private AssertionSet(List list)

    
       
        assert (list != null) : LocalizationMessages.WSP_0037_PRIVATE_CONSTRUCTOR_DOES_NOT_TAKE_NULL();
        this.assertions = list;
    
private AssertionSet(Collection alternatives)

        this.assertions = new LinkedList<PolicyAssertion>();
        for (AssertionSet alternative : alternatives) {
            addAll(alternative.assertions);
        }
    
Methods Summary
private booleanadd(PolicyAssertion assertion)

        if (assertion == null) {
            return false;
        }
        
        if (this.assertions.contains(assertion)) {
            return false;
        } else {
            this.assertions.add(assertion);
            this.vocabulary.add(assertion.getName());
            return true;
        }
    
private booleanaddAll(java.util.Collection assertions)

        boolean result = true;
        
        if (assertions != null) {
            for (PolicyAssertion assertion : assertions) {
                result &= add(assertion); // this is here to ensure that vocabulary is built correctly as well
            }
        }
        
        return result;
    
public intcompareTo(com.sun.xml.ws.policy.AssertionSet that)
An {@code Comparable.compareTo(T o)} interface method implementation.

        if (this.equals(that)) {
            return 0;
        }
        
        // comparing vocabularies
        final Iterator<QName> vIterator1 = this.getVocabulary().iterator();
        final Iterator<QName> vIterator2 = that.getVocabulary().iterator();
        while (vIterator1.hasNext()) {
            final QName entry1 = vIterator1.next();
            if (vIterator2.hasNext()) {
                final QName entry2 = vIterator2.next();
                final int result = PolicyUtils.Comparison.QNAME_COMPARATOR.compare(entry1, entry2);
                if (result != 0) {
                    return result;
                }
            } else {
                return 1; // we have more entries in this vocabulary
            }
        }
        
        if (vIterator2.hasNext()) {
            return -1;  // we have more entries in that vocabulary
        }
        
        // vocabularies are equal => comparing assertions
        final Iterator<PolicyAssertion> pIterator1 = this.getAssertions().iterator();
        final Iterator<PolicyAssertion> pIterator2 = that.getAssertions().iterator();
        while (pIterator1.hasNext()) {
            final PolicyAssertion pa1 = pIterator1.next();
            if (pIterator2.hasNext()) {
                final PolicyAssertion pa2 = pIterator2.next();
                final int result = ASSERTION_COMPARATOR.compare(pa1, pa2);
                if (result != 0) {
                    return result;
                }
            } else {
                return 1; // we have more entries in this assertion set
            }
        }
        
        if (pIterator2.hasNext()) {
            return -1;  // we have more entries in that assertion set
        }
        
        // seems like objects are very simmilar although not equal => we must not return 0 otherwise the TreeSet
        // holding this element would discard the newly added element. Thus we return that the first argument is
        // greater than second (just because it is first...)
        return 1;
    
public booleancontains(javax.xml.namespace.QName assertionName)
Returns true if the assertion set contains the assertion name specified in its vocabulary

param
assertionName the fully qualified name of the assertion
return
{@code true}, if an assertion with the given name could be found in the assertion set vocabulary {@code false} otherwise.

        return vocabulary.contains(assertionName);
    
public static com.sun.xml.ws.policy.AssertionSetcreateAssertionSet(java.util.Collection assertions)
Creates and returns new assertion set holding a set of provided policy assertions.

param
assertions collection of provided policy assertions to be stored in the assertion set. May be {@code null}.
return
new instance of assertion set holding the provided policy assertions

        if (assertions == null || assertions.isEmpty()) {
            return EMPTY_ASSERTION_SET;
        }
        
        final AssertionSet result = new AssertionSet(new LinkedList<PolicyAssertion>());
        result.addAll(assertions);
        Collections.sort(result.assertions, ASSERTION_COMPARATOR);
        
        return result;
    
public static com.sun.xml.ws.policy.AssertionSetcreateMergedAssertionSet(java.util.Collection alternatives)
Creates and returns new assertion set holding content of all provided policy assertion sets.

This method should not be used to perform a merge of general Policy instances. A client should be aware of the method's result meaning and the difference between merge of Policy instances and merge of AssertionSet instances.

param
alternatives collection of provided policy assertion sets which content is to be stored in the assertion set. May be {@code null} - empty assertion set is returned in such case.
return
new instance of assertion set holding the content of all provided policy assertion sets.

        if (alternatives == null || alternatives.isEmpty()) {
            return EMPTY_ASSERTION_SET;
        }
        
        final AssertionSet result = new AssertionSet(alternatives);
        Collections.sort(result.assertions, ASSERTION_COMPARATOR);
        
        return result;
    
public static com.sun.xml.ws.policy.AssertionSetemptyAssertionSet()

        return EMPTY_ASSERTION_SET;
    
public booleanequals(java.lang.Object obj)
An {@code Object.equals(Object obj)} method override.

        if (this == obj) {
            return true;
        }
        
        if (!(obj instanceof AssertionSet)) {
            return false;
        }
        
        final AssertionSet that = (AssertionSet) obj;        
        boolean result = true;
        
        result = result && this.vocabulary.equals(that.vocabulary);
        result = result && this.assertions.size() == that.assertions.size() && this.assertions.containsAll(that.assertions);
        
        return result;
    
public java.util.Collectionget(javax.xml.namespace.QName name)
Searches for assertions with given name. Only assertions that are contained as immediate children of the assertion set are searched, i.e. nested policies are not searched.

param
name The fully qualified name of searched assertion
return
List of all assertions matching the requested name. If no assertions are found, the returned list is empty (i.e. {@code null} value is never returned).

        final List<PolicyAssertion> matched = new LinkedList<PolicyAssertion>();
        
        if (vocabulary.contains(name)) {
            // we iterate the assertion set only if we are sure we contain such assertion name in our vocabulary
            for (PolicyAssertion assertion : assertions) {
                if (assertion.getName().equals(name)) {
                    matched.add(assertion);
                }
            }
        }
        
        return matched;
    
java.util.CollectiongetAssertions()
Return all assertions contained in this assertion set.

return
All assertions contained in this assertion set

        return assertions;
    
java.util.CollectiongetVocabulary()
Retrieves the vocabulary of this policy expression. The vocabulary is represented by an immutable collection of unique QName objects. Each of those objects represents single assertion type contained in the assertion set.

return
immutable collection of assertion types contained in the assertion set (a policy vocabulary).

        return immutableVocabulary;
    
public inthashCode()
An {@code Object.hashCode()} method override.

        int result = 17;
        
        result = 37 * result + vocabulary.hashCode();
        result = 37 * result + assertions.hashCode();
        
        return result;
    
booleanisCompatibleWith(com.sun.xml.ws.policy.AssertionSet alternative)
Checks whether this policy alternative is compatible with the provided policy alternative.

param
alternative policy alternative used for compatibility test
return
{@code true} if the two policy alternatives are compatible, {@code false} otherwise

        return this.vocabulary.equals(alternative.vocabulary);
    
public booleanisEmpty()
Returns {@code true} if this assertion set contains no assertions.

return
{@code true} if this assertion set contains no assertions.

        return assertions.isEmpty();
    
public java.util.Iteratoriterator()
Returns an iterator over a set of child policy assertion objects.

return
policy assertion Iterator.

        return this.assertions.iterator();
    
public java.lang.StringtoString()
An {@code Object.toString()} method override.

        return toString(0, new StringBuffer()).toString();
    
java.lang.StringBuffertoString(int indentLevel, java.lang.StringBuffer buffer)
A helper method that appends indented string representation of this instance to the input string buffer.

param
indentLevel indentation level to be used.
param
buffer buffer to be used for appending string representation of this instance
return
modified buffer containing new string representation of the instance

        final String indent = PolicyUtils.Text.createIndent(indentLevel);
        final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
        
        buffer.append(indent).append("assertion set {").append(PolicyUtils.Text.NEW_LINE);
        
        if (assertions.isEmpty()) {
            buffer.append(innerIndent).append("no assertions").append(PolicyUtils.Text.NEW_LINE);
        } else {
            for (PolicyAssertion assertion : assertions) {
                assertion.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
            }
        }
        
        buffer.append(indent).append('}");
        
        return buffer;