This project has retired. For details please refer to its Attic page.
UserComparator xref
View Javadoc

1   package org.apache.archiva.redback.integration.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Comparator;
23  
24  import org.apache.archiva.redback.users.User;
25  
26  /**
27   * UserComparator
28   *
29   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
30   *
31   */
32  public class UserComparator
33      implements Comparator<User>
34  {
35      private static final int UNKNOWN = -1;
36  
37      private static final int USERNAME = 1;
38  
39      private static final int FULLNAME = 2;
40  
41      private static final int EMAIL = 3;
42  
43      private static final int VALIDATED = 4;
44  
45      private static final int LOCKED = 5;
46  
47      private static final int PERMANENT = 6;
48  
49      private int propKey = UNKNOWN;
50  
51      private boolean ascending;
52  
53      public UserComparator( String property, boolean ascending )
54      {
55          this.ascending = ascending;
56  
57          if ( "username".equals( property ) )
58          {
59              propKey = USERNAME;
60          }
61          else if ( "fullName".equals( property ) )
62          {
63              propKey = FULLNAME;
64          }
65          else if ( "email".equals( property ) )
66          {
67              propKey = EMAIL;
68          }
69          else if ( "validated".equals( property ) )
70          {
71              propKey = VALIDATED;
72          }
73          else if ( "locked".equals( property ) )
74          {
75              propKey = LOCKED;
76          }
77          else if ( "permanent".equals( property ) )
78          {
79              propKey = PERMANENT;
80          }
81      }
82  
83      public int compare( User user1, User user2 )
84      {
85          if ( ( user1 == null ) && ( user2 == null ) )
86          {
87              return 0;
88          }
89  
90          if ( ( user1 == null ) && ( user2 != null ) )
91          {
92              return -1;
93          }
94  
95          if ( ( user1 != null ) && ( user2 == null ) )
96          {
97              return 1;
98          }
99  
100         return compareUsers( user1, user2 ) * ( ascending ? 1 : -1 );
101     }
102 
103     private int compareUsers( User u1, User u2 )
104     {
105         switch ( propKey )
106         {
107             case USERNAME:
108                 return compareStrings( u1.getUsername(), u2.getUsername() );
109             case FULLNAME:
110                 return compareStrings( u1.getFullName(), u2.getFullName() );
111             case EMAIL:
112                 return compareStrings( u1.getEmail(), u2.getEmail() );
113             case VALIDATED:
114                 return compareBooleans( u1.isValidated(), u2.isValidated() );
115             case LOCKED:
116                 return compareBooleans( u1.isLocked(), u2.isLocked() );
117             case PERMANENT:
118                 return compareBooleans( u1.isPermanent(), u2.isPermanent() );
119             default:
120                 return 0;
121 
122         }
123     }
124 
125     private int compareStrings( String s1, String s2 )
126     {
127         if ( ( s1 == null ) && ( s2 == null ) )
128         {
129             return 0;
130         }
131 
132         if ( ( s1 == null ) && ( s2 != null ) )
133         {
134             return -1;
135         }
136 
137         if ( ( s1 != null ) && ( s2 == null ) )
138         {
139             return 1;
140         }
141 
142         return s1.toLowerCase().compareTo( s2.toLowerCase() );
143     }
144 
145     private int compareBooleans( boolean b1, boolean b2 )
146     {
147         if ( b1 == b2 )
148         {
149             return 0;
150         }
151 
152         return ( b1 ) ? 1 : -1;
153     }
154 }