View Javadoc

1   package org.appfuse.webapp.pages.admin;
2   
3   import org.appfuse.model.User;
4   
5   import java.io.Serializable;
6   import java.util.Comparator;
7   
8   /**
9    * A smarter way to compare elements in UserDataProvider.
10   *
11   * Not fully implemented. Currently not used.
12   *
13   * @author Marcin ZajÄ…czkowski, 2011-05-22
14   */
15  public class UserComparatorResolver {
16  
17      public static Comparator<User> getComparatorBySoftProperty(String property, boolean isAscending) {
18  
19          //TODO: Implement resolver or fall back to original solution with a lot of object creation on every compare
20          throw new IllegalStateException("Not implemented yet");
21      }
22  
23      abstract class AbstractUserComparator<T extends Comparable<T>> implements Comparator<User>, Serializable {
24  
25          private final boolean ascending;
26  
27          protected AbstractUserComparator(boolean ascending) {
28              this.ascending = ascending;
29          }
30  
31          public int compare(User o1, User o2) {
32              int result = getPropertyToCompare(o1).compareTo(getPropertyToCompare(o2));
33              if (!ascending) {
34                  result = -result;
35              }
36              return result;
37          }
38  
39          protected abstract T getPropertyToCompare(User user);
40      }
41  
42      class UsernameUserComparator extends AbstractUserComparator<String> {
43  
44          protected UsernameUserComparator(boolean ascending) {
45              super(ascending);
46          }
47  
48          @Override
49          protected String getPropertyToCompare(User user) {
50              return user.getUsername();
51          }
52      }
53  
54      class LastNameUserComparator extends AbstractUserComparator<String> {
55  
56          protected LastNameUserComparator(boolean ascending) {
57              super(ascending);
58          }
59  
60          @Override
61          protected String getPropertyToCompare(User user) {
62              return user.getLastName();
63          }
64      }
65  
66      class EmailUserComparator extends AbstractUserComparator<String> {
67  
68          EmailUserComparator(boolean ascending) {
69              super(ascending);
70          }
71  
72          @Override
73          protected String getPropertyToCompare(User user) {
74              return user.getEmail();
75          }
76      }
77  
78      class EnabledUserComparator extends AbstractUserComparator<Boolean> {
79  
80          EnabledUserComparator(boolean ascending) {
81              super(ascending);
82          }
83  
84          @Override
85          protected Boolean getPropertyToCompare(User user) {
86              return user.isEnabled();
87          }
88      }
89  }