Skip to content

Add assertions to reject null values in JobParameter #3914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,8 @@
import java.io.Serializable;
import java.util.Date;

import org.springframework.util.Assert;

/**
* Domain representation of a parameter to a batch job. Only the following types
* can be parameters: String, Long, Date, and Double. The identifying flag is
Expand All @@ -28,10 +30,10 @@
* @author Lucas Ward
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @since 2.0
*
*/
@SuppressWarnings("serial")
public class JobParameter implements Serializable {

private final Object parameter;
Expand All @@ -42,10 +44,11 @@ public class JobParameter implements Serializable {

/**
* Construct a new JobParameter as a String.
* @param parameter {@link String} instance.
* @param parameter {@link String} instance. Must not be null.
* @param identifying true if JobParameter should be identifying.
*/
public JobParameter(String parameter, boolean identifying) {
Assert.notNull(parameter, "parameter must not be null");
this.parameter = parameter;
parameterType = ParameterType.STRING;
this.identifying = identifying;
Expand All @@ -54,10 +57,11 @@ public JobParameter(String parameter, boolean identifying) {
/**
* Construct a new JobParameter as a Long.
*
* @param parameter {@link Long} instance.
* @param parameter {@link Long} instance. Must not be null.
* @param identifying true if JobParameter should be identifying.
*/
public JobParameter(Long parameter, boolean identifying) {
Assert.notNull(parameter, "parameter must not be null");
this.parameter = parameter;
parameterType = ParameterType.LONG;
this.identifying = identifying;
Expand All @@ -66,10 +70,11 @@ public JobParameter(Long parameter, boolean identifying) {
/**
* Construct a new JobParameter as a Date.
*
* @param parameter {@link Date} instance.
* @param parameter {@link Date} instance. Must not be null.
* @param identifying true if JobParameter should be identifying.
*/
public JobParameter(Date parameter, boolean identifying) {
Assert.notNull(parameter, "parameter must not be null");
this.parameter = parameter;
parameterType = ParameterType.DATE;
this.identifying = identifying;
Expand All @@ -78,10 +83,11 @@ public JobParameter(Date parameter, boolean identifying) {
/**
* Construct a new JobParameter as a Double.
*
* @param parameter {@link Double} instance.
* @param parameter {@link Double} instance. Must not be null.
* @param identifying true if JobParameter should be identifying.
*/
public JobParameter(Double parameter, boolean identifying) {
Assert.notNull(parameter, "parameter must not be null");
this.parameter = parameter;
parameterType = ParameterType.DOUBLE;
this.identifying = identifying;
Expand All @@ -94,9 +100,7 @@ public JobParameter(Double parameter, boolean identifying) {
* @param parameter {@link String} instance.
*/
public JobParameter(String parameter) {
this.parameter = parameter;
parameterType = ParameterType.STRING;
this.identifying = true;
this(parameter, true);
}

/**
Expand All @@ -105,9 +109,7 @@ public JobParameter(String parameter) {
* @param parameter {@link Long} instance.
*/
public JobParameter(Long parameter) {
this.parameter = parameter;
parameterType = ParameterType.LONG;
this.identifying = true;
this(parameter, true);
}

/**
Expand All @@ -116,9 +118,7 @@ public JobParameter(Long parameter) {
* @param parameter {@link Date} instance.
*/
public JobParameter(Date parameter) {
this.parameter = parameter;
parameterType = ParameterType.DATE;
this.identifying = true;
this(parameter, true);
}

/**
Expand All @@ -127,9 +127,7 @@ public JobParameter(Date parameter) {
* @param parameter {@link Double} instance.
*/
public JobParameter(Double parameter) {
this.parameter = parameter;
parameterType = ParameterType.DOUBLE;
this.identifying = true;
this(parameter, true);
}

public boolean isIdentifying() {
Expand All @@ -140,13 +138,7 @@ public boolean isIdentifying() {
* @return the value contained within this JobParameter.
*/
public Object getValue() {

if (parameter != null && parameter.getClass().isInstance(Date.class)) {
return new Date(((Date) parameter).getTime());
}
else {
return parameter;
}
return parameter;
}

/**
Expand All @@ -158,7 +150,7 @@ public ParameterType getType() {

@Override
public boolean equals(Object obj) {
if (obj instanceof JobParameter == false) {
if (!(obj instanceof JobParameter)) {
return false;
}

Expand All @@ -172,8 +164,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return parameter == null ? null : (parameterType == ParameterType.DATE ? "" + ((Date) parameter).getTime()
: parameter.toString());
return parameterType == ParameterType.DATE ? "" + ((Date) parameter).getTime() : parameter.toString();
}

@Override
Expand Down