Merge pull request #6262 from soufiane-cheouati/master

Adding marker interfaces files
This commit is contained in:
Eric Martin 2019-02-06 20:53:20 -06:00 committed by GitHub
commit c9fd46e693
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.markerinterface;
public interface DeletableShape extends Shape {
}

View File

@ -0,0 +1,22 @@
package com.baeldung.markerinterface;
public class Rectangle implements DeletableShape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getCircumference() {
return 2 * (width + height);
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.markerinterface;
public interface Shape {
double getArea();
double getCircumference();
}

View File

@ -0,0 +1,14 @@
package com.baeldung.markerinterface;
public class ShapeDao {
public boolean delete(Object object) {
if (!(object instanceof DeletableShape)) {
return false;
}
// Calling the code that deletes the entity from the database
return true;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.markerinterface;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class MarkerInterfaceUnitTest {
@Test
public void givenDeletableObjectThenTrueReturned() {
ShapeDao shapeDao = new ShapeDao();
Object rectangle = new Rectangle(2, 3);
boolean result = shapeDao.delete(rectangle);
assertEquals(true, result);
}
@Test
public void givenNonDeletableObjectThenFalseReturned() {
ShapeDao shapeDao = new ShapeDao();
Object object = new Object();
boolean result = shapeDao.delete(object);
assertEquals(false, result);
}
}