Advertisement
wingman007

Java2014_NestedLocalAnonymousLambdaFunctional

Dec 15th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package nestedlocalanonymouslambdafunctional;
  7.  
  8. /**
  9.  *
  10.  * @author Kristiqn
  11.  */
  12. public class NestedLocalAnonymousLambdaFunctional {
  13.    
  14.     // functional interface
  15.     public interface Calculatable {
  16.         double calculate(double x, double y);
  17.     }
  18.    
  19.     // not inner simply nested
  20.     // 1.1 public static class AddNestedClassFunction implements Calculatable{
  21.     // 1.2 inner class
  22.     private class AddNestedClassFunction implements Calculatable{
  23.         @Override
  24.         public double calculate(double x, double y) {
  25.             return x + y;
  26.         }  
  27.     }
  28.    
  29.     /**
  30.      * @param args the command line arguments
  31.      */
  32.     public static void main(String[] args) {
  33.         // TODO code application logic here
  34.         // naive approach. static functions with conditional logic
  35.         System.out.println("The result of operation 4.5 + 6.7 = " + calculateFunction(4.5, 6.7, '+'));
  36.        
  37.         // 1.1 static. When the nested class is static
  38.         // AddNestedClassFunction nestedFuction = new AddNestedClassFunction();
  39.        
  40.         // 1.2 inner class there is no static modifier in the class declaration
  41.         NestedLocalAnonymousLambdaFunctional outer = new NestedLocalAnonymousLambdaFunctional();
  42.         AddNestedClassFunction nestedFuction = outer.new AddNestedClassFunction();    
  43.         // not correct
  44.         // AddNestedClassFunction nestedFuction = new NestedLocalAnonymousLambdaFunctional.AddNestedClassFunction();
  45.         System.out.println("Nested Class implementation 54.6 + 67.3 = " + calculateFunction(54.6, 67.3, nestedFuction));
  46.        
  47.        
  48.         // 2. A Local Class
  49.         class LocalClass implements Calculatable {
  50.             @Override
  51.             public double calculate(double x, double y) {
  52.                 return x + y;
  53.             }
  54.         }    
  55.         System.out.println("Local class implementation 34.7 + 65.8 = " + calculateFunction(34.7, 65.8, new LocalClass()));
  56.        
  57.        
  58.         // 3. Anonymous class
  59.         Calculatable anonymousClass = new Calculatable() {
  60.             @Override
  61.             public double calculate(double x, double y) {
  62.                 return x + y;
  63.             }
  64.         };
  65.         System.out.println("Anonymous class implementation 65.3 + 78.9 = " + calculateFunction(65.3, 78.9, anonymousClass));
  66.        
  67.         // 4. Lambda function
  68.         // Calculatable lambda = (double x, double y) -> x + y; // x -> x * x;
  69.         // Calculatable lambda = (double x, double y) -> {return x + y;};
  70.         // System.out.println("Lambda implementation 56.7 + 89.6 = " + calculateFunction(56.7, 89.6, lambda));
  71.         System.out.println("Lambda implementation 56.7 + 89.6 = " +
  72.                 calculateFunction(56.7, 89.6, (double x, double y)-> x + y));
  73.     }
  74.    
  75.     // functional solution
  76.     public static double calculateFunction(double x, double y, Calculatable function) {
  77.         return function.calculate(x, y);
  78.     }  
  79.    
  80.     // naive solution
  81.     public static double calculateFunction(double x, double y, char operation) {
  82.         double result;
  83.         switch (operation) {
  84.             case '+' :
  85.                 result = x + y;
  86.                 break;
  87.             // ... more functions
  88.             default :
  89.                 result = 0.0;
  90.                 break;
  91.         }
  92.         return result;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement