Filling ranges based on some specified strings [closed]

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
-1
down vote

favorite












The method takes two parameters. If I call it with two parameters



first : ["0-1000:2","1000-2000:3"]



second: initial



then these are the expected outputs:




  1. First range:



    rangeId: 0-1000,
    from: 0,
    to: 1000,
    issuingRange: initial,
    minUsers: 2



  2. Second range:



    rangeId: 1000-2000
    from: 1000
    to: 2000
    issuingRange: initial
    minUsers: 3


This code is working but I would like to improve it. Can I use streams in Java 8? Any other change or suggestion can make the code better?




private List<Range> fillRanges(Object roleList, String issuingRange) 

if (roleList != null)
List<String> rangeList2 = null;
if (roleList instanceof ArrayList<?>)

rangeList2 = (ArrayList<String>) roleList;
else if (roleList instanceof String)
rangeList2 = new ArrayList(Arrays.asList(roleList));


for (int i = 0; i < ((ArrayList) rangeList2).size(); i++)
String rangeId = "";
String from = "";
String to = "";
String minUsers = "";
String info = ((ArrayList) rangeList2).get(i).toString();

logger.debug("info: " + info);

if (info.indexOf(":") > -1)
rangeId = info.substring(0, info.indexOf(":"));
minUsers = info.substring(info.indexOf(":") + 1);
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1, info.indexOf(":"));

else
rangeId = info;
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1);

minUsers = "0";


ranges.add(new Range(rangeId, from, to, issuingRange, minUsers));


return ranges;







share|improve this question













closed as off-topic by πάντα ῥεῖ, Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight Mar 5 at 11:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does.
    – Phrancis
    Feb 26 at 23:56

















up vote
-1
down vote

favorite












The method takes two parameters. If I call it with two parameters



first : ["0-1000:2","1000-2000:3"]



second: initial



then these are the expected outputs:




  1. First range:



    rangeId: 0-1000,
    from: 0,
    to: 1000,
    issuingRange: initial,
    minUsers: 2



  2. Second range:



    rangeId: 1000-2000
    from: 1000
    to: 2000
    issuingRange: initial
    minUsers: 3


This code is working but I would like to improve it. Can I use streams in Java 8? Any other change or suggestion can make the code better?




private List<Range> fillRanges(Object roleList, String issuingRange) 

if (roleList != null)
List<String> rangeList2 = null;
if (roleList instanceof ArrayList<?>)

rangeList2 = (ArrayList<String>) roleList;
else if (roleList instanceof String)
rangeList2 = new ArrayList(Arrays.asList(roleList));


for (int i = 0; i < ((ArrayList) rangeList2).size(); i++)
String rangeId = "";
String from = "";
String to = "";
String minUsers = "";
String info = ((ArrayList) rangeList2).get(i).toString();

logger.debug("info: " + info);

if (info.indexOf(":") > -1)
rangeId = info.substring(0, info.indexOf(":"));
minUsers = info.substring(info.indexOf(":") + 1);
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1, info.indexOf(":"));

else
rangeId = info;
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1);

minUsers = "0";


ranges.add(new Range(rangeId, from, to, issuingRange, minUsers));


return ranges;







share|improve this question













closed as off-topic by πάντα ῥεῖ, Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight Mar 5 at 11:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2




    Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does.
    – Phrancis
    Feb 26 at 23:56













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











The method takes two parameters. If I call it with two parameters



first : ["0-1000:2","1000-2000:3"]



second: initial



then these are the expected outputs:




  1. First range:



    rangeId: 0-1000,
    from: 0,
    to: 1000,
    issuingRange: initial,
    minUsers: 2



  2. Second range:



    rangeId: 1000-2000
    from: 1000
    to: 2000
    issuingRange: initial
    minUsers: 3


This code is working but I would like to improve it. Can I use streams in Java 8? Any other change or suggestion can make the code better?




private List<Range> fillRanges(Object roleList, String issuingRange) 

if (roleList != null)
List<String> rangeList2 = null;
if (roleList instanceof ArrayList<?>)

rangeList2 = (ArrayList<String>) roleList;
else if (roleList instanceof String)
rangeList2 = new ArrayList(Arrays.asList(roleList));


for (int i = 0; i < ((ArrayList) rangeList2).size(); i++)
String rangeId = "";
String from = "";
String to = "";
String minUsers = "";
String info = ((ArrayList) rangeList2).get(i).toString();

logger.debug("info: " + info);

if (info.indexOf(":") > -1)
rangeId = info.substring(0, info.indexOf(":"));
minUsers = info.substring(info.indexOf(":") + 1);
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1, info.indexOf(":"));

else
rangeId = info;
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1);

minUsers = "0";


ranges.add(new Range(rangeId, from, to, issuingRange, minUsers));


return ranges;







share|improve this question













The method takes two parameters. If I call it with two parameters



first : ["0-1000:2","1000-2000:3"]



second: initial



then these are the expected outputs:




  1. First range:



    rangeId: 0-1000,
    from: 0,
    to: 1000,
    issuingRange: initial,
    minUsers: 2



  2. Second range:



    rangeId: 1000-2000
    from: 1000
    to: 2000
    issuingRange: initial
    minUsers: 3


This code is working but I would like to improve it. Can I use streams in Java 8? Any other change or suggestion can make the code better?




private List<Range> fillRanges(Object roleList, String issuingRange) 

if (roleList != null)
List<String> rangeList2 = null;
if (roleList instanceof ArrayList<?>)

rangeList2 = (ArrayList<String>) roleList;
else if (roleList instanceof String)
rangeList2 = new ArrayList(Arrays.asList(roleList));


for (int i = 0; i < ((ArrayList) rangeList2).size(); i++)
String rangeId = "";
String from = "";
String to = "";
String minUsers = "";
String info = ((ArrayList) rangeList2).get(i).toString();

logger.debug("info: " + info);

if (info.indexOf(":") > -1)
rangeId = info.substring(0, info.indexOf(":"));
minUsers = info.substring(info.indexOf(":") + 1);
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1, info.indexOf(":"));

else
rangeId = info;
if (info.indexOf("-") > -1)
from = info.substring(0, info.indexOf("-"));
to = info.substring(info.indexOf("-")+1);

minUsers = "0";


ranges.add(new Range(rangeId, from, to, issuingRange, minUsers));


return ranges;









share|improve this question












share|improve this question




share|improve this question








edited Feb 27 at 1:06









200_success

123k14142399




123k14142399









asked Feb 26 at 22:49









user76788

32




32




closed as off-topic by πάντα ῥεῖ, Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight Mar 5 at 11:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by πάντα ῥεῖ, Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight Mar 5 at 11:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Stephen Rauch, Sam Onela, Ben Steffan, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 2




    Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does.
    – Phrancis
    Feb 26 at 23:56













  • 2




    Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does.
    – Phrancis
    Feb 26 at 23:56








2




2




Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does.
– Phrancis
Feb 26 at 23:56





Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does.
– Phrancis
Feb 26 at 23:56











1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Your function seems to be abusing the roleList parameter. There are two modes in which this function can be called: with a single String (in which case one Range is added), or with an ArrayList<String> (in which case one Range is added for each element of the list). If you call it with any other kind of Object, then the code crashes with a NullPointerException. That's not good design.



Rather, you should define two separate functions to handle those two modes of operation. The list-handling version simply chains to the string-handling version of the function.



private void fillRanges(List<String> roleSpecs, String issuingRange) 
for (String roleSpec : roleSpecs)
this.fillRanges(roleSpec, issuingRange);



private void fillRanges(String roleSpec, String issuingRange)
…



To implement the latter function, I would avoid slicing and dicing using substrings. Since this is a parsing problem that involves pattern recognition, I would use a regular expression instead to capture the substrings of interest.



private static final Pattern RANGE_REGEX = Pattern.compile(
"^(?<rangeId>(?<from>\d*)-?(?<to>\d*))(?::(?<minUsers>\d+))?$"
);

private void fillRanges(String roleSpec, String issuingRange)
logger.debug("Role: " + roleSpec);
Matcher m = RANGE_REGEX.matcher(roleSpec);
if (!m.matches())
throw new IllegalArgumentException(roleSpec);

ranges.add(new Range(
m.group("rangeId"),
m.group("from"),
m.group("to"),
issuingRange,
m.group("minUsers") == null ? "0" : m.group("minUsers")
));






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Your function seems to be abusing the roleList parameter. There are two modes in which this function can be called: with a single String (in which case one Range is added), or with an ArrayList<String> (in which case one Range is added for each element of the list). If you call it with any other kind of Object, then the code crashes with a NullPointerException. That's not good design.



    Rather, you should define two separate functions to handle those two modes of operation. The list-handling version simply chains to the string-handling version of the function.



    private void fillRanges(List<String> roleSpecs, String issuingRange) 
    for (String roleSpec : roleSpecs)
    this.fillRanges(roleSpec, issuingRange);



    private void fillRanges(String roleSpec, String issuingRange)
    …



    To implement the latter function, I would avoid slicing and dicing using substrings. Since this is a parsing problem that involves pattern recognition, I would use a regular expression instead to capture the substrings of interest.



    private static final Pattern RANGE_REGEX = Pattern.compile(
    "^(?<rangeId>(?<from>\d*)-?(?<to>\d*))(?::(?<minUsers>\d+))?$"
    );

    private void fillRanges(String roleSpec, String issuingRange)
    logger.debug("Role: " + roleSpec);
    Matcher m = RANGE_REGEX.matcher(roleSpec);
    if (!m.matches())
    throw new IllegalArgumentException(roleSpec);

    ranges.add(new Range(
    m.group("rangeId"),
    m.group("from"),
    m.group("to"),
    issuingRange,
    m.group("minUsers") == null ? "0" : m.group("minUsers")
    ));






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      Your function seems to be abusing the roleList parameter. There are two modes in which this function can be called: with a single String (in which case one Range is added), or with an ArrayList<String> (in which case one Range is added for each element of the list). If you call it with any other kind of Object, then the code crashes with a NullPointerException. That's not good design.



      Rather, you should define two separate functions to handle those two modes of operation. The list-handling version simply chains to the string-handling version of the function.



      private void fillRanges(List<String> roleSpecs, String issuingRange) 
      for (String roleSpec : roleSpecs)
      this.fillRanges(roleSpec, issuingRange);



      private void fillRanges(String roleSpec, String issuingRange)
      …



      To implement the latter function, I would avoid slicing and dicing using substrings. Since this is a parsing problem that involves pattern recognition, I would use a regular expression instead to capture the substrings of interest.



      private static final Pattern RANGE_REGEX = Pattern.compile(
      "^(?<rangeId>(?<from>\d*)-?(?<to>\d*))(?::(?<minUsers>\d+))?$"
      );

      private void fillRanges(String roleSpec, String issuingRange)
      logger.debug("Role: " + roleSpec);
      Matcher m = RANGE_REGEX.matcher(roleSpec);
      if (!m.matches())
      throw new IllegalArgumentException(roleSpec);

      ranges.add(new Range(
      m.group("rangeId"),
      m.group("from"),
      m.group("to"),
      issuingRange,
      m.group("minUsers") == null ? "0" : m.group("minUsers")
      ));






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Your function seems to be abusing the roleList parameter. There are two modes in which this function can be called: with a single String (in which case one Range is added), or with an ArrayList<String> (in which case one Range is added for each element of the list). If you call it with any other kind of Object, then the code crashes with a NullPointerException. That's not good design.



        Rather, you should define two separate functions to handle those two modes of operation. The list-handling version simply chains to the string-handling version of the function.



        private void fillRanges(List<String> roleSpecs, String issuingRange) 
        for (String roleSpec : roleSpecs)
        this.fillRanges(roleSpec, issuingRange);



        private void fillRanges(String roleSpec, String issuingRange)
        …



        To implement the latter function, I would avoid slicing and dicing using substrings. Since this is a parsing problem that involves pattern recognition, I would use a regular expression instead to capture the substrings of interest.



        private static final Pattern RANGE_REGEX = Pattern.compile(
        "^(?<rangeId>(?<from>\d*)-?(?<to>\d*))(?::(?<minUsers>\d+))?$"
        );

        private void fillRanges(String roleSpec, String issuingRange)
        logger.debug("Role: " + roleSpec);
        Matcher m = RANGE_REGEX.matcher(roleSpec);
        if (!m.matches())
        throw new IllegalArgumentException(roleSpec);

        ranges.add(new Range(
        m.group("rangeId"),
        m.group("from"),
        m.group("to"),
        issuingRange,
        m.group("minUsers") == null ? "0" : m.group("minUsers")
        ));






        share|improve this answer













        Your function seems to be abusing the roleList parameter. There are two modes in which this function can be called: with a single String (in which case one Range is added), or with an ArrayList<String> (in which case one Range is added for each element of the list). If you call it with any other kind of Object, then the code crashes with a NullPointerException. That's not good design.



        Rather, you should define two separate functions to handle those two modes of operation. The list-handling version simply chains to the string-handling version of the function.



        private void fillRanges(List<String> roleSpecs, String issuingRange) 
        for (String roleSpec : roleSpecs)
        this.fillRanges(roleSpec, issuingRange);



        private void fillRanges(String roleSpec, String issuingRange)
        …



        To implement the latter function, I would avoid slicing and dicing using substrings. Since this is a parsing problem that involves pattern recognition, I would use a regular expression instead to capture the substrings of interest.



        private static final Pattern RANGE_REGEX = Pattern.compile(
        "^(?<rangeId>(?<from>\d*)-?(?<to>\d*))(?::(?<minUsers>\d+))?$"
        );

        private void fillRanges(String roleSpec, String issuingRange)
        logger.debug("Role: " + roleSpec);
        Matcher m = RANGE_REGEX.matcher(roleSpec);
        if (!m.matches())
        throw new IllegalArgumentException(roleSpec);

        ranges.add(new Range(
        m.group("rangeId"),
        m.group("from"),
        m.group("to"),
        issuingRange,
        m.group("minUsers") == null ? "0" : m.group("minUsers")
        ));







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Feb 27 at 1:01









        200_success

        123k14142399




        123k14142399












            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