Skip to content

Commit

Permalink
Java 8+ modernizations
Browse files Browse the repository at this point in the history
+ Generics
+ for loop
+ lambda expression
+ Java style array declaration instead of C-style
+ List.of() / Set.of()
+ Collections.emptyIterator()
+ Boolean.TRUE
+ Objects.equals

Signed-off-by: pizzi80 <[email protected]>
  • Loading branch information
pizzi80 committed Jun 20, 2023
1 parent 9a972b3 commit 5f4a069
Show file tree
Hide file tree
Showing 52 changed files with 223 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private String getNavigationOutcome(FacesContext context, MethodExpression expre

return invokeResult.toString();
} catch (ELException | NullPointerException e) {
LOGGER.log(FINE, e, () -> e.getMessage());
LOGGER.log(FINE, e, e::getMessage);

throw new FacesException(expression.getExpressionString() + ": " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public void handleNavigation(FacesContext context, String fromAction, String out

// If we are exiting all flows
if (caseStruct.newFlow == null) { // NOPMD
parameters.put(TO_FLOW_DOCUMENT_ID_REQUEST_PARAM_NAME, asList(NULL_FLOW));
parameters.put(FLOW_ID_REQUEST_PARAM_NAME, asList(""));
parameters.put(TO_FLOW_DOCUMENT_ID_REQUEST_PARAM_NAME, List.of(NULL_FLOW));
parameters.put(FLOW_ID_REQUEST_PARAM_NAME, List.of(""));
FlowHandler flowHandler = context.getApplication().getFlowHandler();

if (flowHandler instanceof FlowHandlerImpl) {
Expand All @@ -272,10 +272,10 @@ public void handleNavigation(FacesContext context, String fromAction, String out
if (!parameters.containsKey(TO_FLOW_DOCUMENT_ID_REQUEST_PARAM_NAME) || !parameters.containsKey(FLOW_ID_REQUEST_PARAM_NAME)) {

// Overwrite both of them.
List<String> toFlowDocumentIdParam = asList(caseStruct.navCase.getToFlowDocumentId());
List<String> toFlowDocumentIdParam = Collections.singletonList(caseStruct.navCase.getToFlowDocumentId());
parameters.put(TO_FLOW_DOCUMENT_ID_REQUEST_PARAM_NAME, toFlowDocumentIdParam);

List<String> flowIdParam = asList(caseStruct.newFlow.getId());
List<String> flowIdParam = Collections.singletonList(caseStruct.newFlow.getId());
parameters.put(FLOW_ID_REQUEST_PARAM_NAME, flowIdParam);
}
}
Expand Down Expand Up @@ -866,8 +866,8 @@ private CaseStruct findImplicitMatch(FacesContext context, String viewId, String
Map<String, Object> appMap = context.getExternalContext().getApplicationMap();

String[] queryElements = Util.split(appMap, queryString, "&amp;|&");
for (int i = 0, len = queryElements.length; i < len; i++) {
String[] elements = Util.split(appMap, queryElements[i], "=", 2);
for (String queryElement : queryElements) {
String[] elements = Util.split(appMap, queryElement, "=", 2);
if (elements.length == 2) {
String rightHandSide = elements[1];
String sanitized = null != rightHandSide && 2 < rightHandSide.length() ? rightHandSide.trim() : "";
Expand All @@ -877,19 +877,13 @@ private CaseStruct findImplicitMatch(FacesContext context, String viewId, String
}
rightHandSide = "";
}

if (parameters == null) {
parameters = new LinkedHashMap<>(len / 2, 1.0f);
List<String> values = new ArrayList<>(2);
values.add(rightHandSide);
parameters.put(elements[0], values);
} else {
List<String> values = parameters.get(elements[0]);
if (values == null) {
values = new ArrayList<>(2);
parameters.put(elements[0], values);
}
values.add(rightHandSide);
parameters = new LinkedHashMap<>(queryElements.length / 2, 1.0f);
}

parameters.computeIfAbsent(elements[0], k -> new ArrayList<>(2))
.add(rightHandSide);
}
}
}
Expand Down Expand Up @@ -1278,7 +1272,7 @@ private CaseStruct findViewNodeMatch(FacesContext context, String fromAction, St
Flow currentFlow = flowHandler.getCurrentFlow(context);
if (null != currentFlow) {
FlowNode node = currentFlow.getNode(outcome);
if (null != node && node instanceof ViewNode) {
if (node instanceof ViewNode) {
result = synthesizeCaseStruct(context, currentFlow, fromAction, outcome);
}
}
Expand Down Expand Up @@ -1418,8 +1412,8 @@ private static final class NavigationInfo {

private static final class NavigationMap extends AbstractMap<String, Set<NavigationCase>> {

private HashMap<String, Set<NavigationCase>> mapToLookForNavCase = new HashMap<>();
private TreeSet<String> wildcardMatchList = new TreeSet<>((fromViewId1, fromViewId2) -> -fromViewId1.compareTo(fromViewId2));
private final HashMap<String, Set<NavigationCase>> mapToLookForNavCase = new HashMap<>();
private final TreeSet<String> wildcardMatchList = new TreeSet<>((fromViewId1, fromViewId2) -> -fromViewId1.compareTo(fromViewId2));

// ---------------------------------------------------- Methods from Map

Expand Down Expand Up @@ -1472,11 +1466,11 @@ public void putAll(Map<? extends String, ? extends Set<NavigationCase>> m) {

@Override
public Set<String> keySet() {
return new AbstractSet<String>() {
return new AbstractSet<>() {

@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
return new Iterator<>() {

Iterator<Map.Entry<String, Set<NavigationCase>>> i = entrySet().iterator();

Expand Down Expand Up @@ -1506,11 +1500,11 @@ public int size() {

@Override
public Collection<Set<NavigationCase>> values() {
return new AbstractCollection<Set<NavigationCase>>() {
return new AbstractCollection<>() {

@Override
public Iterator<Set<NavigationCase>> iterator() {
return new Iterator<Set<NavigationCase>>() {
return new Iterator<>() {

Iterator<Map.Entry<String, Set<NavigationCase>>> i = entrySet().iterator();

Expand Down Expand Up @@ -1540,12 +1534,12 @@ public int size() {

@Override
public Set<Entry<String, Set<NavigationCase>>> entrySet() {
return new AbstractSet<Entry<String, Set<NavigationCase>>>() {
return new AbstractSet<>() {

@Override
public Iterator<Entry<String, Set<NavigationCase>>> iterator() {

return new Iterator<Entry<String, Set<NavigationCase>>>() {
return new Iterator<>() {

Iterator<Entry<String, Set<NavigationCase>>> i = mapToLookForNavCase.entrySet().iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class SystemEventHelper {

public SystemEventHelper() {

systemEventInfoCache = new Cache<>(arg -> new SystemEventInfo(arg));
systemEventInfoCache = new Cache<>(SystemEventInfo::new);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ public int getParent() {
private int getProperChildIndex(UIComponent component) {
int result = -1;

if (component.getParent().getChildren().indexOf(component) != -1) {
if (component.getParent().getChildren().contains(component)) {
UIComponent parent = component.getParent();
int index = 0;
Iterator<UIComponent> iterator = parent.getChildren().iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ public void setStyleClass(java.lang.String styleClass) {
getStateHelper().put(PropertyKeys.styleClass, styleClass);
}

private static final Collection<String> EVENT_NAMES = Collections.unmodifiableCollection(
Arrays.asList("click", "dblclick", "keydown", "keypress", "keyup", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"));
private static final List<String> EVENT_NAMES = List.of(
"click", "dblclick", "keydown", "keypress", "keyup", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup");

@Override
public Collection<String> getEventNames() {
Expand All @@ -376,7 +376,7 @@ private void handleAttribute(String name, Object value) {
List<String> setAttributes = (List<String>) getAttributes().get("jakarta.faces.component.UIComponentBase.attributesThatAreSet");
if (setAttributes == null) {
String cname = this.getClass().getName();
if (cname != null && cname.startsWith(OPTIMIZED_PACKAGE)) {
if (cname.startsWith(OPTIMIZED_PACKAGE)) {
setAttributes = new ArrayList<>(6);
getAttributes().put("jakarta.faces.component.UIComponentBase.attributesThatAreSet", setAttributes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void add(SearchKeywordResolver searchKeywordResolver) {
public void resolve(SearchKeywordContext context, UIComponent current, String keyword) {
context.setKeywordResolved(false);

for (int i = 0; i < resolvers.size(); i++) {
SearchKeywordResolver resolver = resolvers.get(i);
for (SearchKeywordResolver resolver : resolvers) {
if (resolver.isResolverForKeyword(context.getSearchExpressionContext(), keyword)) {
resolver.resolve(context, current, keyword);
if (context.isKeywordResolved()) {
Expand All @@ -59,8 +58,7 @@ public void resolve(SearchKeywordContext context, UIComponent current, String ke

@Override
public boolean isResolverForKeyword(SearchExpressionContext searchExpressionContext, String keyword) {
for (int i = 0; i < resolvers.size(); i++) {
SearchKeywordResolver resolver = resolvers.get(i);
for (SearchKeywordResolver resolver : resolvers) {
if (resolver.isResolverForKeyword(searchExpressionContext, keyword)) {
return true;
}
Expand All @@ -71,8 +69,7 @@ public boolean isResolverForKeyword(SearchExpressionContext searchExpressionCont

@Override
public boolean isPassthrough(SearchExpressionContext searchExpressionContext, String keyword) {
for (int i = 0; i < resolvers.size(); i++) {
SearchKeywordResolver resolver = resolvers.get(i);
for (SearchKeywordResolver resolver : resolvers) {
if (resolver.isResolverForKeyword(searchExpressionContext, keyword)) {
return resolver.isPassthrough(searchExpressionContext, keyword);
}
Expand All @@ -83,8 +80,7 @@ public boolean isPassthrough(SearchExpressionContext searchExpressionContext, St

@Override
public boolean isLeaf(SearchExpressionContext searchExpressionContext, String keyword) {
for (int i = 0; i < resolvers.size(); i++) {
SearchKeywordResolver resolver = resolvers.get(i);
for (SearchKeywordResolver resolver : resolvers) {
if (resolver.isResolverForKeyword(searchExpressionContext, keyword)) {
return resolver.isLeaf(searchExpressionContext, keyword);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
public class SearchExpressionHandlerImpl extends SearchExpressionHandler {

protected void addHint(SearchExpressionContext searchExpressionContext, SearchExpressionHint hint) {
// It is a Set already
if (!searchExpressionContext.getExpressionHints().contains(hint)) {
searchExpressionContext.getExpressionHints().add(hint);
}
// It is a Set already, it will add only if just not contains the hint
searchExpressionContext.getExpressionHints().add(hint);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private void initializeCollections(Collection<String> clientIds) {
// Initialize ids collection
ids = new HashSet<>();

// Intialize subtreeClientIds collection
// Initialize subtreeClientIds collection
subtreeClientIds = new HashMap<>();

// Initialize the clientIds collection. Note that we proxy
Expand Down Expand Up @@ -265,8 +265,8 @@ private String getVisitId(UIComponent component) {
return clientIds.contains(clientId) ? clientId : null;
}

// Converts an client id into a plain old id by ripping
// out the trailing id segmetn.
// Converts a client id into a plain old id by ripping
// out the trailing id segment.
private String getIdFromClientId(String clientId) {
FacesContext facesContext = getFacesContext();
char separator = UINamingContainer.getSeparatorChar(facesContext);
Expand Down Expand Up @@ -309,12 +309,7 @@ private void addSubtreeClientId(String clientId) {
// NamingContainer client id. If not, create the
// Collection for this NamingContainer client id and
// stash it away in our map
Collection<String> c = subtreeClientIds.get(namingContainerClientId);

if (c == null) {
c = new ArrayList<>();
subtreeClientIds.put(namingContainerClientId, c);
}
Collection<String> c = subtreeClientIds.computeIfAbsent(namingContainerClientId, k -> new ArrayList<>());

// Stash away the client id
c.add(clientId);
Expand Down Expand Up @@ -373,7 +368,7 @@ public boolean add(E o) {
return added;
}

private Collection<E> wrapped;
private final Collection<E> wrapped;
}

// Little proxy iterator implementation used by CollectionProxy
Expand Down Expand Up @@ -405,7 +400,7 @@ public void remove() {
wrapped.remove();
}

private Iterator<E> wrapped;
private final Iterator<E> wrapped;

private E current = null;
}
Expand All @@ -426,8 +421,8 @@ public void remove() {
private Map<String, Collection<String>> subtreeClientIds;

// The FacesContext for this request
private FacesContext facesContext;
private final FacesContext facesContext;

// Our visit hints
private Set<VisitHint> hints;
private final Set<VisitHint> hints;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Collection<URI> getResources(ServletContext context) {
}

return resourceURLs.stream()
.map(url -> transformToURI(url))
.map(this::transformToURI)
.collect(toList());

} catch (IOException ioe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ public Collection<URI> getResources(ServletContext context) {
}
}

Set<URI> uris = sortedJarMap.get(jarName);
if (uris == null) {
uris = new HashSet<>();
sortedJarMap.put(jarName, uris);
}
Set<URI> uris = sortedJarMap.computeIfAbsent(jarName, k -> new HashSet<>());
uris.add(uri);
} else {
unsortedResourceList.add(0, uri);
Expand Down Expand Up @@ -141,7 +137,7 @@ private Collection<URI> loadURLs(ServletContext context) throws IOException {
urls.add(new URI(urlString));
}
// special case for finding taglib files in WEB-INF/classes/META-INF
Set paths = context.getResourcePaths(WEB_INF_CLASSES);
Set<String> paths = context.getResourcePaths(WEB_INF_CLASSES);
if (paths != null) {
for (Object path : paths) {
String p = path.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Collection<URI> getResources(ServletContext context) {
}
}

return null == list ? Collections.EMPTY_LIST : list;
return null == list ? Collections.emptyList() : list;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.sun.faces.config.initfacescontext;

import static java.util.Collections.emptyIterator;
import static java.util.Collections.emptyList;

import java.util.Iterator;
Expand All @@ -38,8 +39,7 @@ public Lifecycle getLifecycle() {

@Override
public Iterator<String> getClientIdsWithMessages() {
List<String> list = emptyList();
return list.iterator();
return emptyIterator();
}

@Override
Expand All @@ -49,8 +49,7 @@ public FacesMessage.Severity getMaximumSeverity() {

@Override
public Iterator<FacesMessage> getMessages() {
List<FacesMessage> list = emptyList();
return list.iterator();
return emptyIterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ public Map<String, Object> getRequestMap() {

@Override
public Map<String, String> getRequestParameterMap() {
return unmodifiableMap(emptyMap());
return emptyMap();
}

@Override
public Iterator<String> getRequestParameterNames() {
return Collections.<String>emptyList().iterator();
return Collections.emptyIterator();
}

@Override
public Map<String, String[]> getRequestParameterValuesMap() {
return unmodifiableMap(emptyMap());
return emptyMap();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public FacesEntityResolver() {
try {
schemaUrl = schemaFile.toURI().toURL();
} catch (MalformedURLException mue) {
LOGGER.log(SEVERE, mue, () -> mue.toString());
LOGGER.log(SEVERE, mue, mue::toString);
}

if (schemaUrl == null) {
Expand Down Expand Up @@ -353,7 +353,7 @@ private static Map<FacesSchema, Schema> getSchemaMap(ServletContext servletConte

if (schemaMap == null) {
synchronized (servletContext) {
schemaMap = synchronizedMap(new EnumMap<FacesSchema, Schema>(FacesSchema.class));
schemaMap = synchronizedMap(new EnumMap<>(FacesSchema.class));
servletContext.setAttribute(SCHEMA_MAP, schemaMap);
}
}
Expand Down
Loading

0 comments on commit 5f4a069

Please sign in to comment.