File Source: NicklistComparator.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.NicklistComparator__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.components;
24
25 import com.dmdirc.parser.irc.ChannelClientInfo;
26
27 import java.io.Serializable;
28 import java.util.Comparator;
29 /**
30 * Compares nicklist entries to each other, for sorting purposes.
31 */
/*
P/P * Method: int compare(Object, Object)
*
* Preconditions:
* x0 != null
* x1 != null
*
* Postconditions:
* init'ed(return_value)
*/
32 public final class NicklistComparator implements Comparator<ChannelClientInfo>,
33 Serializable {
34 /**
35 * A version number for this class. It should be changed whenever the class
36 * structure is changed (or anything else that would prevent serialized
37 * objects being unserialized with the new class).
38 */
39 private static final long serialVersionUID = 1;
40
41 /**
42 * whether to sort the nicklist by modes.
43 */
44 private final boolean sortByMode;
45
46 /**
47 * whether to sort the nicklist by case.
48 */
49 private final boolean sortByCase;
50
51 /**
52 * Creates a new instance of NicklistComparator.
53 * @param newSortByMode sorts by channel mode of the user
54 * @param newSortByCase sorts by nickname case
55 */
56 public NicklistComparator(final boolean newSortByMode,
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.NicklistComparator(bool, bool)
*
* Postconditions:
* this.sortByCase == newSortByCase
* init'ed(this.sortByCase)
* this.sortByMode == newSortByMode
* init'ed(this.sortByMode)
*/
57 final boolean newSortByCase) {
58 this.sortByMode = newSortByMode;
59 this.sortByCase = newSortByCase;
60 }
61
62 /**
63 * Compares two ChannelClient objects based on the settings the comparator
64 * was initialised with.
65 * @param client1 the first client to be compared
66 * @param client2 the second client to be compared
67 * @return a negative integer, zero, or a positive integer as the first
68 * argument is less than, equal to, or greater than the second.
69 */
70 public int compare(final ChannelClientInfo client1,
71 final ChannelClientInfo client2) {
/*
P/P * Method: int compare(ChannelClientInfo, ChannelClientInfo)
*
* Preconditions:
* client1 != null
* client2 != null
*
* Presumptions:
* com.dmdirc.parser.irc.ChannelClientInfo:getNickname(...)@72 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.sortByCase: {0}, {1}
* this.sortByMode: {0}, {1}
*/
72 final String nickname1 = client1.getNickname();
73 final String nickname2 = client2.getNickname();
74
75 if (sortByMode) {
76 if (client1.getImportantModeValue()
77 > client2.getImportantModeValue()) {
78 return -1;
79 } else if (client1.getImportantModeValue()
80 < client2.getImportantModeValue()) {
81 return 1;
82 }
83 }
84
85 if (sortByCase) {
86 return nickname1.compareTo(nickname2);
87 } else {
88 return nickname1.compareToIgnoreCase(nickname2);
89 }
90 }
91 }
SofCheck Inspector Build Version : 2.17854
| NicklistComparator.java |
2009-Jun-25 01:54:24 |
| NicklistComparator.class |
2009-Sep-02 17:04:15 |