1 package org.apache.archiva.redback.integration.util;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.Comparator;
24 import org.apache.archiva.redback.users.User;
29 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
32 public class UserComparator
33 implements Comparator<User>
35 private static final int UNKNOWN = -1;
37 private static final int USERNAME = 1;
39 private static final int FULLNAME = 2;
41 private static final int EMAIL = 3;
43 private static final int VALIDATED = 4;
45 private static final int LOCKED = 5;
47 private static final int PERMANENT = 6;
49 private int propKey = UNKNOWN;
51 private boolean ascending;
53 public UserComparator( String property, boolean ascending )
55 this.ascending = ascending;
57 if ( "username".equals( property ) )
61 else if ( "fullName".equals( property ) )
65 else if ( "email".equals( property ) )
69 else if ( "validated".equals( property ) )
73 else if ( "locked".equals( property ) )
77 else if ( "permanent".equals( property ) )
83 public int compare( User user1, User user2 )
85 if ( ( user1 == null ) && ( user2 == null ) )
90 if ( ( user1 == null ) && ( user2 != null ) )
95 if ( ( user1 != null ) && ( user2 == null ) )
100 return compareUsers( user1, user2 ) * ( ascending ? 1 : -1 );
103 private int compareUsers( User u1, User u2 )
108 return compareStrings( u1.getUsername(), u2.getUsername() );
110 return compareStrings( u1.getFullName(), u2.getFullName() );
112 return compareStrings( u1.getEmail(), u2.getEmail() );
114 return compareBooleans( u1.isValidated(), u2.isValidated() );
116 return compareBooleans( u1.isLocked(), u2.isLocked() );
118 return compareBooleans( u1.isPermanent(), u2.isPermanent() );
125 private int compareStrings( String s1, String s2 )
127 if ( ( s1 == null ) && ( s2 == null ) )
132 if ( ( s1 == null ) && ( s2 != null ) )
137 if ( ( s1 != null ) && ( s2 == null ) )
142 return s1.toLowerCase().compareTo( s2.toLowerCase() );
145 private int compareBooleans( boolean b1, boolean b2 )
152 return ( b1 ) ? 1 : -1;