Sunday, 31 January 2021

Spring Shell: Specify multiple keys to a parameter

Using ‘@ShellOption’ argument, you can specify multiple keys for a single parameter.

 

Example

@ShellComponent(value = "Specify Multiple Named Parameter keys using @ShellOption")
public class CustomizeMultipleNamedKeysForSingleParameter {
	@ShellMethod(value = "Commands to print Person Details", key = "print-person", prefix = "-")
	public String printPersonDetails(String name, @ShellOption({"-emp-age", "-age"}) int age,
			@ShellOption("--emp-city") String city) {

		StringBuilder builder = new StringBuilder();

		builder.append("Hello ").append(name).append("!!!!").append(". You are ").append(age)
				.append(" years old and you are from ").append(city);

		return builder.toString();
	}
}

  As you see above snippet, I defined two shell options {"-emp-age", "-age"} for the parameter age. So you can use either -emp-age or -age while executing the command "print-person".

 

shell:>print-person -name "Krishna" -emp-age 45 --emp-city "Chennai"
Hello Krishna!!!!. You are 45 years old and you are from Chennai
shell:>
shell:>print-person -name "Krishna" -age 45 --emp-city "Chennai"
Hello Krishna!!!!. You are 45 years old and you are from Chennai

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world


 

  

Previous                                                    Next                                                    Home

Spring Shell: Customize named parameter keys

In my previous post, I explained how to customized the prefix while calling named parameters.

 

We can even customize named parameter keys using '@ShellOption' argument.

 

Example

@ShellComponent(value = "Customize Named Parameter keys using @ShellOption")
public class CustomizeNamedParamKeys {
	@ShellMethod(value = "Commands to print Employee Details", key = "print-employee", prefix = "-")
	public String printEmployeeDetails(String name, @ShellOption("emp-age") int age,
			@ShellOption("--emp-city") String city) {

		StringBuilder builder = new StringBuilder();

		builder.append("Hello ").append(name).append("!!!!").append(". You are ").append(age)
				.append(" years old and you are from ").append(city);

		return builder.toString();
	}
}

  For the above example, the possible parameter keys will be -name, emp-age and –emp-city.

 

shell:>print-employee -name "Krishna" emp-age 23 --emp-city "Hyderabad"
Hello Krishna!!!!. You are 23 years old and you are from Hyderabad

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world



 

Previous                                                    Next                                                    Home

Spring Shell: Customize named parameter keys to use given prefix

In my previous post, I explained how to call the command using named parameters. As you remember, we called named parameters using the prefix --.

 

get-user --name "Krishna" --age 29 --city "Bangalore"

 

We can customize this prefix using the ‘prefix’ attribute of ShellMethod annotation.

 

Example

@ShellComponent(value = "Customize Named Parameters Prefix")
public class CustomizeNamedParamKeysPrefix {
	@ShellMethod(value = "Commands to print Student Details", key = "print-student", prefix="-")
	public String printStudentDetails(String name, int age, String city) {

		StringBuilder builder = new StringBuilder();

		builder.append("Hello ").append(name).append("!!!!").append(". You are ").append(age)
				.append(" years old and you are from ").append(city);

		return builder.toString();
	}
}

  Now you can pass named arguments to the command ‘print-student’ using the prefix -.

 

shell:>print-student -name "Krishna" -age 23 -city Hyderabad
Hello Krishna!!!!. You are 23 years old and you are from Hyderabad

 

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 

 

 

  

Previous                                                    Next                                                    Home

Spring Shell: Named Parameters

You can execute the command by specifying a parameter name while executing.

 

Example

@ShellComponent(value = "Named Parameters Demo")
public class NamedParams {
    @ShellMethod(value = "Commands to print User Details", key = "get-user")
    public String printUserDetails(String name, int age, String city) {

        StringBuilder builder = new StringBuilder();

        builder.append("Hello ").append(name).append("!!!!").append(". You are ").append(age)
                .append(" years old and you are from ").append(city);

        return builder.toString();
    }
}

 

Using positional parameters, you can call the command ‘get-user’ like below.

get-user "Krishna" 29 "Bangalore"

 

You can call the above command using named parameters like below.

get-user --name "Krishna" --age 29 --city "Bangalore"

 

As you see above command, I executed the command using a parameter key (e.g. --arg value).

 

Named parameters can be reordered as desired.

get-user --age 29  --city "Bangalore" --name "Krishna"

 

You can even mix both named and positional parameters. The non by-name parameters are resolved in the order they appear

 

get-user --age 29 "Krishna" "Bangalore"

get-user "Krishna" "Bangalore" --age 29

 

shell:>get-user "Krishna" 29 "Bangalore"
Hello Krishna!!!!. You are 29 years old and you are from Bangalore
shell:>
shell:>get-user --name "Krishna" --age 29 --city "Bangalore"
Hello Krishna!!!!. You are 29 years old and you are from Bangalore
shell:>
shell:>get-user --age 29  --city "Bangalore" --name "Krishna"
Hello Krishna!!!!. You are 29 years old and you are from Bangalore
shell:>
shell:>get-user --age 29 "Krishna" "Bangalore" 
Hello Krishna!!!!. You are 29 years old and you are from Bangalore
shell:>
shell:>get-user "Krishna" "Bangalore" --age 29 
Hello Krishna!!!!. You are 29 years old and you are from Bangalore

 

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 

 

  

Previous                                                    Next                                                    Home

Spring Shell: Register command with multiple alias names

In my previous post, I explained how to change the command name using ‘key’ attribute. You can even set multiple keys to the same method using ‘key’ attribute.

 

Example

@ShellComponent(value = "Multiple names to the same commands")
public class MultipleAliases {

    @ShellMethod(value = "Commands to welcome user", key = { "welcome", "wish", "say-hello" })
    public String sayHello(String name) {
        return "Good Morning " + name + "!!!!!";
    }
}

 

Now you can wish the user using either of below command names.

a.   welcome

b.   wish

c.    say-hello

 

shell:>say-hello "Krishna"
Good Morning Krishna!!!!!
shell:>
shell:>welcome "Krishna"
Good Morning Krishna!!!!!
shell:>
shell:>wish "Krishna"
Good Morning Krishna!!!!!
shell:>

 

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 

 

 

Previous                                                    Next                                                    Home

Saturday, 30 January 2021

Java12: Switch expression(Preview mode)

Java12 introduces switch expression feature in preview mode. From Java12 onwards, you can use switch statement either as a statement or an expression.

 

switch expressions bring clear and safe code. Let us see it with an example.

 

Prior to Java12

switch (number) {
	case 2:
		System.out.println("Number is even and prime");
		break;
	case 3:
	case 5:
		System.out.println("You choosen a prime number and it is < 7");
		break;
	case 4:
	case 6:
		System.out.println("You entered even number and it is < 8.");
	
	default:
		System.out.println("You entered a number");
}

 

From Java12 onwards, you can use switch as expression like below.

 

String result = switch (number) {
	case 2 ->  "Number is even and prime";
	case 3, 5 -> "You choosen a prime number and it is < 7";
	case 4, 6 -> "You entered even number and it is < 8.";
	default -> "You entered a number";
};

Find the below working application.

 

SwitchExpressionExample.java

public class SwitchExpressionExample {
	public static void main(String args[]) {
		int number = 2;

		// Prior to Java12
		switch (number) {
		case 2:
			System.out.println("Number is even and prime");
			break;
		case 3:
		case 5:
			System.out.println("You choosen a prime number and it is < 7");
			break;
		case 4:
		case 6:
			System.out.println("You entered even number and it is < 8.");

		default:
			System.out.println("You entered a number");
		}

		// From Java12
		String result = switch (number) {
			case 2 ->  "Number is even and prime";
			case 3, 5 -> "You choosen a prime number and it is < 7";
			case 4, 6 -> "You entered even number and it is < 8.";
			default -> "You entered a number";
		};
		
		System.out.println(result);
	}

}


How to compile SwitchExpressionExample application?

javac --release 12 --enable-preview SwitchExpressionExample.java

 

How to run SwitchExpressionExample?

java --enable-preview SwitchExpressionExample

 

Output

Number is even and prime

Number is even and prime

 

If your case statement has multiple lines, then enclose them in curly braces {} and return the last line using break statement.

 

SwitchExpressionExample2.java

public class SwitchExpressionExample2 {
	public static void main(String args[]) {
		int number = 2;

		
		String result = switch (number) {
		case 2 -> {
			System.out.println("For input 2");
			break "Number is even and prime";
		}
		case 3, 5 -> {
			System.out.println("For input 3 or 5");
			break "You choosen a prime number and it is < 7";
		}
		case 4, 6 -> {
			System.out.println("For input 4 or 6");
			break "You entered even number and it is < 8.";
		}
		default -> {
			System.out.println("No input matched");
			break "You entered a number";
		}
	};

		System.out.println(result);
	}

}


Output

$javac --release 12 --enable-preview SwitchExpressionExample2.java 
Note: SwitchExpressionExample2.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
$
$
$java --enable-preview SwitchExpressionExample2
For input 2
Number is even and prime


Reference

https://openjdk.java.net/jeps/325

 





 

 

Previous                                                    Next                                                    Home

Java12: Unicode support

Unicode 11.0 is the latest version of the Unicode Standard, released on June 5, 2018. Unicode 11.0 adds 684 characters, for a total of 137,374 characters. These additions include 7 new scripts, for a total of 146 scripts, as well as 66 new emoji characters.

 

Unicode11Example.java

package com.sample.app.strings;

public class Unicode11Example {
	public static void main(String args[]) {
		String emoji1 = "\uD83E\uDD76";
		String emoji2 = "\uD83E\uDD81";
		String emoji3 = "\uD83E\uDD85";
		
		System.out.println(emoji1 + " " + emoji2 + " " + emoji3);
	}

}

Output

🥶 🦁 🦅

 

Reference

https://unicode.org/versions/Unicode11.0.0/

https://home.unicode.org/emoji/


  

Previous                                                    Next                                                    Home