Skip to content

Commit

Permalink
Add assertions to reject null values in JobParameter
Browse files Browse the repository at this point in the history
This is a WIP. Tests should be updated accordingly.

Issue spring-projects#3913
  • Loading branch information
fmbenhassine committed May 18, 2021
1 parent 1beb07e commit 49cb5ca
Showing 1 changed file with 19 additions and 28 deletions.
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

0 comments on commit 49cb5ca

Please sign in to comment.