Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package nestedlocalanonymouslambdafunctional;
- /**
- *
- * @author Kristiqn
- */
- public class NestedLocalAnonymousLambdaFunctional {
- // functional interface
- public interface Calculatable {
- double calculate(double x, double y);
- }
- // not inner simply nested
- // 1.1 public static class AddNestedClassFunction implements Calculatable{
- // 1.2 inner class
- private class AddNestedClassFunction implements Calculatable{
- @Override
- public double calculate(double x, double y) {
- return x + y;
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- // naive approach. static functions with conditional logic
- System.out.println("The result of operation 4.5 + 6.7 = " + calculateFunction(4.5, 6.7, '+'));
- // 1.1 static. When the nested class is static
- // AddNestedClassFunction nestedFuction = new AddNestedClassFunction();
- // 1.2 inner class there is no static modifier in the class declaration
- NestedLocalAnonymousLambdaFunctional outer = new NestedLocalAnonymousLambdaFunctional();
- AddNestedClassFunction nestedFuction = outer.new AddNestedClassFunction();
- // not correct
- // AddNestedClassFunction nestedFuction = new NestedLocalAnonymousLambdaFunctional.AddNestedClassFunction();
- System.out.println("Nested Class implementation 54.6 + 67.3 = " + calculateFunction(54.6, 67.3, nestedFuction));
- // 2. A Local Class
- class LocalClass implements Calculatable {
- @Override
- public double calculate(double x, double y) {
- return x + y;
- }
- }
- System.out.println("Local class implementation 34.7 + 65.8 = " + calculateFunction(34.7, 65.8, new LocalClass()));
- // 3. Anonymous class
- Calculatable anonymousClass = new Calculatable() {
- @Override
- public double calculate(double x, double y) {
- return x + y;
- }
- };
- System.out.println("Anonymous class implementation 65.3 + 78.9 = " + calculateFunction(65.3, 78.9, anonymousClass));
- // 4. Lambda function
- // Calculatable lambda = (double x, double y) -> x + y; // x -> x * x;
- // Calculatable lambda = (double x, double y) -> {return x + y;};
- // System.out.println("Lambda implementation 56.7 + 89.6 = " + calculateFunction(56.7, 89.6, lambda));
- System.out.println("Lambda implementation 56.7 + 89.6 = " +
- calculateFunction(56.7, 89.6, (double x, double y)-> x + y));
- }
- // functional solution
- public static double calculateFunction(double x, double y, Calculatable function) {
- return function.calculate(x, y);
- }
- // naive solution
- public static double calculateFunction(double x, double y, char operation) {
- double result;
- switch (operation) {
- case '+' :
- result = x + y;
- break;
- // ... more functions
- default :
- result = 0.0;
- break;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement