net.jini.core.constraint
Class ConstraintAlternatives

java.lang.Object
  extended by net.jini.core.constraint.ConstraintAlternatives
All Implemented Interfaces:
Serializable, InvocationConstraint, RelativeTimeConstraint

public final class ConstraintAlternatives
extends Object
implements RelativeTimeConstraint, Serializable

Combines two or more constraint alternatives into a single overall constraint. The semantics of this aggregate constraint are that at least one of the individual constraint alternatives must be satisfied. The alternatives do not have to be instances of the same type, but they cannot themselves be ConstraintAlternatives instances.

Note that this class implements RelativeTimeConstraint even though the constraint elements might not implement RelativeTimeConstraint.

An instance containing an exhaustive list of alternatives (for example, an instance containing both ClientAuthentication.YES and ClientAuthentication.NO) serves no useful purpose, as a requirement or as a preference. A don't care condition should be expressed by the absence of constraints.

Since:
2.0
Author:
Sun Microsystems, Inc.
See Also:
Serialized Form

Field Summary
private  InvocationConstraint[] constraints
          The alternative constraints.
private  boolean rel
          Indicates whether any of the constraints are based on relative time.
private static ObjectStreamField[] serialPersistentFields
           
private static long serialVersionUID
           
 
Constructor Summary
  ConstraintAlternatives(Collection c)
          Creates an instance containing the specified alternative constraints, with duplicate constraints removed.
  ConstraintAlternatives(InvocationConstraint[] constraints)
          Creates an instance containing the specified alternative constraints, with duplicate constraints removed.
private ConstraintAlternatives(InvocationConstraint[] constraints, boolean allAbs)
          Creates a constraint containing the specified alternative constraints, and computes the rel field if allAbs is false.
 
Method Summary
static InvocationConstraint create(Collection c)
          Returns a constraint representing the specified alternative constraints, with duplicate constraints removed.
static InvocationConstraint create(InvocationConstraint[] constraints)
          Returns a constraint representing the specified alternative constraints, with duplicate constraints removed.
 Set elements()
          Returns an immutable set of all of the constraints.
 boolean equals(Object obj)
          Two instances of this class are equal if they have the same constraints (ignoring order).
(package private)  InvocationConstraint[] getConstraints()
          Returns the elements, without copying.
 int hashCode()
          Returns a hash code value for this object.
 InvocationConstraint makeAbsolute(long baseTime)
          Returns a constraint equal to the result of taking the constraints in this instance, replacing each constraint that is an instance of RelativeTimeConstraint with the result of invoking that constraint's makeAbsolute method with the specified base time, and invoking the create method of this class with the revised collection of constraints.
private  void readObject(ObjectInputStream s)
          Verifies that there are at least two constraints, that none are null and none are instances of this class, and that there are no duplicates.
private static InvocationConstraint[] reduce(InvocationConstraint[] constraints)
          Verifies that the array has at least 2 elements, and that the elements are all non-null and not ConstraintAlternatives instances, removes duplicates, modifying the array in place, verifies that there are still at least 2 elements, and returns an array containing the remaining elements.
private static InvocationConstraint reduce(InvocationConstraint[] constraints, boolean allAbs)
          Verifies that the array is non-empty, and that the elements are all non-null and not ConstraintAlternatives instances.
private static int reduce0(InvocationConstraint[] constraints)
          Eliminates duplicates, modifying the array in place, and returns the resulting number of elements.
(package private)  boolean relative()
          Returns true if any of the constraints are relative, false otherwise.
private  void setRelative()
          Sets the rel field to true if any of the constraints are relative.
 String toString()
          Returns a string representation of this object.
private static void verify(InvocationConstraint[] constraints, int min)
          Verifies that the array has at least min elements, and that the elements are all non-null and not ConstraintAlternatives instances.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

serialPersistentFields

private static final ObjectStreamField[] serialPersistentFields

constraints

private final InvocationConstraint[] constraints
The alternative constraints.


rel

private transient boolean rel
Indicates whether any of the constraints are based on relative time.

Constructor Detail

ConstraintAlternatives

public ConstraintAlternatives(InvocationConstraint[] constraints)
Creates an instance containing the specified alternative constraints, with duplicate constraints removed. The argument passed to this constructor is neither modified nor retained; subsequent changes to that argument have no effect on the instance created.

Parameters:
constraints - the alternative constraints
Throws:
NullPointerException - if the argument is null or any element is null
IllegalArgumentException - if any of the elements are instances of ConstraintAlternatives, or if fewer than two elements remain after duplicate constraints are removed

ConstraintAlternatives

public ConstraintAlternatives(Collection c)
Creates an instance containing the specified alternative constraints, with duplicate constraints removed. The argument passed to this constructor is neither modified nor retained; subsequent changes to that argument have no effect on the instance created.

Parameters:
c - the alternative constraints
Throws:
NullPointerException - if the argument is null or any element is null
IllegalArgumentException - if any of the elements are instances of ConstraintAlternatives, or if the elements are not all instances of InvocationConstraint, or if fewer than two elements remain after duplicate constraints are removed

ConstraintAlternatives

private ConstraintAlternatives(InvocationConstraint[] constraints,
                               boolean allAbs)
Creates a constraint containing the specified alternative constraints, and computes the rel field if allAbs is false.

Method Detail

create

public static InvocationConstraint create(InvocationConstraint[] constraints)
Returns a constraint representing the specified alternative constraints, with duplicate constraints removed. If a single constraint remains after duplicates are removed, then that constraint is returned, otherwise an instance of ConstraintAlternatives containing the remaining constraints is returned. The argument passed to this method is neither modified nor retained; subsequent changes to that argument have no effect on the instance created.

Parameters:
constraints - the alternative constraints
Returns:
a constraint representing the specified alternative constraints, with duplicate constraints removed
Throws:
NullPointerException - if the argument is null or any element is null
IllegalArgumentException - if the argument is empty, or if any of the elements are instances of ConstraintAlternatives

create

public static InvocationConstraint create(Collection c)
Returns a constraint representing the specified alternative constraints, with duplicate constraints removed. If a single constraint remains after duplicates are removed, then that constraint is returned, otherwise an instance of ConstraintAlternatives containing the remaining constraints is returned. The argument passed to this method is neither modified nor retained; subsequent changes to that argument have no effect on the instance created.

Parameters:
c - the alternative constraints
Returns:
a constraint representing the specified alternative constraints, with duplicate constraints removed
Throws:
NullPointerException - if the argument is null or any element is null
IllegalArgumentException - if the argument is empty, or if any of the elements are instances of ConstraintAlternatives, or if the elements are not all instances of InvocationConstraint

setRelative

private void setRelative()
Sets the rel field to true if any of the constraints are relative.


relative

boolean relative()
Returns true if any of the constraints are relative, false otherwise.


reduce

private static InvocationConstraint reduce(InvocationConstraint[] constraints,
                                           boolean allAbs)
Verifies that the array is non-empty, and that the elements are all non-null and not ConstraintAlternatives instances. Removes duplicates and returns a single constraint if there's only one left, otherwise returns an ConstraintAlternatives containing the remaining constraints. The argument is modified in place.


verify

private static void verify(InvocationConstraint[] constraints,
                           int min)
Verifies that the array has at least min elements, and that the elements are all non-null and not ConstraintAlternatives instances.


reduce

private static InvocationConstraint[] reduce(InvocationConstraint[] constraints)
Verifies that the array has at least 2 elements, and that the elements are all non-null and not ConstraintAlternatives instances, removes duplicates, modifying the array in place, verifies that there are still at least 2 elements, and returns an array containing the remaining elements.


reduce0

private static int reduce0(InvocationConstraint[] constraints)
Eliminates duplicates, modifying the array in place, and returns the resulting number of elements.


elements

public Set elements()
Returns an immutable set of all of the constraints. Any attempt to modify this set results in an UnsupportedOperationException being thrown.

Returns:
an immutable set of all of the constraints

getConstraints

InvocationConstraint[] getConstraints()
Returns the elements, without copying.


makeAbsolute

public InvocationConstraint makeAbsolute(long baseTime)
Returns a constraint equal to the result of taking the constraints in this instance, replacing each constraint that is an instance of RelativeTimeConstraint with the result of invoking that constraint's makeAbsolute method with the specified base time, and invoking the create method of this class with the revised collection of constraints.

Specified by:
makeAbsolute in interface RelativeTimeConstraint
Parameters:
baseTime - an absolute time, specified in milliseconds from midnight, January 1, 1970 UTC
Returns:
a constraint that has the relative times converted to absolute times by adding the specified absolute time to each relative time

hashCode

public int hashCode()
Returns a hash code value for this object.

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Two instances of this class are equal if they have the same constraints (ignoring order).

Overrides:
equals in class Object

toString

public String toString()
Returns a string representation of this object.

Overrides:
toString in class Object

readObject

private void readObject(ObjectInputStream s)
                 throws IOException,
                        ClassNotFoundException
Verifies that there are at least two constraints, that none are null and none are instances of this class, and that there are no duplicates.

Throws:
InvalidObjectException - if there are less than two constraints, or any constraint is null or an instance of this class, or if there are duplicates
IOException
ClassNotFoundException


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.