Java to Beanshell: validate an attribute for each record of a spreadsheet

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Suppose this Java code is converted to Beanshell and called by an application which reads a spreadsheet and validates the department of each employee at a university.
My primary concern is if there is a more efficient, standard, or otherwise better way to go about validating an employee's department rather than how I have done so below. Null should be returned if the department is invalid.
import java.util.HashSet;
import java.util.Arrays;
public class Test
 public static final String VALID_DEPARTMENTS = 
 "Computer Science", "Biology"
 ;
 public static final HashSet<String> set = new HashSet<String>( Arrays.asList(VALID_DEPARTMENTS) );
 public String validate(String dep) 
 if( set.contains(dep) )
 return dep; 
 return null;
 
 public static void main(String args)
 Test t = new Test();
 System.out.println( t.validate("CS") );
 
java
add a comment |Â
up vote
0
down vote
favorite
Suppose this Java code is converted to Beanshell and called by an application which reads a spreadsheet and validates the department of each employee at a university.
My primary concern is if there is a more efficient, standard, or otherwise better way to go about validating an employee's department rather than how I have done so below. Null should be returned if the department is invalid.
import java.util.HashSet;
import java.util.Arrays;
public class Test
 public static final String VALID_DEPARTMENTS = 
 "Computer Science", "Biology"
 ;
 public static final HashSet<String> set = new HashSet<String>( Arrays.asList(VALID_DEPARTMENTS) );
 public String validate(String dep) 
 if( set.contains(dep) )
 return dep; 
 return null;
 
 public static void main(String args)
 Test t = new Test();
 System.out.println( t.validate("CS") );
 
java
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Suppose this Java code is converted to Beanshell and called by an application which reads a spreadsheet and validates the department of each employee at a university.
My primary concern is if there is a more efficient, standard, or otherwise better way to go about validating an employee's department rather than how I have done so below. Null should be returned if the department is invalid.
import java.util.HashSet;
import java.util.Arrays;
public class Test
 public static final String VALID_DEPARTMENTS = 
 "Computer Science", "Biology"
 ;
 public static final HashSet<String> set = new HashSet<String>( Arrays.asList(VALID_DEPARTMENTS) );
 public String validate(String dep) 
 if( set.contains(dep) )
 return dep; 
 return null;
 
 public static void main(String args)
 Test t = new Test();
 System.out.println( t.validate("CS") );
 
java
Suppose this Java code is converted to Beanshell and called by an application which reads a spreadsheet and validates the department of each employee at a university.
My primary concern is if there is a more efficient, standard, or otherwise better way to go about validating an employee's department rather than how I have done so below. Null should be returned if the department is invalid.
import java.util.HashSet;
import java.util.Arrays;
public class Test
 public static final String VALID_DEPARTMENTS = 
 "Computer Science", "Biology"
 ;
 public static final HashSet<String> set = new HashSet<String>( Arrays.asList(VALID_DEPARTMENTS) );
 public String validate(String dep) 
 if( set.contains(dep) )
 return dep; 
 return null;
 
 public static void main(String args)
 Test t = new Test();
 System.out.println( t.validate("CS") );
 
java
asked Feb 5 at 13:08
user159559
add a comment |Â
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
up vote
2
down vote
A few things come to mind:
- In Java 9 one can use Set.of("X", "Y", ...).
- Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));
- Could modify validate(dep) to return set.contains(dep) ? dep : null;
- It would be nice to return ""instead of null if at all possible. This is so that a project won't end up with tons ofif (x != null)all over the place.
- Recommend renaming Testto something more descriptive. MaybeDepartmentValidatormight be a step in the right direction.
- Probably refactor deptodepartment. Eventually the meaning may become less obvious as time passes and the code base grows larger.
- Note: the validation is currently quite strict (exact match), so I'm assuming the incoming data is pretty exact (no misspellings or different capitalization, etc.)
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
A few things come to mind:
- In Java 9 one can use Set.of("X", "Y", ...).
- Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));
- Could modify validate(dep) to return set.contains(dep) ? dep : null;
- It would be nice to return ""instead of null if at all possible. This is so that a project won't end up with tons ofif (x != null)all over the place.
- Recommend renaming Testto something more descriptive. MaybeDepartmentValidatormight be a step in the right direction.
- Probably refactor deptodepartment. Eventually the meaning may become less obvious as time passes and the code base grows larger.
- Note: the validation is currently quite strict (exact match), so I'm assuming the incoming data is pretty exact (no misspellings or different capitalization, etc.)
add a comment |Â
up vote
2
down vote
A few things come to mind:
- In Java 9 one can use Set.of("X", "Y", ...).
- Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));
- Could modify validate(dep) to return set.contains(dep) ? dep : null;
- It would be nice to return ""instead of null if at all possible. This is so that a project won't end up with tons ofif (x != null)all over the place.
- Recommend renaming Testto something more descriptive. MaybeDepartmentValidatormight be a step in the right direction.
- Probably refactor deptodepartment. Eventually the meaning may become less obvious as time passes and the code base grows larger.
- Note: the validation is currently quite strict (exact match), so I'm assuming the incoming data is pretty exact (no misspellings or different capitalization, etc.)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
A few things come to mind:
- In Java 9 one can use Set.of("X", "Y", ...).
- Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));
- Could modify validate(dep) to return set.contains(dep) ? dep : null;
- It would be nice to return ""instead of null if at all possible. This is so that a project won't end up with tons ofif (x != null)all over the place.
- Recommend renaming Testto something more descriptive. MaybeDepartmentValidatormight be a step in the right direction.
- Probably refactor deptodepartment. Eventually the meaning may become less obvious as time passes and the code base grows larger.
- Note: the validation is currently quite strict (exact match), so I'm assuming the incoming data is pretty exact (no misspellings or different capitalization, etc.)
A few things come to mind:
- In Java 9 one can use Set.of("X", "Y", ...).
- Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));
- Could modify validate(dep) to return set.contains(dep) ? dep : null;
- It would be nice to return ""instead of null if at all possible. This is so that a project won't end up with tons ofif (x != null)all over the place.
- Recommend renaming Testto something more descriptive. MaybeDepartmentValidatormight be a step in the right direction.
- Probably refactor deptodepartment. Eventually the meaning may become less obvious as time passes and the code base grows larger.
- Note: the validation is currently quite strict (exact match), so I'm assuming the incoming data is pretty exact (no misspellings or different capitalization, etc.)
answered Feb 10 at 20:00
rmbdev
414
414
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186818%2fjava-to-beanshell-validate-an-attribute-for-each-record-of-a-spreadsheet%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password