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

The name of the pictureThe name of the pictureThe name of the pictureClash 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") );








share|improve this question

























    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") );








    share|improve this question





















      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") );








      share|improve this question











      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") );










      share|improve this question










      share|improve this question




      share|improve this question









      asked Feb 5 at 13:08







      user159559



























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          A few things come to mind:



          1. In Java 9 one can use Set.of("X", "Y", ...).

          2. Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));

          3. Could modify validate(dep) to return set.contains(dep) ? dep : null;

          4. 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 of if (x != null) all over the place.

          5. Recommend renaming Test to something more descriptive. Maybe DepartmentValidator might be a step in the right direction.

          6. Probably refactor dep to department. Eventually the meaning may become less obvious as time passes and the code base grows larger.

          7. 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.)





          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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





























            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:



            1. In Java 9 one can use Set.of("X", "Y", ...).

            2. Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));

            3. Could modify validate(dep) to return set.contains(dep) ? dep : null;

            4. 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 of if (x != null) all over the place.

            5. Recommend renaming Test to something more descriptive. Maybe DepartmentValidator might be a step in the right direction.

            6. Probably refactor dep to department. Eventually the meaning may become less obvious as time passes and the code base grows larger.

            7. 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.)





            share|improve this answer

























              up vote
              2
              down vote













              A few things come to mind:



              1. In Java 9 one can use Set.of("X", "Y", ...).

              2. Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));

              3. Could modify validate(dep) to return set.contains(dep) ? dep : null;

              4. 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 of if (x != null) all over the place.

              5. Recommend renaming Test to something more descriptive. Maybe DepartmentValidator might be a step in the right direction.

              6. Probably refactor dep to department. Eventually the meaning may become less obvious as time passes and the code base grows larger.

              7. 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.)





              share|improve this answer























                up vote
                2
                down vote










                up vote
                2
                down vote









                A few things come to mind:



                1. In Java 9 one can use Set.of("X", "Y", ...).

                2. Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));

                3. Could modify validate(dep) to return set.contains(dep) ? dep : null;

                4. 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 of if (x != null) all over the place.

                5. Recommend renaming Test to something more descriptive. Maybe DepartmentValidator might be a step in the right direction.

                6. Probably refactor dep to department. Eventually the meaning may become less obvious as time passes and the code base grows larger.

                7. 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.)





                share|improve this answer













                A few things come to mind:



                1. In Java 9 one can use Set.of("X", "Y", ...).

                2. Or could simplify to private static final Set<String> VALID_DEPARTMENTS = new HashSet<>(Arrays.asList("A", "B"));

                3. Could modify validate(dep) to return set.contains(dep) ? dep : null;

                4. 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 of if (x != null) all over the place.

                5. Recommend renaming Test to something more descriptive. Maybe DepartmentValidator might be a step in the right direction.

                6. Probably refactor dep to department. Eventually the meaning may become less obvious as time passes and the code base grows larger.

                7. 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.)






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Feb 10 at 20:00









                rmbdev

                414




                414






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation