Friday, January 6, 2017
Favor object composition over class inheritance in Java with Project Lombok Delegate annotation
Subject: Object composition in Java via Lombok @Delegate annotation
Problem Statement : One common drawback of using composition instead of inheritance is that methods being provided by individual components may have to be implemented in the derived type, even if they are only forwarding methods. In contrast, inheritance does not require all of the base class's methods to be re-implemented within the derived class. Rather, the derived class only need to implement (override) the methods having different behavior than the base class methods. This can require significantly less programming effort if the base class contains many methods providing default behavior and only a few of them need to be overridden within the derived class.
Solution: This drawback can be avoided by using traits, mixins, or protocol extensions. Some languages, such as Perl 6, provide a handles keyword to facilitate method forwarding. In Java, Project Lombok lets you implement delegation using a single @Delegate annotation on the field instead of copying and maintaining names and types of all methods from the delegated field
Code : For the ones who says - "No bullshit , just the code..." :))
import lombok.Data;
@Data
public class BasicStudent {
private String name;
public void writeYourNameOnTheBlackBoard (){
System.out.println ("There is no He/she/it in turkish because we are not sexists :) ");
}
}
import lombok.experimental.Delegate;
public class EnhancedStudent {
@Delegate
BasicStudent beaFeaturesCameHereWithAnnotation;
}
public class StudentProcessor {
public static void main (String[] args) {
EnhancedStudent enhancedStudent = new EnhancedStudent ();
enhancedStudent.writeYourNameOnTheBlackBoard ();
}
}
-- for the memory of Christoph Portman
Tuesday, January 3, 2017
Java Maze solver program
Copy , paste, run...
The ones are the walls in the below matrix and zeroes are open ways in the labyrinth.
We are trying to find shortest path to the given destination : which is (x,y)-> (19,8) here in the example
Just modify the maze by playing with the Ones and Zeroes and run it in your local
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company.samples.way8; | |
public class JavaMazeSolver { | |
public static void main (String[] args) { | |
// A sample maze to solve... | |
// We will try to reach to cell [6][8] = val-> (-1) | |
int exitRow = 19; | |
int exitColumn = 8; | |
// One(s)=1 are the blocked cells, | |
// Zero(s)=0 are the open cells | |
int[][] maze = new int[][]{ | |
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
{ 0,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1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },//18 | |
{ 1, 0, 1, 0, 1, 1, 1, 1,-1, 1, 1, 1, 1, 1, 1, 1 },//19 | |
{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },//20 | |
{ 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 } //21 | |
}; | |
// This is someting like linkedList... | |
// We need a two dimensional array, same sized with the given maze | |
LinkedMatrixCell[][] linkedMatrixMatrixCell = new LinkedMatrixCell[maze.length][maze[0].length] ; | |
// Let each cell know the map | |
LinkedMatrixCell.setLinkedMatrixCellMaze (linkedMatrixMatrixCell); | |
// This will build up our whole/complete map, see this constructor | |
// You can print the maze after calling this if you want | |
new LinkedMatrixCell (0,0, maze, 0); | |
// now it is time to call out recursive function starting from the destination | |
findMinPathToTarget ( maze, exitRow, exitColumn, 0); | |
// And lets see some isThisTheShortestPath huh ?? | |
displayTheNavigatableMap ( LinkedMatrixCell.getLinkedMatrixCellMaze ()); | |
} | |
private static void displayTheNavigatableMap (LinkedMatrixCell[][] linkedMatrixCellMaze) { | |
for (int i = 0; i < linkedMatrixCellMaze.length; i++) { | |
for (int j = 0; j < linkedMatrixCellMaze[0].length; j++) { | |
if(linkedMatrixCellMaze[i][j] != null){ | |
System.out.print ("(" + String.format ("%03d",Integer.parseInt (linkedMatrixCellMaze[i][j].toString ())) + ") "); | |
}else{ | |
System.out.print ("(**) "); | |
} | |
} | |
System.out.println (); | |
} | |
System.out.println ("linkedMatrixCellMaze = " + linkedMatrixCellMaze); | |
} | |
public static int findMinPathToTarget (int[][] maze, int exitRow, int exitCol, int distance) { | |
LinkedMatrixCell[][] linkedMatrixCellMaze = LinkedMatrixCell.getLinkedMatrixCellMaze (); | |
LinkedMatrixCell currentCell = linkedMatrixCellMaze[exitRow][exitCol]; | |
DistanceResult distanceResult = currentCell.setDistance2Start (distance); | |
int oneMoreStep = distance + 1; | |
DistanceResult distanceSetResult = null; | |
// try to go left | |
if (currentCell.left != null) { | |
distanceSetResult = currentCell.left.setDistance2Start (oneMoreStep); | |
if (exitCol -1 >= 0 && distanceSetResult.isThisTheShortestPath == true) | |
findMinPathToTarget (maze, exitRow, exitCol -1, distanceSetResult.getDistance ()); | |
} | |
// try to go right | |
if (currentCell.right != null) { | |
distanceSetResult = currentCell.right.setDistance2Start (oneMoreStep); | |
if(exitCol + 1 <= linkedMatrixCellMaze[0].length && distanceSetResult.isThisTheShortestPath == true) | |
findMinPathToTarget (maze, exitRow, exitCol + 1, distanceSetResult.getDistance ()); | |
} | |
// try to go down | |
if (currentCell.down != null) { | |
distanceSetResult = currentCell.down.setDistance2Start (oneMoreStep); | |
if(exitRow <= linkedMatrixCellMaze.length && distanceSetResult.isThisTheShortestPath == true) | |
findMinPathToTarget (maze, exitRow + 1, exitCol, distanceSetResult.getDistance ()); | |
} | |
// try to go up | |
if (currentCell.up != null) { | |
distanceSetResult = currentCell.up.setDistance2Start (oneMoreStep); | |
if(exitRow >= 0 && distanceSetResult.isThisTheShortestPath == true) | |
findMinPathToTarget (maze, exitRow -1, exitCol, distanceSetResult.getDistance ()); | |
} | |
return distance; | |
} | |
static class LinkedMatrixCell { | |
// let each cell has the map information | |
static LinkedMatrixCell[][] linkedMatrixCellMaze; | |
// cell coordinates : value can be 0 (open road), 1 (blocked road) , -1 (destination) | |
int xPosition , yPosition , value; | |
// I will drive my code to find always smaller distances if possible | |
int distance2Start = Integer.MAX_VALUE -1; | |
// let each cell get linked to its neighbours | |
LinkedMatrixCell up, down,left, right = null; | |
public LinkedMatrixCell (int xPosition, int yPosition, int[][] maze, int distance2Start) { | |
linkedMatrixCellMaze[xPosition][yPosition] = this; | |
this.xPosition = xPosition; | |
this.yPosition = yPosition; | |
this.value = maze[this.xPosition][this.yPosition]; | |
// I would refactor the below statements but i guess these are more readable & understandable | |
if (this.yPosition > 0 && maze[xPosition][yPosition - 1] != 1) { | |
if (linkedMatrixCellMaze[xPosition][yPosition - 1] == null) this.left = new LinkedMatrixCell (xPosition, yPosition - 1, maze, distance2Start + 1); | |
else this.left = linkedMatrixCellMaze[xPosition][yPosition - 1]; | |
} | |
if (this.yPosition < maze[0].length - 1 && maze[xPosition][yPosition + 1] != 1) { | |
if (linkedMatrixCellMaze[xPosition][yPosition + 1] == null) this.right = new LinkedMatrixCell (xPosition, yPosition + 1, maze,distance2Start + 1); | |
else this.right = linkedMatrixCellMaze[xPosition][yPosition + 1]; | |
} | |
if (this.xPosition > 0 && maze[xPosition - 1][yPosition] != 1) { | |
if (linkedMatrixCellMaze[xPosition - 1][yPosition] == null) this.up = new LinkedMatrixCell (xPosition - 1, yPosition, maze,distance2Start + 1); | |
else this.up = linkedMatrixCellMaze[xPosition - 1][yPosition]; | |
} | |
if (this.xPosition < maze.length - 1 && maze[xPosition + 1][yPosition] != 1) { | |
if (linkedMatrixCellMaze[xPosition + 1][yPosition] == null) this.down = new LinkedMatrixCell (xPosition + 1, yPosition, maze,distance2Start + 1); | |
else this.down = linkedMatrixCellMaze[xPosition + 1][yPosition]; | |
} | |
} | |
public static void setLinkedMatrixCellMaze (LinkedMatrixCell[][] linkedMatrixCellMaze) { | |
LinkedMatrixCell.linkedMatrixCellMaze = linkedMatrixCellMaze; | |
} | |
public static LinkedMatrixCell[][] getLinkedMatrixCellMaze () { | |
return linkedMatrixCellMaze; | |
} | |
public int getDistance2Start () { | |
return distance2Start; | |
} | |
public DistanceResult setDistance2Start (int distance2Start) { | |
DistanceResult result = new DistanceResult (distance2Start, true); | |
DistanceResult result1 = checkOtherSide (distance2Start, this.up); | |
if(result1.getDistance () < result.getDistance ()){ result = result1;} | |
DistanceResult result2 = checkOtherSide (distance2Start, this.down); | |
if(result2.getDistance () < result.getDistance ()){ result = result2;} | |
DistanceResult result3 = checkOtherSide (distance2Start, this.left); | |
if(result3.getDistance () < result.getDistance ()){ result = result3;} | |
DistanceResult result4 = checkOtherSide (distance2Start, this.right); | |
if(result4.getDistance () < result.getDistance ()){ result = result4;} | |
return result; | |
} | |
private DistanceResult checkOtherSide (int distance2Start, LinkedMatrixCell sideCell) { | |
DistanceResult result; | |
if (sideCell != null && sideCell.getDistance2Start () + 1 < distance2Start) { | |
this.distance2Start = sideCell.getDistance2Start () + 1; | |
result = new DistanceResult (distance2Start, false); | |
} else { | |
if (this.distance2Start >= distance2Start) { | |
this.distance2Start = distance2Start; | |
result = new DistanceResult (distance2Start, true); | |
} else { | |
result = new DistanceResult (this.distance2Start, false); | |
} | |
} | |
return result; | |
} | |
@Override | |
public String toString () { | |
return ""+distance2Start ; | |
} | |
} | |
static final class DistanceResult{ | |
int distance; | |
boolean isThisTheShortestPath; | |
public DistanceResult (int distance, boolean isThisTheShortestPath) { | |
this.distance = distance; | |
this.isThisTheShortestPath = isThisTheShortestPath; | |
} | |
public int getDistance () { | |
return distance; | |
} | |
@Override | |
public String toString () { | |
return "DistanceResult{" + | |
"distance=" + distance + | |
'}'; | |
} | |
} | |
} |
HuntMacllory Algorithm Java Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company.samples.way8; | |
import java.util.Arrays; | |
/** | |
* G C A C G C T G G C A C G C T G <= SJ | |
* 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
* G 0 1 1 1 1 1 1 1 1 G 0 1 1 0 0 0 0 0 0 | |
* A 0 1 1 2 2 2 2 2 2 A 0 0 0 1 0 0 0 0 0 | |
* C 0 1 2 2 3 3 3 3 3 C 0 0 0 0 1 0 0 0 0 | |
* G 0 1 2 2 3 4 4 4 4 SI=> G 0 0 0 0 0 1 0 0 0 | |
* C 0 1 2 2 3 4 5 5 5 C 0 0 0 0 0 0 1 1 0 | |
* G 0 1 2 2 3 4 5 5 6 G 0 0 0 0 0 0 0 0 1 | |
* C 0 1 2 2 3 4 5 5 6 C 0 0 0 0 0 0 0 0 1 | |
* G 0 1 2 2 3 4 5 5 6 G 0 0 0 0 0 0 0 0 1 | |
* | |
S-i : G-ACGC-GCG | |
S-j : GGCACGGC-- | |
* | |
*/ | |
public class HuntMacllury { | |
public static void main (String[] args) { | |
String si = "GACGCGCG"; | |
String sj = "GCACGCTG"; | |
int[][] matrix = new int[si.length () + 1][sj.length () + 1]; | |
for (int i = 1; i < si.length () + 1; i++) { | |
for (int j = 1; j < sj.length () + 1; j++) { | |
if (si.charAt (i - 1) == sj.charAt (j - 1)) { | |
matrix[i][j] = matrix[i - 1][j - 1] + 1; | |
} else { | |
matrix[i][j] = matrix[i - 1][j] > matrix[i][j - 1] ? matrix[i - 1][j] : matrix[i][j - 1]; | |
} | |
} | |
} | |
printMatrix (matrix); | |
int[][] directionMatrix = createDirectionMatrix (matrix); | |
directionMatrix = getIncrementalDiagonalMatrix (matrix, directionMatrix, matrix.length - 1, matrix[0].length - 1); | |
printMatrix (directionMatrix); | |
printStringI (si, directionMatrix); | |
printStringJ (sj, directionMatrix); | |
} | |
private static void printStringJ (String sj, int[][] directionMatrix) { | |
System.out.print ("S-j : "); | |
for (int j = 1; j < directionMatrix[0].length; j++) { | |
boolean charPrinted = false; | |
for (int i = 1; i < directionMatrix.length; i++) { | |
if (directionMatrix[i][j] == 1) { | |
if (!charPrinted) { | |
System.out.print (sj.charAt (j - 1)); | |
} else { | |
System.out.print ("-"); | |
} | |
charPrinted = true; | |
} | |
} | |
} | |
System.out.println (""); | |
} | |
private static void printStringI (String si, int[][] directionMatrix) { | |
System.out.print ("S-i : "); | |
for (int i = 1; i < directionMatrix.length; i++) { | |
boolean charPrinted = false; | |
for (int j = 1; j < directionMatrix[0].length; j++) { | |
if (directionMatrix[i][j] == 1) { | |
if (!charPrinted) { | |
System.out.print (si.charAt (i - 1)); | |
} else { | |
System.out.print ("-"); | |
} | |
charPrinted = true; | |
} | |
} | |
} | |
System.out.println (""); | |
} | |
private static void printMatrix (int[][] matrix) { | |
for (int i = 0; i < matrix[0].length; i++) { | |
for (int j = 0; j < matrix[0].length; j++) { | |
System.out.print (matrix[i][j] + " "); | |
} | |
System.out.println (""); | |
} | |
System.out.println (""); | |
} | |
private static int[][] getIncrementalDiagonalMatrix (int[][] matrix, int[][] directionMatrix, int starti, int startj) { | |
directionMatrix[starti][startj] = 1; | |
if (starti == 1 && startj == 1) { | |
return directionMatrix; | |
} | |
int i = starti; | |
int j = startj; | |
int left = matrix[i][j - 1]; | |
int up = matrix[i - 1][j]; | |
int diagon = matrix[i - 1][j - 1]; | |
if (diagon >= up && diagon >= left) { | |
i--; | |
j--; | |
return getIncrementalDiagonalMatrix (matrix, directionMatrix, i, j); | |
} else if (up > diagon && up > left) { | |
i--; | |
return getIncrementalDiagonalMatrix (matrix, directionMatrix, i, j); | |
} else if (left > diagon && left > up) { | |
j--; | |
return getIncrementalDiagonalMatrix (matrix, directionMatrix, i, j); | |
} | |
return directionMatrix; | |
} | |
private static int[][] createDirectionMatrix (int[][] matrix) { | |
int[][] directionMatrix = new int[matrix.length][matrix[0].length]; | |
for (int i = 0; i < matrix.length; i++) { | |
Arrays.fill (directionMatrix[i], 0); | |
} | |
return directionMatrix; | |
} | |
} |
Subscribe to:
Posts (Atom)