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

1   package org.apache.archiva.redback.users.memory.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 org.apache.archiva.redback.users.User;
23  
24  import java.util.Comparator;
25  
26  /**
27   * UserSorter
28   */
29  public class UserSorter
30      implements Comparator<User>
31  {
32      private boolean ascending;
33  
34      public UserSorter()
35      {
36          this.ascending = true;
37      }
38  
39      public UserSorter( boolean ascending )
40      {
41          this.ascending = ascending;
42      }
43  
44      public int compare( User o1, User o2 )
45      {
46          if ( ( o1 == null ) && ( o2 == null ) )
47          {
48              return 0;
49          }
50  
51          if ( ( o1 == null ) && ( o2 != null ) )
52          {
53              return -1;
54          }
55  
56          if ( ( o1 != null ) && ( o2 != null ) )
57          {
58              return 1;
59          }
60  
61          User u1 = null;
62          User u2 = null;
63  
64          if ( isAscending() )
65          {
66              u1 = o1;
67              u2 = o2;
68          }
69          else
70          {
71              u1 = o2;
72              u2 = o1;
73          }
74  
75          return u1.getUsername().compareTo( u2.getUsername() );
76      }
77  
78      public boolean isAscending()
79      {
80          return ascending;
81      }
82  
83      public void setAscending( boolean ascending )
84      {
85          this.ascending = ascending;
86      }
87  
88  }