sessionresultfilter.java

来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 588 行 · 第 1/2 页

JAVA
588
字号

    /**
     * Returns the upper boundary on server packets for sessions to be selected
     * by the result filter. A value of {@link #NO_PACKET_LIMIT} will be returned if
     * there is no upper packet limit.
     *
     * @return the upper limit of server packets allowed for sessions to meet this
     *      filter requirement.
     */
    public long getServerPacketRangeMax() {
        return serverPacketRangeMax;
    }

    /**
     * Sets the upper boundary on server packets for sessions to be selected
     * by the result filter. If this value is not set (using the value
     * {@link #NO_PACKET_LIMIT}), the results filter will have no upper bounds
     * for server packets selected.
     *
     * @param max the upper limit of server packets allowed for sessions to meet
     *      this filter requirement.
     */
    public void setServerPacketRangeMax(long max) {
        this.serverPacketRangeMax = max;
    }

    /**
     * Returns the currently selected sort field. The default value is
     * SessionResultFilter.SORT_LAST_ACTIVITY_DATE.
     *
     * @return current sort field.
     */
    public int getSortField() {
        return sortField;
    }

    /**
     * Sets the sort field to use. The default value is
     * SessionResultFilter.SORT_LAST_ACTIVITY_DATE.
     *
     * @param sortField the field that will be used for sorting.
     */
    public void setSortField(int sortField) {
        this.sortField = sortField;
    }

    /**
     * Returns the sort order, which will be SessionResultFilter.ASCENDING for
     * ascending sorting, or SessionResultFilter.DESCENDING for descending sorting.
     * Descending sorting is: 3, 2, 1, etc. Ascending sorting is 1, 2, 3, etc.
     *
     * @return the sort order.
     */
    public int getSortOrder() {
        return this.sortOrder;
    }

    /**
     * Sets the sort type. Valid arguments are SessionResultFilter.ASCENDING for
     * ascending sorting or SessionResultFilter.DESCENDING for descending sorting.
     * Descending sorting is: 3, 2, 1, etc. Ascending sorting is 1, 2, 3, etc.
     *
     * @param sortOrder the order that results will be sorted in.
     */
    public void setSortOrder(int sortOrder) {
        if (!(sortOrder == SessionResultFilter.ASCENDING || sortOrder == SessionResultFilter.DESCENDING)) {
            throw new IllegalArgumentException();
        }
        this.sortOrder = sortOrder;
    }

    /**
     * <p>Returns the max number of results that should be returned.</p>
     * <p>The default value for is NO_RESULT_LIMIT, which means there will be no limit
     * on the number of results. This method can be used in combination with
     * setStartIndex(int) to perform pagination of results.</p>
     *
     * @return the max number of results to return or NO_RESULT_LIMIT for no limit
     * @see #setStartIndex(int)
     */
    public int getNumResults() {
        return numResults;
    }

    /**
     * <p>Sets the limit on the number of results to be returned.</p>
     * <p>User NO_RESULT_LIMIT if you don't want to limit the results returned.</p>
     *
     * @param numResults the number of results to return or NO_RESULT_LIMIT for no limit
     */
    public void setNumResults(int numResults) {
        if (numResults != NO_RESULT_LIMIT && numResults < 0) {
            throw new IllegalArgumentException("numResults cannot be less than 0.");
        }
        this.numResults = numResults;
    }

    /**
     * Returns the index of the first result to return.
     *
     * @return the index of the first result which should be returned.
     */
    public int getStartIndex() {
        return startIndex;
    }

    /**
     * Sets the index of the first result to return. For example, if the start
     * index is set to 20, the Iterator returned will start at the 20th result
     * in the query. This method can be used in combination with
     * setNumResults(int) to perform pagination of results.
     *
     * @param startIndex the index of the first result to return.
     */
    public void setStartIndex(int startIndex) {
        if (startIndex < 0) {
            throw new IllegalArgumentException("A start index less than 0 is not valid.");
        }
        this.startIndex = startIndex;
    }

    /**
     * Returns a comparator that will sort a standard sorted set according
     * to this filter's sort order.
     *
     * @return a comparator that sorts Sessions matching the sort order for this filter.
     */
    public Comparator<Session> getSortComparator() {
        return new SessionComparator();
    }

    /**
     * Compares sessions according to sort fields.
     *
     * @author Iain Shigeoka
     */
    private class SessionComparator implements Comparator {

        public int compare(Object o1, Object o2) {
            Session lhs = (Session)o1;
            Session rhs = (Session)o2;
            int comparison;
            switch (sortField) {
                case SessionResultFilter.SORT_CREATION_DATE:
                    comparison = lhs.getCreationDate().compareTo(rhs.getCreationDate());
                    break;
                case SessionResultFilter.SORT_LAST_ACTIVITY_DATE:
                    comparison = lhs.getLastActiveDate().compareTo(rhs.getCreationDate());
                    break;
                case SessionResultFilter.SORT_NUM_CLIENT_PACKETS:
                    comparison = (int)(lhs.getNumClientPackets() - rhs.getNumClientPackets());
                    break;
                case SessionResultFilter.SORT_NUM_SERVER_PACKETS:
                    comparison = (int)(lhs.getNumServerPackets() - rhs.getNumServerPackets());
                    break;
                case SessionResultFilter.SORT_USER:
                    // sort first by name, then by resource
                    comparison = compareString(lhs.getAddress().getNode(),
                            rhs.getAddress().getNode());
                    if (comparison == 0) {
                        comparison = compareString(lhs.getAddress().getResource(),
                                rhs.getAddress().getResource());
                    }
                    break;
                default:
                    comparison = 0;
            }
            if (sortOrder == SessionResultFilter.DESCENDING) {
                comparison *= -1; // Naturally ascending, flip sign if descending
            }
            return comparison;
        }

        private int compareString(String lhs, String rhs) {
            if (lhs == null) {
                lhs = "";
            }
            if (rhs == null) {
                rhs = "";
            }
            return lhs.compareTo(rhs);
        }
    }

    /**
     * Rounds the given date down to the nearest specified second. The following
     * table shows sample input and expected output values: (Note, only
     * the time portion of the date is shown for brevity) <p>
     *
     * <table border="1">
     * <tr><th>Date</th><th>Seconds</th><th>Result</th></tr>
     * <tr><td>1:37.48</td><td>5</td><td>1:37.45</td></tr>
     * <tr><td>1:37.48</td><td>10</td><td>1:37.40</td></tr>
     * <tr><td>1:37.48</td><td>30</td><td>1:37.30</td</tr>
     * <tr><td>1:37.48</td><td>60</td><td>1:37.00</td></tr>
     * <tr><td>1:37.48</td><td>120</td><td>1:36.00</td></tr>
     * </table><p>
     *
     * This method is useful when calculating the last post in
     * a forum or the number of new messages from a given date. Using a rounded
     * date allows Jive to internally cache the results of the date query.
     * Here's an example that shows the last posted message in a forum accurate
     * to the last 60 seconds:<p>
     *
     * <pre>
     * SessionResultFilter filter = new SessionResultFilter();
     * filter.setSortOrder(SessionResultFilter.DESCENDING);
     * filter.setSortField(JiveGlobals.SORT_CREATION_DATE);
     * <b>filter.setCreationDateRangeMin(SessionResultFilter.roundDate(forum.getModificationDate(), 60));</b>
     * filter.setNumResults(1);
     * Iterator messages = forum.messages(filter);
     * ForumMessage lastPost = (ForumMessage)messages.next();
     * </pre>
     *
     * @param date the <tt>Date</tt> we want to round.
     * @param seconds the number of seconds we want to round the date to.
     * @return the given date, rounded down to the nearest specified number of seconds.
     */
    public static Date roundDate(Date date, int seconds) {
        return new Date(roundDate(date.getTime(), seconds));
    }

    /**
     * Rounds the given date down to the nearest specfied second.
     *
     * @param date the date (as a long) that we want to round.
     * @param seconds the number of seconds we want to round the date to.
     * @return the given date (as a long), rounded down to the nearest
     *         specified number of seconds.
     */
    public static long roundDate(long date, int seconds) {
        return date - (date % (1000 * seconds));
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object != null && object instanceof SessionResultFilter) {
            SessionResultFilter o = (SessionResultFilter)object;
            if (sortField != o.sortField) {
                return false;
            }
            if (sortOrder != o.sortOrder) {
                return false;
            }
            if (numResults != o.numResults) {
                return false;
            }
            if (!compare(username, o.username)) {
                return false;
            }

            if (!compare(creationDateRangeMin, o.creationDateRangeMin)) {
                return false;
            }
            if (!compare(creationDateRangeMax, o.creationDateRangeMax)) {
                return false;
            }
            if (!compare(lastActivityDateRangeMin, o.lastActivityDateRangeMin)) {
                return false;
            }
            if (!compare(lastActivityDateRangeMax, o.lastActivityDateRangeMax)) {
                return false;
            }
            // All checks passed, so equal.
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Returns true if two objects are equal. This is a helper method
     * to assist with the case that one or both objects are <tt>null</tt>;
     * if both objects are <tt>null</tt> then they're considered equal.
     *
     * @param one the first object.
     * @param two the second object.
     * @return true if the objects are equal.
     */
    private static boolean compare (Object one, Object two) {
        if (one == null && two != null) {
            return false;
        }
        else if (one != null) {
            if (!one.equals(two)) {
                return false;
            }
        }
        return true;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?