Skip to content

Commit

Permalink
Avoid duplicate search for username on user creation. (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis-Cruz authored Sep 7, 2017
1 parent 8f6a66f commit c6bc802
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions bennu-core/src/main/java/org/fenixedu/bennu/core/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pt.ist.fenixframework.FenixFramework;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;

import pt.ist.fenixframework.FenixFramework;

/**
* The application end user.
*/
Expand All @@ -68,14 +68,17 @@ public static interface UsernameGenerator {
}

public User(UserProfile profile) {
this(generateUsername(profile), profile);
init(generateUsername(profile), profile);
}

public User(String username, UserProfile profile) {
super();
if (findByUsername(username) != null) {
throw BennuCoreDomainException.duplicateUsername(username);
}
init(username, profile);
}

private void init(final String username, final UserProfile profile) {
setBennu(Bennu.getInstance());
setCreated(new DateTime());
setUsername(username);
Expand Down Expand Up @@ -267,13 +270,17 @@ public static User findByUsername(final String username) {
}

private static User manualFind(String username) {
return Bennu.getInstance().getUserSet().stream().peek(User::cacheUser)
.filter(user -> user.getUsername().equals(username)).findAny()
.orElse(null);
for (final User user : Bennu.getInstance().getUserSet()) {
cacheUser(user);
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}

private static void cacheUser(User user) {
map.put(user.getUsername(), user);
map.putIfAbsent(user.getUsername(), user);
}

public static void setUsernameGenerator(UsernameGenerator generator) {
Expand Down

0 comments on commit c6bc802

Please sign in to comment.