Version 1 (modified by mitex, 16 years ago) (diff) |
---|
Analysis
Overview
This task should provide the ability to have priority among simple operations.
Task requirements
- The operations must have a sort order key(equivalent to their priority).
- The sortKey of an operation should be accessible through the class org.sophie2.core.mvc.SimpleOperation.
- The extension point will sort the registered extensions according to this key.
Task result
The result of this task should be:
- source code
Implementation idea
- Create an annotation which would mark that an existing operation has a sort order key.
- Add functionality to get the sortKey of an operation in the class org.sophie2.core.mvc.SimpleOperation. Using Reflection get the sort order key through the annotation if it is available for this operation otherwise assume that the sortKey is an empty string. This makes certain that all operations which have not provided a sortKey will have equal prioryty when they are rigisterred as extensions.
- Assure that the extension point will sort the registered extensions according to the sort order key. Override the method getSorter() from the class SophieExtensionPoint<T> in org.sophie2.core.mvc.CoreMvcModule, to provide a proper Comparator which would compare the operations acording to their sorKey if such is available.
Related
How to demo
Design
- A new annotation will be introduced. Using this annotation an operation will define its sort order key and this assures that different operations which have similar event filters can have different priority.
/
- Annotation that marks an operation which can be sorted using its sortKey. * */
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Sortable {
/
- The sorting key for the field.
- @return The sorting key for the operation. */
String sortKey();
}
- The class org.sophie2.core.mvc.SimpleOperation will be modified. A method getSortKey() will be added. It will return the sort order key of the current SimpleOperation object. If the operation hasn't defined sortKey through the Sortable annotation the method will return an empty string, otherwise the method should return the corresponding sort order key. This is achieved in the following manner - we obtain the operationDef object - corresponding to the particular operation, then we get from the enumeration the appropriate enum field representing the current SimpleOperation and finally we obtain its sortkey if such is available.
/
- Gets the sorting key for this operation.
- @return The sorting key of this SimpleOperation or an
- empty string if the operation has not provided a sorting key. */
public String getSortKey() {
String sortKey = "";
Enum<?> op = (Enum<?>) this.opDef; try {
Field field = this.opDef.getClass().getField(op.name()); Sortable s = field.getAnnotation(Sortable.class); if(s != null)
sortKey = s.sortKey();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return sortKey;
}
- The class org.sophie2.core.mvc will be modified. In the existing public static class OperationPoint which extends SophieExtensionPoint<OperationR3> we will overload the method getSorter() inherited from SophieExtensionPoint<T> located in org.sophie2.core.modularity.
@Override protected Comparator<SophieExtension<OperationR3>> getSorter() {
return new Comparator<SophieExtension<OperationR3>>() {
public int compare(SophieExtension<OperationR3> se1, SophieExtension<OperationR3> se2) {
if((se1.getObject() instanceof SimpleOperation)&& (se2.getObject() instanceof SimpleOperation)) {
SimpleOperation so1 = (SimpleOperation)se1.getObject(); SimpleOperation so2 = (SimpleOperation)se2.getObject();
return so1.getSortKey().compareTo(so2.getSortKey());
}
return 0;
}
};
}
Implementation
- Done according to design.