-1

I am trying to migrate the python library into the java native script but I facing extreme complexity with the parameters while migration.

Here the code I need to migrate the python method with the default & optional parameters with different datatypes into the java method:

def connect_network(self,
                        bssid=None,
                        proto="http",
                        check_redirect_code=True,
                        redirect_code='302',
                        portal_url=None,
                        subscriber_portal='scg',
                        expect_href_list_zd_sp='google',
                        check_user_block=False,
                        redirect_url='',
                        tnc_content="",
                        path="/tmp/"):
   pass

Here is my example code which I tried in java equivalent:

public class LinuxClientUtils {

public void DefaultNameParameter1(HashMap<Integer, String> params){
    System.out.Println(params.toString());
}

public void DefaultNameParameter2(Map.Entry<String, String>... params){
    System.out.Println(params.toString());
}

public void DefaultNameParameter3(Optional<String> name, Optional<String> age){
    System.out.Println(name.toString());
}
 

}

I will import that Java library in the robot framework and call the method like this,

*** Settings ***
Library     test.LinuxClientUtils
*** Test Cases ***
Testing
   [tags]            service
   [Documentation]   Add Network
   Default Name Parameter3    req_network_id=89

Still, None of the methods didn't work.


  • I have tried few Methods from the following URLs Link-1 Link-2 But I am unable to figure it out from those links.

I'm new to JAVA programming and haven't been able to fix this one. Any help would be great, thanks.

Veera Balla Deva
  • 790
  • 6
  • 19
  • Does this answer your question: https://stackoverflow.com/questions/997482/does-java-support-default-parameter-values – Gunnarhawk Mar 19 '21 at 15:22
  • I don't want to use any method overloading, here just simple as the python I need to pass default/optional parameters without any hassle. – Veera Balla Deva Mar 19 '21 at 15:35
  • @Pradam, Java is a strictly typed language in which it is impossible to do the same things as in Python. You can transmit parameters as an object that encapsulates all fields. And use the builder template to create this object. – chptr-one Mar 19 '21 at 17:12

1 Answers1

1

Create a new class for a parameter object. It will have each of the parameters as a field.

The constructor of this parameter class has no parameters. Instead, each field has a default value. (null and false are automatically assigned by default for object and boolean fields, anyway.)

Your function will just take a parameter object as a single parameter.

 public class A {

    static class ParameterObject {
        public ParameterObject(){
            //empty
        }
        private int x;
        private boolean b;
        private String s;

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public boolean isB() {
            return b;
        }

        public void setB(boolean b) {
            this.b = b;
        }

        public String getS() {
            return s;
        }

        public void setS(String s) {
            this.s = s;
        }
    }

    public static void f(ParameterObject o){
        //Do something with object
    }
    public static void main(String[] args) {
        ParameterObject paramObj=new ParameterObject();
        paramObj.setX(10);
        f(paramObj);
    }
}
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147