package org.mxchange.addressbook.validator.user;
import java.text.MessageFormat;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
+import java.util.Set;
+import java.util.TreeSet;
import javax.ejb.EJB;
import javax.enterprise.event.Observes;
import javax.faces.application.FacesMessage;
/**
* Cached user status
*/
- private static final ConcurrentMap<Long, Boolean> cachedStatus = new ConcurrentHashMap<>(0);
+ private static final Set<Long> cachedStatus = new TreeSet<>();
@Override
public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
Boolean ifUserExists = false;
// Is a map entry there?
- if (UserIdValidator.cachedStatus.containsKey(userId)) {
+ if (UserIdValidator.cachedStatus.contains(userId)) {
// Get from cache
- ifUserExists = UserIdValidator.cachedStatus.get(userId);
+ ifUserExists = Boolean.TRUE;
} else {
// Get status
ifUserExists = this.userBean.ifUserIdExists(userId);
-
- // Add to cache
- UserIdValidator.cachedStatus.put(userId, ifUserExists);
}
- // Is the address book id valid?
+ // Is the user id valid?
if (!ifUserExists) {
// Is not valid
throw new ValidatorException(new FacesMessage(MessageFormat.format("No user found with id {0}. Please check your link.", userId))); //NOI18N
}
+ // Add to cache if valid
+ UserIdValidator.cachedStatus.add(userId);
+
// Trace message
//this.getLogger().logTrace("validate: EXIT!"); //NOI18N
}
User registeredUser = event.getUser();
// Update cache
- UserIdValidator.cachedStatus.put(registeredUser.getUserId(), Boolean.TRUE);
+ UserIdValidator.cachedStatus.add(registeredUser.getUserId());
}
}