java 代码
- /**
- * <p>
- * This method simply orders the ranked query result by date.
- * </p>
- *
- * @param result ranked result to be ordered
- *
- * @return ordered result
- *
- * @throws OrderException if the order execution fails because of some reason.
- */
- public RankedQueryResult order(RankedQueryResult result) throws OrderException {
- SearcherHelper.checkNull(result, "result");
- // TreeMap(Key date, Value id) may be a choice to implement sort. But it has a problem. The
- // Date of two Documents may be equal. In that case, when put them into the Map,
- // one Document will be neglected and a bug is introduced.
- // Also, I tried Arrays.sort(rankedOrder,new Comparator(){}. But in this
- // case, we can not throw the DocumentRepositoryException from Document Repository in Comparator#compare.
- // What's more, every time it compares Date, it'll have to retrieve the Date from // the repository.
- // Finally, I decided to implement it this way.
- /**
- * An Object for WordSourceId Date pair for the convenience of sorting WordSourceId by Date.
- */
- final class IdDateObject {
- /** The WordSourceId this object related */
- private WordSourceId id;
- /** The Date of the WordSourceId */
- private Date date;
- /**
- * Constructor for the project with the related WordSourceId and its date.
- *
- * @param id the WordSourceId this object related
- * @param date the Date of the WordSourceId
- */
- IdDateObject(WordSourceId id, Date date) {
- this.id = id;
- this.date = date;
- }
- /**
- * Returns the date of the WordSourceId.
- *
- * @return the date of the WordSourceId
- */
- Date getDate() {
- return date;
- }
- /**
- * Returns the WordSourceId the object related.
- *
- * @return the WordSourceId the object related
- */
- WordSourceId getId() {
- return id;
- }
- }// end for final class IdDateObject
- // constructs the IdDateObject array
- WordSourceId[] rankedOrder = result.getRankedOrder();
- IdDateObject[] idDateObjects = new IdDateObject[rankedOrder.length];
- for (int i = 0; i < rankedOrder.length; i++) {
- idDateObjects[i] = new IdDateObject(rankedOrder[i], getDocumentDate(rankedOrder[i]));
- }
- // sort in idDateObjects
- Arrays.sort(idDateObjects, new Comparator() {
- /**
- * Compares two IdDateObject objects according to the Date.
- * <p>
- *
- * <pre>
- * If the local variable decreasing is true:
- * If two objects' creation Dates are equal, 0 will be returned.
- * If arg0 greater than arg1, a positive int is returned.
- * Else a negative is returned.
- * Else the local variable decreasing is false:
- * If two objects' creation Dates, 0 will be returned.
- * If arg0 greater than arg1, a negitive integer is returned.
- * Else a positive integer is returned.
- * </pre>
- *
- * @param arg0 the first WordSourceId to compare
- * @param arg1 the second WordSourceId to compare
- * @return an integer representing the compare result according the above rule
- * @throws ClassCastException if the arguments' types prevent them from being compared by this Comparator.
- */
- public int compare(Object arg0, Object arg1) {
- IdDateObject object1 = (IdDateObject) arg0;
- IdDateObject object2 = (IdDateObject) arg1;
- Date date0 = object1.getDate();
- Date date1 = object2.getDate();
- int result = date0.compareTo(date1);
- if (decreasing) {
- result *= -1;
- }
- return result;
- }
- });
- // retrieve sort result
- WordSourceId[] resultOrder = new WordSourceId[rankedOrder.length];
- for (int i = 0; i < idDateObjects.length; i++) {
- resultOrder[i] = idDateObjects[i].getId();
- }
- // create and return a new ranked query result ordered by date
- return new RankedQueryResult(result.getResult(), result.getAllRelevances(), resultOrder);
- }