1 11 12 package org.jivesoftware.messenger; 13 14 import java.util.Comparator ; 15 import java.util.Date ; 16 17 34 public class SessionResultFilter { 35 36 42 public static final int DESCENDING = 0; 43 44 47 public static final int ASCENDING = 1; 48 49 55 public static final int NO_RESULT_LIMIT = -1; 56 57 63 public static final long NO_PACKET_LIMIT = -1; 64 public static final int SORT_USER = 0; 68 public static final int SORT_CREATION_DATE = 1; 69 public static final int SORT_LAST_ACTIVITY_DATE = 2; 70 public static final int SORT_NUM_CLIENT_PACKETS = 3; 71 public static final int SORT_NUM_SERVER_PACKETS = 4; 72 73 77 public static SessionResultFilter createDefaultSessionFilter() { 78 SessionResultFilter resultFilter = new SessionResultFilter(); 79 resultFilter.setSortField(SORT_USER); 80 resultFilter.setSortOrder(ASCENDING); 81 return resultFilter; 82 } 83 84 private int sortField = SORT_LAST_ACTIVITY_DATE; 85 private int sortOrder = DESCENDING; 86 private long clientPacketRangeMin = NO_PACKET_LIMIT; 87 private long clientPacketRangeMax = NO_PACKET_LIMIT; 88 private long serverPacketRangeMin = NO_PACKET_LIMIT; 89 private long serverPacketRangeMax = NO_PACKET_LIMIT; 90 91 private String username = null; 92 93 96 private int startIndex = 0; 97 98 102 private int numResults = NO_RESULT_LIMIT; 103 104 private Date creationDateRangeMin = null; 105 private Date creationDateRangeMax = null; 106 private Date lastActivityDateRangeMin = null; 107 private Date lastActivityDateRangeMax = null; 108 109 115 public String getUsername() { 116 return username; 117 } 118 119 125 public void setUsername(String username) { 126 this.username = username; 127 } 128 129 136 public Date getCreationDateRangeMin() { 137 return creationDateRangeMin; 138 } 139 140 148 public void setCreationDateRangeMin(Date creationDateRangeMin) { 149 this.creationDateRangeMin = creationDateRangeMin; 150 } 151 152 160 public Date getCreationDateRangeMax() { 161 return creationDateRangeMax; 162 } 163 164 172 public void setCreationDateRangeMax(Date creationDateRangeMax) { 173 this.creationDateRangeMax = creationDateRangeMax; 174 } 175 176 185 public Date getLastActivityDateRangeMin() { 186 return lastActivityDateRangeMin; 187 } 188 189 197 public void setLastActivityDateRangeMin(Date lastActivityDateRangeMin) { 198 this.lastActivityDateRangeMin = lastActivityDateRangeMin; 199 } 200 201 209 public Date getLastActivityDateRangeMax() { 210 return lastActivityDateRangeMax; 211 } 212 213 221 public void setLastActivityDateRangeMax(Date lastActivityDateRangeMax) { 222 this.lastActivityDateRangeMax = lastActivityDateRangeMax; 223 } 224 225 233 public long getClientPacketRangeMin() { 234 return clientPacketRangeMin; 235 } 236 237 245 public void setClientPacketRangeMin(long max) { 246 this.clientPacketRangeMin = max; 247 } 248 249 257 public long getClientPacketRangeMax() { 258 return clientPacketRangeMax; 259 } 260 261 270 public void setClientPacketRangeMax(long max) { 271 this.clientPacketRangeMax = max; 272 } 273 274 282 public long getServerPacketRangeMin() { 283 return serverPacketRangeMin; 284 } 285 286 294 public void setServerPacketRangeMin(long max) { 295 this.serverPacketRangeMin = max; 296 } 297 298 306 public long getServerPacketRangeMax() { 307 return serverPacketRangeMax; 308 } 309 310 319 public void setServerPacketRangeMax(long max) { 320 this.serverPacketRangeMax = max; 321 } 322 323 329 public int getSortField() { 330 return sortField; 331 } 332 333 339 public void setSortField(int sortField) { 340 this.sortField = sortField; 341 } 342 343 350 public int getSortOrder() { 351 return this.sortOrder; 352 } 353 354 361 public void setSortOrder(int sortOrder) { 362 if (!(sortOrder == SessionResultFilter.ASCENDING || sortOrder == SessionResultFilter.DESCENDING)) { 363 throw new IllegalArgumentException (); 364 } 365 this.sortOrder = sortOrder; 366 } 367 368 377 public int getNumResults() { 378 return numResults; 379 } 380 381 387 public void setNumResults(int numResults) { 388 if (numResults != NO_RESULT_LIMIT && numResults < 0) { 389 throw new IllegalArgumentException ("numResults cannot be less than 0."); 390 } 391 this.numResults = numResults; 392 } 393 394 399 public int getStartIndex() { 400 return startIndex; 401 } 402 403 411 public void setStartIndex(int startIndex) { 412 if (startIndex < 0) { 413 throw new IllegalArgumentException ("A start index less than 0 is not valid."); 414 } 415 this.startIndex = startIndex; 416 } 417 418 424 public Comparator <Session> getSortComparator() { 425 return new SessionComparator(); 426 } 427 428 433 private class SessionComparator implements Comparator { 434 435 442 public int compare(Object o1, Object o2) { 443 Session lhs = (Session)o1; 444 Session rhs = (Session)o2; 445 int comparison; 446 switch (sortField) { 447 case SessionResultFilter.SORT_CREATION_DATE: 448 comparison = lhs.getCreationDate().compareTo(rhs.getCreationDate()); 449 break; 450 case SessionResultFilter.SORT_LAST_ACTIVITY_DATE: 451 comparison = lhs.getLastActiveDate().compareTo(rhs.getCreationDate()); 452 break; 453 case SessionResultFilter.SORT_NUM_CLIENT_PACKETS: 454 comparison = (int)(lhs.getNumClientPackets() - rhs.getNumClientPackets()); 455 break; 456 case SessionResultFilter.SORT_NUM_SERVER_PACKETS: 457 comparison = (int)(lhs.getNumServerPackets() - rhs.getNumServerPackets()); 458 break; 459 case SessionResultFilter.SORT_USER: 460 comparison = compareString(lhs.getAddress().getNode(), 462 rhs.getAddress().getNode()); 463 if (comparison == 0) { 464 comparison = compareString(lhs.getAddress().getResource(), 465 rhs.getAddress().getResource()); 466 } 467 break; 468 default: 469 comparison = 0; 470 } 471 if (sortOrder == SessionResultFilter.DESCENDING) { 472 comparison *= -1; } 474 return comparison; 475 } 476 477 private int compareString(String lhs, String rhs) { 478 if (lhs == null) { 479 lhs = ""; 480 } 481 if (rhs == null) { 482 rhs = ""; 483 } 484 return lhs.compareTo(rhs); 485 } 486 } 487 488 534 public static Date roundDate(Date date, int seconds) { 535 return new Date (roundDate(date.getTime(), seconds)); 536 } 537 538 546 public static long roundDate(long date, int seconds) { 547 return date - (date % (1000 * seconds)); 548 } 549 550 553 public Object clone() { 554 SessionResultFilter clonedFilter = new SessionResultFilter(); 555 clonedFilter.setCreationDateRangeMax(getCreationDateRangeMax()); 556 clonedFilter.setCreationDateRangeMin(getCreationDateRangeMin()); 557 clonedFilter.setLastActivityDateRangeMax(getLastActivityDateRangeMax()); 558 clonedFilter.setLastActivityDateRangeMin(getLastActivityDateRangeMin()); 559 clonedFilter.setNumResults(getNumResults()); 560 clonedFilter.setSortField(getSortField()); 561 clonedFilter.setSortOrder(getSortOrder()); 562 clonedFilter.setStartIndex(getStartIndex()); 563 clonedFilter.setUsername(getUsername()); 564 return clonedFilter; 565 } 566 567 public boolean equals(Object object) { 568 if (this == object) { 569 return true; 570 } 571 if (object != null && object instanceof SessionResultFilter) { 572 SessionResultFilter o = (SessionResultFilter)object; 573 return 574 sortField == o.sortField && 575 sortOrder == o.sortOrder && 576 startIndex == o.startIndex && 577 numResults == o.numResults && 578 username == o.username && 579 ((creationDateRangeMin == null && o.creationDateRangeMin == null) || 580 creationDateRangeMin.equals(o.creationDateRangeMin)) && 581 ((creationDateRangeMax == null && o.creationDateRangeMax == null) || 582 creationDateRangeMax.equals(o.creationDateRangeMax)) && 583 ((lastActivityDateRangeMin == null && o.lastActivityDateRangeMin == null) || 584 lastActivityDateRangeMin.equals(o.lastActivityDateRangeMin)) && 585 ((lastActivityDateRangeMax == null && o.lastActivityDateRangeMax == null) || 586 lastActivityDateRangeMax.equals(o.lastActivityDateRangeMax)); 587 } 588 else { 589 return false; 590 } 591 } 592 } | Popular Tags |