GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.6 -r1.7
--- IIdentifierField.java 1 Nov 2007 00:49:07 -0000 1.6
+++ IIdentifierField.java 17 Nov 2010 00:00:54 -0000 1.7
@@ -6,7 +6,7 @@
* an ordered collection of these fields.
*
* @author Jeremy McCormick
- * @version $Id: IIdentifierField.java,v 1.6 2007/11/01 00:49:07 jeremy Exp $
+ * @version $Id: IIdentifierField.java,v 1.7 2010/11/17 00:00:54 jeremy Exp $
*/
public interface IIdentifierField
{
@@ -15,37 +15,37 @@
* @return The label.
*/
public String getLabel();
-
+
/**
* Get the number of bits in this field.
* @return The number of bits in this field.
*/
public int getNumberOfBits();
-
+
/**
* Get offset, or starting position, of this field.
* @return The offset of the field.
*/
public int getOffset();
-
+
/**
* Get mask on as long value.
* @return The on mask as a long.
*/
public long getLongMask();
-
+
/**
* Get mask on as int value.
* @return The on mask on an int.
*/
public int getIntegerMask();
-
+
/**
* True if field is capable of holding signed values.
* @return True if field is signed.
*/
public boolean isSigned();
-
+
/**
* Get the order of this field in the id, or -1 if the
* ordering is unknown.
@@ -53,21 +53,21 @@
* @return The order this field in the id.
*/
public int getOrder();
-
+
/**
* Unpack and return the value of this field from a 64-bit {@link IIdentifier}.
*
* @return Field's int value.
*/
- public int unpack(IIdentifier id);
-
+ public int unpack( IIdentifier id );
+
/**
* Unpack and return the value of this field from a raw long.
*
* @return Field's int value.
- */
- public int unpack( long value );
-
+ */
+ public int unpack( long value );
+
/**
* Pack a single value of this field into a {@link IIdentifier}.
*
@@ -75,16 +75,16 @@
* @return An identifier with the packed value.
*/
public IIdentifier pack( int value );
-
+
/**
* Pack a single value this field into an existing {@link IIdentifier},
* preserving the other field values that may be present.
*
* @param value The field value.
* @param id An Identifier.
- */
- public void pack( int value, IIdentifier id );
-
+ */
+ public void pack( int value, IIdentifier id );
+
/**
* Get the field value from an {@link IExpandedIdentifier}.
*
@@ -92,5 +92,23 @@
* @return The field value.
* @throws IllegalArgumentException if field index is not valid for the id.
*/
- public int getFieldValue( IExpandedIdentifier id );
+ public int getFieldValue( IExpandedIdentifier id );
+
+ /**
+ * Get the maximum inclusive value that can be stored in this field.
+ * @return The maximum field value.
+ */
+ public int getMaxValue();
+
+ /**
+ * Get the minimum inclusive value that can be stored in this field.
+ * @return The minimum field value.
+ */
+ public int getMinValue();
+
+ /**
+ * True if value is within valid range of field values; false if not.
+ * @return True if value is valid; false if not.
+ */
+ public boolean isValidValue( int value );
}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.5 -r1.6
--- IdentifierField.java 1 Nov 2007 00:49:07 -0000 1.5
+++ IdentifierField.java 17 Nov 2010 00:00:54 -0000 1.6
@@ -2,9 +2,9 @@
/**
* Implementation of {@link IIdentifierField}.
- *
+ *
* @author Jeremy McCormick
- * @version $Id: IdentifierField.java,v 1.5 2007/11/01 00:49:07 jeremy Exp $
+ * @version $Id: IdentifierField.java,v 1.6 2010/11/17 00:00:54 jeremy Exp $
*/
public class IdentifierField implements IIdentifierField
@@ -14,53 +14,72 @@
private boolean isSigned;
private String label;
private int order;
- long longMask;
- int intMask;
+ private int maxValue;
+ private int minValue;
+ private long longMask;
+ private int intMask;
+
+ private static final int MAX_OFFSET = 64;
+ private static final int MIN_OFFSET = 0;
+ private static final int MAX_BITS = 32;
+ private static final int MIN_BITS = 1;
- private static final int MAX_OFFSET=64;
- private static final int MIN_OFFSET=0;
- private static final int MAX_BITS=32;
- private static final int MIN_BITS=1;
-
- public IdentifierField(String label, int numberOfBits, int offset, boolean isSigned, int order)
+ public static class ValueOutOfRangeException extends RuntimeException
+ {
+ public ValueOutOfRangeException(int value, IIdentifierField field)
+ {
+ super("The value <" + value + "> is outside range <" + field.getMinValue() + ", " + field.getMaxValue() + "> of field <" + field.getLabel() + "> which has <" + field.getNumberOfBits() + "> bits.");
+ }
+ }
+
+ public IdentifierField( String label, int numberOfBits, int offset, boolean isSigned, int order)
{
if ( label == null )
{
- throw new IllegalArgumentException(label + " - label is null.");
+ throw new IllegalArgumentException( label + " - label is null." );
}
-
+
this.label = label;
-
+
if ( numberOfBits > MAX_BITS || numberOfBits < MIN_BITS )
{
- throw new IllegalArgumentException(label + " - number of bits is not between " + MIN_BITS + " and " + MAX_BITS);
+ throw new IllegalArgumentException( label + " - number of bits is not between " + MIN_BITS + " and " + MAX_BITS );
}
-
+
this.numberOfBits = numberOfBits;
-
+
if ( offset > MAX_OFFSET || offset < MIN_OFFSET )
{
- throw new IllegalArgumentException(label + " - offset <" + offset + "> is not between " + MIN_OFFSET + " and " + MAX_OFFSET);
+ throw new IllegalArgumentException( label + " - offset <" + offset + "> is not between " + MIN_OFFSET + " and " + MAX_OFFSET );
}
-
+
if ( (offset + numberOfBits) > MAX_OFFSET )
{
- throw new IllegalArgumentException(label + " - offset + numberOfBits <" + (offset + numberOfBits) + "> is greater than " + MAX_OFFSET);
+ throw new IllegalArgumentException( label + " - offset + numberOfBits <" + (offset + numberOfBits) + "> is greater than " + MAX_OFFSET );
}
-
- if (isSigned && numberOfBits < 2)
+
+ if ( isSigned && numberOfBits < 2 )
{
- throw new IllegalArgumentException("The signed field " + label + " needs at least 2 bits.");
+ throw new IllegalArgumentException( "The signed field " + label + " needs at least 2 bits." );
}
-
+
this.offset = offset;
- this.isSigned = isSigned;
+ this.isSigned = isSigned;
this.order = order;
-
+
this.intMask = (1 << numberOfBits) - 1;
- this.longMask = ((1L<<numberOfBits) - 1);
+ this.longMask = ((1L << numberOfBits) - 1);
+
+ maxValue = ((int) Math.pow( 2, getNumberOfBits() )) - 1;
+ minValue = 0;
+ if ( isSigned() )
+ {
+ // In a signed field, one bit is reserved for the sign bit.
+ maxValue = ((int) Math.pow( 2, getNumberOfBits() - 1 )) - 1;
+ minValue = -maxValue;
+ }
}
-
+
public String getLabel()
{
return label;
@@ -80,68 +99,83 @@
{
return isSigned;
}
-
+
public int getOrder()
{
return order;
- }
-
+ }
+
public String toString()
{
return getOrder() + ":" + getLabel() + ":" + getOffset() + ":" + getNumberOfBits() + ":" + isSigned() + "\n";
}
-
+
// FIXME: Move to IdentifierUtil.
public int unpack( long value )
- {
+ {
int start = getOffset();
int length = getNumberOfBits();
int mask = getIntegerMask();
-
+
int result = (int) ((value >> start) & mask);
- if (isSigned())
+ if ( isSigned() )
{
- int signBit = 1<<(length-1);
- if ((result & signBit) != 0) result -= (1<<length);
+ int signBit = 1 << (length - 1);
+ if ( (result & signBit) != 0 )
+ result -= (1 << length);
}
-
+
return result;
}
-
+
// FIXME: Move to IdentifierUtil.
public int unpack( IIdentifier compact )
- {
- return unpack( compact.getValue());
- }
-
+ {
+ return unpack( compact.getValue() );
+ }
+
// FIXME: Move to IdentifierUtil.
public IIdentifier pack( int value )
{
+ if ( !isValidValue(value) )
+ throw new ValueOutOfRangeException( value, this );
long result = 0;
- int start = getOffset();
+ int start = getOffset();
long mask = getLongMask();
result |= (mask & value) << start;
- return new Identifier(result);
+ return new Identifier( result );
}
-
+
// FIXME: Move to IdentifierUtil.
public void pack( int value, IIdentifier id )
{
+ if ( !isValidValue(value) )
+ throw new ValueOutOfRangeException( value, this );
long result = id.getValue();
- int start = getOffset();
+ int start = getOffset();
long mask = getLongMask();
result |= (mask & value) << start;
- id.setValue(result);
+ id.setValue( result );
}
-
+
// FIXME: Move to IdentifierUtil.
public int getFieldValue( IExpandedIdentifier id )
{
- if ( !id.isValidIndex(order))
+ if ( !id.isValidIndex( order ) )
{
- throw new IllegalArgumentException("Index of the field <" + getLabel() + "> is not valid for this id.");
+ throw new IllegalArgumentException( "Index of the field <" + getLabel() + "> is not valid for this id." );
}
- return id.getValue(order);
+ return id.getValue( order );
+ }
+
+ public int getMaxValue()
+ {
+ return maxValue;
+ }
+
+ public int getMinValue()
+ {
+ return minValue;
}
public int getIntegerMask()
@@ -153,4 +187,9 @@
{
return this.longMask;
}
+
+ public boolean isValidValue(int value)
+ {
+ return value < getMaxValue() && value > getMinValue();
+ }
}
\ No newline at end of file