* Three levels are defined that are outside the scope system: the "not in list" value, which is defined
* as the (integer) value -1; the "no access" value, which is greater than any value in the lowband or
* highband of any scope; and the "unrestricted user" value, which is greater than any value in the lowband
* of any scope, but less than any value in the highband of any scope. (Administrator levels are generally
* defined within the highband of their scope, allowing administrators to exert control over any enclosed
* objects as well.)
*
* @author Eric J. Bowersox <erbo@silcom.com>
* @version X
*/
public final class ScopeInfo implements Cloneable, Comparable
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static final int[] LB_LOW = // Scope values, lowband, low end of range
{ 0, 2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 22000, 24000, 26000, 28000,
30000 };
private static final int[] LB_HIGH = // Scope values, lowband, high end of range
{ 1999, 3999, 5999, 7999, 9999, 11999, 13999, 15999, 17999, 19999, 21999, 23999, 25999, 27999, 29999,
31999 };
private static final int[] HB_LOW = // Scope values, highband, low end of range
{ 63000, 61000, 59000, 57000, 55000, 53000, 51000, 49000, 47000, 45000, 43000, 41000, 39000, 37000,
35000, 33000 };
private static final int[] HB_HIGH = // Scope values, highband, high end of range
{ 64999, 62999, 60999, 58999, 56999, 54999, 52999, 50999, 48999, 46999, 44999, 42999, 40999, 38999,
36999, 34999 };
/**
* The defined "not there" value.
*/
public static final int L_NOT_THERE = -1;
/**
* The defined "unrestricted user" value.
*/
public static final int L_UNRESTRICTED = 32500;
/**
* The defined "no access" value.
*/
public static final int L_NO_ACCESS = 65500;
/**
* A value which represents the difference between the low end and high end of a single band.
*/
public static final int BAND_WIDTH = 1999;
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
int scope; // the scope value
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
/**
* Creates a new ScopeInfo
object.
*
* @param scope The scope value.
* @exception java.lang.IndexOutOfBoundsException The given scope value is out of range. It must be
* between 0 and 15.
*/
public ScopeInfo(int scope)
{
if ((scope<0) || (scope>=LB_LOW.length))
throw new IndexOutOfBoundsException("invalid scope value");
this.scope = scope;
} // end constructor
/*--------------------------------------------------------------------------------
* Overrides from class Object
*--------------------------------------------------------------------------------
*/
/**
* Tests this object for equality with another object. Two ScopeInfo
objects are equivalent
* iff the scopes that they enclose are equal.
*
* @param obj The other object to compare to this one.
* @return true
if the two objects are equal.
*/
public boolean equals(Object o)
{
if ((o==null) || !(o instanceof ScopeInfo))
return false;
ScopeInfo other = (ScopeInfo)o;
return (scope==other.scope);
} // end equals
/**
* Returns a hash code that can be used as an index for this object in a hash table. In keeping
* with the definition of equals
, a ScopeInfo
's hash code is its scope.
*
* @return The hash code for this object.
* @see #equals(java.lang.Object)
*/
public int hashCode()
{
return scope;
} // end hashCode
/**
* Returns a "debugging" string equivalent for this ScopeInfo
.
*
* @return The string equivalent for this ScopeInfo
.
*/
public String toString()
{
StringBuffer buf = new StringBuffer("{ScopeInfo(");
buf.append(scope).append("): ranges [").append(LB_LOW[scope]).append('-').append(LB_HIGH[scope]);
buf.append("], [").append(HB_LOW[scope]).append('-').append(HB_HIGH[scope]).append("]}");
return buf.toString();
} // end toString
/*--------------------------------------------------------------------------------
* Implementations from interface Comparable
*--------------------------------------------------------------------------------
*/
/**
* Compares one ScopeInfo
object to another. A ScopeInfo
is logically less
* than another one if its scope is less than the other one's scope.
*
* @param obj The other ScopeInfo
object to compare to.
* @return A value less than 0 if this ScopeInfo
is logically "less than" the specified one; a
* value greater than 0 if this ScopeInfo
is logically "greater than" the other one;
* 0 if the two are equal.
* @exception java.lang.ClassCastException If obj
is not a ScopeInfo
object.
*/
public int compareTo(Object o)
{
if (o==null)
throw new NullPointerException("can't compare to a null object");
ScopeInfo other = (ScopeInfo)o; // may throw ClassCastException - that's OK
return scope - other.scope;
} // end compareTo
/*--------------------------------------------------------------------------------
* External operations
*--------------------------------------------------------------------------------
*/
/**
* Returns the scope associated with this ScopeInfo
object.
*
* @return The scope associated with this ScopeInfo
object.
*/
public final int getScope()
{
return scope;
} // end getScope
/**
* Returns the lower limit of the lowband of this scope.
*
* @return The lower limit of the lowband of this scope.
*/
public final int getLowBandLow()
{
return LB_LOW[scope];
} // end getLowBandLow
/**
* Returns the upper limit of the lowband of this scope.
*
* @return The upper limit of the lowband of this scope.
*/
public final int getLowBandHigh()
{
return LB_HIGH[scope];
} // end getLowBandHigh
/**
* Returns the lower limit of the highband of this scope.
*
* @return The lower limit of the highband of this scope.
*/
public final int getHighBandLow()
{
return HB_LOW[scope];
} // end getHighBandLow
/**
* Returns the upper limit of the highband of this scope.
*
* @return The upper limit of the highband of this scope.
*/
public final int getHighBandHigh()
{
return HB_HIGH[scope];
} // end getHighBandHigh
/**
* Computes a new level within the scope.
*
* @param highband true
to return a value within the highband of this scope, false
* to return a value within a lowband of this scope.
* @param offset Offset from the lower limit of the band. If this value is negative, it is viewed as
* an offset from the upper limit of the band.
* @return The computed level.
* @exception java.lang.IllegalArgumentException If the computed level would be outside this scope.
*/
public final int getLevel(boolean highband, int offset)
{
int rc; // return from this method
if (highband)
{ // it's in the highband
if (offset<0)
{ // negative offset from high end of highband
rc = HB_HIGH[scope] + offset;
if (rctrue
if the specified level represents a value within this scope,
* false
if not.
*
* @param value The level value to be tested.
* @return true
if the specified level represents a value within this scope.
*/
public final boolean isInScope(int value)
{
if ((value>=LB_LOW[scope]) && (value<=LB_HIGH[scope]))
return true;
if ((value>=HB_LOW[scope]) && (value<=HB_HIGH[scope]))
return true;
return false;
} // end isInScope
/*--------------------------------------------------------------------------------
* External static operations
*--------------------------------------------------------------------------------
*/
/**
* Returns true
if the specified index represents a valid scope, false
if not.
*
* @param s The scope index to be tested.
* @return true
if the specified index represents a valid scope.
*/
public static final boolean isValidScope(int s)
{
return ((s>=0) && (s