1package org.apache.archiva.redback.users.memory;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.redback.users.AbstractUserQuery;
23import org.apache.archiva.redback.users.User;
2425import java.util.Comparator;
2627publicclassSimpleUserQuery28extendsAbstractUserQuery29 {
3031/**32 * Returns true if this user should be considered a match of the current query33 *34 * @param user35 * @return36 */37publicboolean matches( User user )
38 {
39if ( getUsername() != null && user.getUsername() != null &&
40 user.getUsername().toLowerCase().indexOf( getUsername().toLowerCase() ) == -1 )
41 {
42return false;
43 }
44elseif ( getFullName() != null && user.getFullName() != null &&
45 user.getFullName().toLowerCase().indexOf( getFullName().toLowerCase() ) == -1 )
46 {
47return false;
48 }
49elseif ( getEmail() != null && user.getEmail() != null &&
50 user.getEmail().toLowerCase().indexOf( getEmail().toLowerCase() ) == -1 )
51 {
52return false;
53 }
54else55 {
56returntrue;
57 }
5859 }
6061/**62 * Returns a comparator used for sorting a collection of User objects based on the ordering set63 * on this UserQuery's {@link #setOrderBy(String)} and {@link #setAscending(boolean)}. 64 * @return65 */66public Comparator<User> getComparator()
67 {
68returnnew Comparator<User>()
69 {
70publicint compare( User user1, User user2 )
71 {
72return ( isAscending() ? 1 : -1 ) * compareUsers( user1, user2 );
73 }
74 };
75 }
7677privateint compareUsers( User user, User user1 )
78 {
79if ( ORDER_BY_EMAIL.equals( getOrderBy() ) )
80 {
81return user.getEmail() == null ? -1
82 : user1.getEmail() == null ? 1 : user.getEmail().compareTo( user1.getEmail() );
83 }
84elseif ( ORDER_BY_FULLNAME.equals( getOrderBy() ) )
85 {
86return user.getFullName() == null ? -1
87 : user1.getFullName() == null ? 1 : user.getFullName().compareTo( user1.getFullName() );
88 }
89else90 {
91return user.getUsername().compareTo( user1.getUsername() );
92 }
93 }
94 }