Skip to content

Commit

Permalink
Kogito-7949 Sonarcloud - Fixing code smells by returning empty collec…
Browse files Browse the repository at this point in the history
…tions instead of null and using .isEmpty() to test for emptiness (#2520)

* KOGITO-7949 Addressed code smells by returning empty collections instead of null and using isEmpty to test for emptiness

* fixing formatting issues
  • Loading branch information
kbowers-ibm authored Oct 11, 2022
1 parent f619316 commit 71046d3
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -252,7 +253,7 @@ protected ProcessInstanceEventBody create(ProcessEvent event) {

protected Set<MilestoneEventBody> createMilestones(KogitoWorkflowProcessInstance pi) {
if (pi.milestones() == null) {
return null;
return Collections.emptySet();
}

return pi.milestones().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testMilestones() {
KogitoWorkflowProcessInstance pi = mock(KogitoWorkflowProcessInstance.class);

when(pi.milestones()).thenReturn(null);
assertThat(batch.createMilestones(pi)).isNull();
assertThat(batch.createMilestones(pi)).isEmpty();

when(pi.milestones()).thenReturn(emptyList());
assertThat(batch.createMilestones(pi)).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ protected Date getTimeAfter(Date afterTime) {

// get second.................................................
st = seconds.tailSet(sec);
if (st.size() != 0) {
if (!st.isEmpty()) {
sec = (Integer) st.first();
} else {
sec = (Integer) seconds.first();
Expand All @@ -1155,7 +1155,7 @@ protected Date getTimeAfter(Date afterTime) {

// get minute.................................................
st = minutes.tailSet(min);
if (st.size() != 0) {
if (!st.isEmpty()) {
t = min;
min = (Integer) st.first();
} else {
Expand All @@ -1176,7 +1176,7 @@ protected Date getTimeAfter(Date afterTime) {

// get hour...................................................
st = hours.tailSet(hr);
if (st.size() != 0) {
if (!st.isEmpty()) {
t = hr;
hr = (Integer) st.first();
} else {
Expand Down Expand Up @@ -1279,7 +1279,7 @@ protected Date getTimeAfter(Date afterTime) {
day = (Integer) daysOfMonth.first();
mon++;
}
} else if (st.size() != 0) {
} else if (!st.isEmpty()) {
t = day;
day = (Integer) st.first();
// make sure we don't over-run a short month, such as february
Expand Down Expand Up @@ -1396,7 +1396,7 @@ protected Date getTimeAfter(Date afterTime) {
int dow = (Integer) daysOfWeek.first(); // desired
// d-o-w
st = daysOfWeek.tailSet(cDow);
if (st != null && st.size() > 0) {
if (!st.isEmpty()) {
dow = (Integer) st.first();
}

Expand Down Expand Up @@ -1451,7 +1451,7 @@ protected Date getTimeAfter(Date afterTime) {

// get month...................................................
st = months.tailSet(mon);
if (st.size() != 0) {
if (!st.isEmpty()) {
t = mon;
mon = (Integer) st.first();
} else {
Expand All @@ -1477,7 +1477,7 @@ protected Date getTimeAfter(Date afterTime) {

// get year...................................................
st = years.tailSet(year);
if (st.size() != 0) {
if (!st.isEmpty()) {
t = year;
year = (Integer) st.first();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ protected boolean containsExtensionElements(Node node) {
}

protected void writeScripts(final String type, List<DroolsAction> actions, final StringBuilder xmlDump) {
if (actions != null && actions.size() > 0) {
if (!actions.isEmpty()) {
for (DroolsAction action : actions) {
writeScript(action, type, xmlDump);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
writeNode("intermediateCatchEvent", eventNode, xmlDump, metaDataType);
xmlDump.append(">" + EOL);
writeExtensionElements(eventNode, xmlDump);
if (eventNode.getEventFilters().size() > 0) {
if (!eventNode.getEventFilters().isEmpty()) {
String type = ((EventTypeFilter) eventNode.getEventFilters().get(0)).getType();
if (type.startsWith("Message-")) {
type = type.substring(8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ private void postProcessNodes(RuleFlowProcess process, NodeContainer container)
handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().isEmpty()) {
throw new ProcessParsingValidationException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected Node handleNode(final Node node, final Element element, final String u
}
xmlNode = xmlNode.getNextSibling();
}
if (owners.size() > 0) {
if (!owners.isEmpty()) {
String owner = owners.get(0);
for (int i = 1; i < owners.size(); i++) {
owner += "," + owners.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ protected void visitInterfaces(org.kie.api.definition.process.Node[] nodes, Stri
}
} else if (node instanceof EventNode) {
List<EventFilter> filters = ((EventNode) node).getEventFilters();
if (filters.size() > 0) {
if (!filters.isEmpty()) {
String messageRef = ((EventTypeFilter) filters.get(0)).getType();
if (messageRef.startsWith("Message-")) {
messageRef = messageRef.substring(8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.kie.kogito.process.bpmn2;

import java.util.Collections;
import java.util.Map;

import org.jbpm.process.instance.InternalProcessRuntime;
Expand Down Expand Up @@ -43,7 +44,7 @@ public BpmnProcessInstance(AbstractProcess<BpmnVariables> process, BpmnVariables
@Override
protected Map<String, Object> bind(BpmnVariables variables) {
if (variables == null) {
return null;
return Collections.emptyMap();
}
return variables.toMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected void visitHeader(WorkflowProcess process, StringBuilder xmlDump, boole
}

private void visitImports(Collection<String> imports, StringBuilder xmlDump) {
if (imports != null && imports.size() > 0) {
if (imports != null && !imports.isEmpty()) {
xmlDump.append(" <imports>" + EOL);
for (String importString : imports) {
xmlDump.append(" <import name=\"" + importString + "\" />" + EOL);
Expand All @@ -124,7 +124,7 @@ private void visitImports(Collection<String> imports, StringBuilder xmlDump) {
}

private void visitFunctionImports(List<String> imports, StringBuilder xmlDump) {
if (imports != null && imports.size() > 0) {
if (imports != null && !imports.isEmpty()) {
xmlDump.append(" <functionImports>" + EOL);
for (String importString : imports) {
xmlDump.append(" <functionImport name=\"" + importString + "\" />" + EOL);
Expand All @@ -144,7 +144,7 @@ private void visitGlobals(Map<String, String> globals, StringBuilder xmlDump) {
}

public static void visitVariables(List<Variable> variables, StringBuilder xmlDump) {
if (variables != null && variables.size() > 0) {
if (variables != null && !variables.isEmpty()) {
xmlDump.append(" <variables>" + EOL);
for (Variable variable : variables) {
xmlDump.append(" <variable name=\"" + variable.getName() + "\" >" + EOL);
Expand All @@ -160,7 +160,7 @@ public static void visitVariables(List<Variable> variables, StringBuilder xmlDum
}

private void visitSwimlanes(Collection<Swimlane> swimlanes, StringBuilder xmlDump) {
if (swimlanes != null && swimlanes.size() > 0) {
if (swimlanes != null && !swimlanes.isEmpty()) {
xmlDump.append(" <swimlanes>" + EOL);
for (Swimlane swimlane : swimlanes) {
xmlDump.append(" <swimlane name=\"" + swimlane.getName() + "\" />" + EOL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public void endElement(final String uri,
localName);

if (handler == null) {
if (this.configurationStack.size() >= 1) {
if (!this.configurationStack.isEmpty()) {
endElementBuilder();
}
return;
Expand Down Expand Up @@ -608,7 +608,7 @@ public Element endElementBuilder() {

public Object getParent() {
try {
return this.parents.size() > 0 ? this.parents.getLast() : null;
return !this.parents.isEmpty() ? this.parents.getLast() : null;
} catch (NoSuchElementException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected void writeMetaData(final Node node, final StringBuilder xmlDump) {
}

protected void writeActions(final String type, List<DroolsAction> actions, final StringBuilder xmlDump) {
if (actions != null && actions.size() > 0) {
if (actions != null && !actions.isEmpty()) {
xmlDump.append(" <" + type + ">" + EOL);
for (DroolsAction action : actions) {
writeAction(action, xmlDump);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -105,11 +106,11 @@ public Map<String, Object> getValueMap(ClassLoader classLoader) {
try {
this.valueMap = new HashMap<>();
if (className == null) {
return null;
return Collections.emptyMap();
}
Class<?> clazz = classLoader == null ? Class.forName(className) : Class.forName(className, true, classLoader);
if (!clazz.isEnum()) {
return null;
return Collections.emptyMap();
}
Object[] values = (Object[]) clazz.getMethod("values", null).invoke(clazz, null);
for (Object value : values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -63,7 +64,7 @@ public JsonResolver() {
*/
public Map<String, Object> resolveOnlyAnnotatedItems(Map<String, Object> items) {
if (Objects.isNull(items)) {
return null;
return Collections.emptyMap();
}
Map<String, Map> resolved = items.entrySet().stream()
.filter(v -> Objects.nonNull(v.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jbpm.process.instance;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.drools.core.common.EndOperationListener;
Expand Down Expand Up @@ -177,7 +178,7 @@ public void unregisterChannel(String name) {

@Override
public Map<String, Channel> getChannels() {
return null;
return Collections.emptyMap();
}

@Override
Expand All @@ -202,7 +203,7 @@ public void removeEventListener(ProcessEventListener listener) {

@Override
public Collection<ProcessEventListener> getProcessEventListeners() {
return null;
return Collections.emptyList();
}

@Override
Expand All @@ -217,7 +218,7 @@ public void removeEventListener(RuleRuntimeEventListener listener) {

@Override
public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
return null;
return Collections.emptyList();
}

@Override
Expand All @@ -232,7 +233,7 @@ public void removeEventListener(AgendaEventListener listener) {

@Override
public Collection<AgendaEventListener> getAgendaEventListeners() {
return null;
return Collections.emptyList();
}

@Override
Expand Down Expand Up @@ -288,12 +289,12 @@ public void signalEvent(String type, Object event, String processInstanceId) {

@Override
public Collection<ProcessInstance> getProcessInstances() {
return null;
return Collections.emptyList();
}

@Override
public Collection<KogitoProcessInstance> getKogitoProcessInstances() {
return null;
return Collections.emptyList();
}

@Override
Expand Down Expand Up @@ -333,7 +334,7 @@ public EntryPoint getEntryPoint(String name) {

@Override
public Collection<? extends EntryPoint> getEntryPoints() {
return null;
return Collections.emptyList();
}

@Override
Expand Down Expand Up @@ -393,22 +394,22 @@ public Object getObject(FactHandle factHandle) {

@Override
public Collection<? extends Object> getObjects() {
return null;
return Collections.emptyList();
}

@Override
public Collection<? extends Object> getObjects(ObjectFilter filter) {
return null;
return Collections.emptyList();
}

@Override
public <T extends FactHandle> Collection<T> getFactHandles() {
return null;
return Collections.emptyList();
}

@Override
public <T extends FactHandle> Collection<T> getFactHandles(ObjectFilter filter) {
return null;
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public StartProcessEventListener(StartNode startNode, Trigger trigger, String pr

@Override
public String[] getEventTypes() {
return null;
return new String[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public StartProcessEventListener(StartNode startNode, Trigger trigger, String pr

@Override
public String[] getEventTypes() {
return null;
return new String[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void reconnect() {

@Override
public String[] getEventTypes() {
return null;
return new String[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void validateRemoveOutgoingConnection(final String type, final Connection
public Connection getFrom() {
final List<Connection> list =
getIncomingConnections(Node.CONNECTION_DEFAULT_TYPE);
if (list.size() == 0) {
if (list.isEmpty()) {
return null;
}
if (list.size() == 1) {
Expand All @@ -284,7 +284,7 @@ public Connection getFrom() {
public Connection getTo() {
final List<Connection> list =
getOutgoingConnections(Node.CONNECTION_DEFAULT_TYPE);
if (list.size() == 0) {
if (list.isEmpty()) {
return null;
}
if (list.size() == 1) {
Expand Down
Loading

0 comments on commit 71046d3

Please sign in to comment.