Example usage for org.apache.commons.collections4.comparators ComparatorChain size

List of usage examples for org.apache.commons.collections4.comparators ComparatorChain size

Introduction

In this page you can find the example usage for org.apache.commons.collections4.comparators ComparatorChain size.

Prototype

public int size() 

Source Link

Document

Number of Comparators in the current ComparatorChain.

Usage

From source file:uk.ac.cam.cl.dtg.isaac.api.managers.GameManager.java

/**
 * Lookup gameboards belonging to a current user.
 * //w ww . ja v a  2 s .c  om
 * @param user
 *            - containing so that we can augment the response with personalised information
 * @param startIndex
 *            - the initial index to return.
 * @param limit
 *            - the limit of the number of results to return
 * @param showOnly
 *            - show only gameboards matching the given state.
 * @param sortInstructions
 *            - List of instructions of the form fieldName -> SortOrder. Can be null.
 * @return a list of gameboards (without full question information) which are associated with a given user.
 * @throws SegueDatabaseException
 *             - if there is a problem retrieving the gameboards from the database.
 * @throws ContentManagerException
 *             - if there is an error retrieving the content requested.
 */
public final GameboardListDTO getUsersGameboards(final RegisteredUserDTO user,
        @Nullable final Integer startIndex, @Nullable final Integer limit,
        @Nullable final GameboardState showOnly,
        @Nullable final List<Map.Entry<String, SortOrder>> sortInstructions)
        throws SegueDatabaseException, ContentManagerException {
    Validate.notNull(user);

    List<GameboardDTO> usersGameboards = this.gameboardPersistenceManager.getGameboardsByUserId(user);
    Map<String, Map<String, List<QuestionValidationResponse>>> questionAttemptsFromUser = questionManager
            .getQuestionAttemptsByUser(user);

    if (null == usersGameboards || usersGameboards.isEmpty()) {
        return new GameboardListDTO();
    }

    List<GameboardDTO> resultToReturn = Lists.newArrayList();

    Long totalCompleted = 0L;
    Long totalInProgress = 0L;
    Long totalNotStarted = 0L;

    // filter gameboards based on selection.
    for (GameboardDTO gameboard : usersGameboards) {
        this.augmentGameboardWithUserInformation(gameboard, questionAttemptsFromUser, user);

        if (null == showOnly) {
            resultToReturn.add(gameboard);
        } else if (gameboard.isStartedQuestion() && showOnly.equals(GameboardState.IN_PROGRESS)) {
            resultToReturn.add(gameboard);
        } else if (!gameboard.isStartedQuestion() && showOnly.equals(GameboardState.NOT_ATTEMPTED)) {
            resultToReturn.add(gameboard);
        } else if (gameboard.getPercentageCompleted() == 100 && showOnly.equals(GameboardState.COMPLETED)) {
            resultToReturn.add(gameboard);
        }

        // counts
        if (!gameboard.isStartedQuestion()) {
            totalNotStarted++;
        } else if (gameboard.getPercentageCompleted() == 100) {
            totalCompleted++;
        } else if (gameboard.isStartedQuestion()) {
            totalInProgress++;
        }
    }

    ComparatorChain<GameboardDTO> comparatorForSorting = new ComparatorChain<GameboardDTO>();
    Comparator<GameboardDTO> defaultComparitor = new Comparator<GameboardDTO>() {
        public int compare(final GameboardDTO o1, final GameboardDTO o2) {
            return o1.getLastVisited().getTime() > o2.getLastVisited().getTime() ? -1 : 1;
        }
    };

    // assume we want reverse date order for visited date for now.
    if (null == sortInstructions || sortInstructions.isEmpty()) {
        comparatorForSorting.addComparator(defaultComparitor);
    } else {
        // we have to use a more complex sorting Comparator.

        for (Map.Entry<String, SortOrder> sortInstruction : sortInstructions) {
            Boolean reverseOrder = false;
            if (sortInstruction.getValue().equals(SortOrder.DESC)) {
                reverseOrder = true;
            }

            if (sortInstruction.getKey().equals(CREATED_DATE_FIELDNAME)) {
                comparatorForSorting.addComparator((o1, o2) -> {
                    if (o1.getCreationDate().getTime() == o2.getCreationDate().getTime()) {
                        return 0;
                    } else {
                        return o1.getCreationDate().getTime() > o2.getCreationDate().getTime() ? -1 : 1;
                    }
                }, reverseOrder);
            } else if (sortInstruction.getKey().equals(VISITED_DATE_FIELDNAME)) {
                comparatorForSorting.addComparator((o1, o2) -> {
                    if (o1.getLastVisited().getTime() == o2.getLastVisited().getTime()) {
                        return 0;
                    } else {
                        return o1.getLastVisited().getTime() > o2.getLastVisited().getTime() ? -1 : 1;
                    }
                }, reverseOrder);
            } else if (sortInstruction.getKey().equals(TITLE_FIELDNAME)) {
                comparatorForSorting.addComparator((o1, o2) -> {
                    if (o1.getTitle() == null && o2.getTitle() == null) {
                        return 0;
                    }
                    if (o1.getTitle() == null) {
                        return 1;
                    }
                    if (o2.getTitle() == null) {
                        return -1;
                    }
                    return o1.getTitle().compareTo(o2.getTitle());
                }, reverseOrder);
            }
        }
    }

    if (comparatorForSorting.size() == 0) {
        comparatorForSorting.addComparator(defaultComparitor);
    }

    Collections.sort(resultToReturn, comparatorForSorting);

    int toIndex = startIndex + limit > resultToReturn.size() ? resultToReturn.size() : startIndex + limit;

    List<GameboardDTO> sublistOfGameboards = resultToReturn.subList(startIndex, toIndex);

    // fully augment only those we are returning.
    this.gameboardPersistenceManager.augmentGameboardItems(sublistOfGameboards);

    GameboardListDTO myBoardsResults = new GameboardListDTO(sublistOfGameboards, (long) resultToReturn.size(),
            totalNotStarted, totalInProgress, totalCompleted);

    return myBoardsResults;
}