BAEL2489 - Avoid check for null statement in Java
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
public class APIContracts {
|
||||
|
||||
/**
|
||||
* Prints the value of {@code param} if not null. Prints {@code null} otherwise.
|
||||
* @param param
|
||||
*/
|
||||
public void print(Object param) {
|
||||
System.out.println("Printing " + param);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return non null result
|
||||
* @throws Exception - if result is null
|
||||
*/
|
||||
public Object process() throws Exception {
|
||||
Object result = doSomething();
|
||||
if (result == null)
|
||||
throw new Exception("Processing fail. Got a null response");
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
private Object doSomething() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
public class Assertions {
|
||||
|
||||
public void accept(Object param){
|
||||
assert param != null;
|
||||
|
||||
doSomething(param);
|
||||
}
|
||||
|
||||
private void doSomething(Object param) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class EmptyCollections {
|
||||
|
||||
public List<String> names(){
|
||||
if (userExist())
|
||||
return Stream.of(readName()).collect(Collectors.toList());
|
||||
else
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private boolean userExist() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private String readName() {
|
||||
return "test";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class FindBugsAnnotations {
|
||||
|
||||
public void accept(@Nonnull Object param) {
|
||||
System.out.println(param.toString());
|
||||
}
|
||||
|
||||
public void print(@Nullable Object param) {
|
||||
System.out.println("Printing " + param);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Object process() throws Exception {
|
||||
Object result = doSomething();
|
||||
if (result == null)
|
||||
throw new Exception("Processing fail. Got a null response");
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
private Object doSomething() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
public class Preconditions {
|
||||
|
||||
public void goodAccept(String one, String two, String three) {
|
||||
if (null == one || null == two || three == null)
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public void badAccept(String one, String two, String three){
|
||||
if (null == one)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(one);
|
||||
|
||||
if (null == two)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(two);
|
||||
|
||||
if (null == three)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
process(three);
|
||||
|
||||
}
|
||||
|
||||
private void process(String one) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
public class PrimitivesAndWrapper {
|
||||
|
||||
public static int sum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static Integer sum(Integer a, Integer b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public static Integer goodSum(Integer a, Integer b) {
|
||||
if (a != null && b != null)
|
||||
return a + b;
|
||||
else
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
sum(0, 0);
|
||||
sum(null, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
public class UsingLombok {
|
||||
|
||||
public void accept(@NonNull Object param){
|
||||
System.out.println(param);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class UsingObjects {
|
||||
|
||||
private String checked;
|
||||
|
||||
public void accept(Object param) {
|
||||
try {
|
||||
Objects.requireNonNull(param);
|
||||
} catch (NullPointerException e) {
|
||||
//doSomethingElseToo
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void caller() throws Exception {
|
||||
if (Objects.nonNull(checked))
|
||||
accept(checked);
|
||||
else
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class UsingOptional {
|
||||
|
||||
public Optional<Object> process() {
|
||||
if (isProcessed())
|
||||
return Optional.of("dummy");
|
||||
else
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public void caller() {
|
||||
Optional<Object> result = process();
|
||||
result.ifPresent(p -> System.out.println(p));
|
||||
result.orElseThrow(() -> new IllegalArgumentException());
|
||||
}
|
||||
|
||||
private boolean isProcessed() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.nulls;
|
||||
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class UsingStringUtils {
|
||||
|
||||
public void accept(String param) {
|
||||
if (StringUtils.isNotEmpty(param))
|
||||
System.out.println(param);
|
||||
}
|
||||
|
||||
public void acceptOnlyNonBlank(String param) {
|
||||
if (StringUtils.isNotBlank(param))
|
||||
System.out.println(param);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user