class SortIntAttribute extends IlpComputedAttribute {
IlpAttribute arrayAttribute;
public SortIntAttribute(String name,
IlpAttributeGroup model,
IlpAttribute arrayAttribute) {
super(name, model, arrayAttribute.getValueClass());
this.arrayAttribute = arrayAttribute;
}
public Object getValue(IlpAttributeValueHolder h) {
Object value = h.getAttributeValue(arrayAttribute);
if (value != IlpAttributeValueHolder.VALUE_NOT_SET) {
int[] array = (int [])h.getAttributeValue(arrayAttribute);
array = (int [])array.clone();
Arrays. sort(array);
return array;
}
return IlpAttributeValueHolder.VALUE_NOT_SET;
}
public boolean isDependentOn (IlpAttribute a) {
return a == arrayAttribute;
}
}
The constructor of
SortIntAttributes
takes three arguments:
name
is the name of the attribute,
model
specifies the attribute group (usually, an
IlpClass
),
arrayAttribute
specifies the attribute that the computed attribute depends
on.
The constructor of the abstract class
IlpComputedAttribute takes three
arguments: the name, the model, and the Java class of the
value it returns. If the last argument is set to
null
, the computed attribute returns
java.lang.Object
. In this example, this argument is specified and
corresponds to the value class of the attribute on which the
computed attribute depends.
The
getValue
method computes the attribute value. Computation is protected.
As a consequence, if the value of the attribute on which the
computed attribute depends is not initialized, or in other
words if its value is
IlpAttributeValueHolder, the method does
not perform the computation and returns
IlpAttributeValueHolder.VALUE_NOT_SET
. In this example, when the value is set, the returned array is
duplicated, sorted, and returned.
The implementation of the
isDependentOn
method is quite straightforward, since in this example the
computed attribute depends only on one other attribute, which
is specified in the constructor.