#1021 Checkstyle fixes for Layers

This commit is contained in:
Ilkka Seppälä 2019-11-05 17:38:05 +02:00
parent 47ae477a56
commit 8ecdee44c8
15 changed files with 69 additions and 92 deletions

View File

@ -26,32 +26,32 @@ package com.iluwatar.layers;
import java.util.List; import java.util.List;
/** /**
* * Layers is an architectural style where software responsibilities are divided among the
* Layers is an architectural style where software responsibilities are divided among the different layers of the * different layers of the application.
* application. *
* <p> * <p>This example demonstrates a traditional 3-layer architecture consisting of data access
* This example demonstrates a traditional 3-layer architecture consisting of data access layer, business layer and * layer, business layer and presentation layer.
* presentation layer. *
* <p> * <p>The data access layer is formed of Spring Data repositories <code>CakeDao</code>,
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>, <code>CakeToppingDao</code> and * <code>CakeToppingDao</code> and <code>CakeLayerDao</code>. The repositories can be used
* <code>CakeLayerDao</code>. The repositories can be used for CRUD operations on cakes, cake toppings and cake layers * for CRUD operations on cakes, cake toppings and cake layers respectively.
* respectively. *
* <p> * <p>The business layer is built on top of the data access layer. <code>CakeBakingService</code>
* The business layer is built on top of the data access layer. <code>CakeBakingService</code> offers methods to * offers methods to retrieve available cake toppings and cake layers and baked cakes. Also the
* retrieve available cake toppings and cake layers and baked cakes. Also the service is used to create new cakes out of * service is used to create new cakes out of cake toppings and cake layers.
* cake toppings and cake layers. *
* <p> * <p>The presentation layer is built on the business layer and in this example it simply lists
* The presentation layer is built on the business layer and in this example it simply lists the cakes that have been * the cakes that have been baked.
* baked. *
* <p> * <p>We have applied so called strict layering which means that the layers can only access the
* We have applied so called strict layering which means that the layers can only access the classes directly beneath * classes directly beneath them. This leads the solution to create an additional set of DTOs
* them. This leads the solution to create an additional set of DTOs ( <code>CakeInfo</code>, * ( <code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) to translate
* <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) to translate data between layers. In other words, * data between layers. In other words, <code>CakeBakingService</code> cannot return entities
* <code>CakeBakingService</code> cannot return entities ( <code>Cake</code>, <code>CakeTopping</code>, * ( <code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>) directly since these
* <code>CakeLayer</code>) directly since these reside on data access layer but instead translates these into business * reside on data access layer but instead translates these into business layer DTOs
* layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) and returns them * (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) and returns
* instead. This way the presentation layer does not have any knowledge of other layers than the business layer and thus * them instead. This way the presentation layer does not have any knowledge of other layers than
* is not affected by changes to them. * the business layer and thus is not affected by changes to them.
* *
* @see Cake * @see Cake
* @see CakeTopping * @see CakeTopping
@ -70,7 +70,7 @@ public class App {
private static CakeBakingService cakeBakingService = new CakeBakingServiceImpl(); private static CakeBakingService cakeBakingService = new CakeBakingServiceImpl();
/** /**
* Application entry point * Application entry point.
* *
* @param args Command line parameters * @param args Command line parameters
*/ */
@ -85,7 +85,7 @@ public class App {
} }
/** /**
* Initializes the example data * Initializes the example data.
*/ */
private static void initializeData(CakeBakingService cakeBakingService) { private static void initializeData(CakeBakingService cakeBakingService) {
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200)); cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
@ -108,10 +108,10 @@ public class App {
} catch (CakeBakingException e) { } catch (CakeBakingException e) {
e.printStackTrace(); e.printStackTrace();
} }
CakeInfo cake2 = CakeInfo cake2 = new CakeInfo(new CakeToppingInfo("cherry", 0), List.of(
new CakeInfo(new CakeToppingInfo("cherry", 0), List.of( new CakeLayerInfo("vanilla", 0),
new CakeLayerInfo("vanilla", 0), new CakeLayerInfo("lemon", 0), new CakeLayerInfo( new CakeLayerInfo("lemon", 0),
"strawberry", 0))); new CakeLayerInfo("strawberry", 0)));
try { try {
cakeBakingService.bakeNewCake(cake2); cakeBakingService.bakeNewCake(cake2);
} catch (CakeBakingException e) { } catch (CakeBakingException e) {

View File

@ -35,9 +35,7 @@ import javax.persistence.OneToMany;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
/** /**
* * Cake entity.
* Cake entity
*
*/ */
@Entity @Entity
public class Cake { public class Cake {

View File

@ -24,15 +24,14 @@
package com.iluwatar.layers; package com.iluwatar.layers;
/** /**
* * Custom exception used in cake baking.
* Custom exception used in cake baking
*
*/ */
public class CakeBakingException extends Exception { public class CakeBakingException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public CakeBakingException() {} public CakeBakingException() {
}
public CakeBakingException(String message) { public CakeBakingException(String message) {
super(message); super(message);

View File

@ -26,39 +26,37 @@ package com.iluwatar.layers;
import java.util.List; import java.util.List;
/** /**
* * Service for cake baking operations.
* Service for cake baking operations
*
*/ */
public interface CakeBakingService { public interface CakeBakingService {
/** /**
* Bakes new cake according to parameters * Bakes new cake according to parameters.
*/ */
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException; void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
/** /**
* Get all cakes * Get all cakes.
*/ */
List<CakeInfo> getAllCakes(); List<CakeInfo> getAllCakes();
/** /**
* Store new cake topping * Store new cake topping.
*/ */
void saveNewTopping(CakeToppingInfo toppingInfo); void saveNewTopping(CakeToppingInfo toppingInfo);
/** /**
* Get available cake toppings * Get available cake toppings.
*/ */
List<CakeToppingInfo> getAvailableToppings(); List<CakeToppingInfo> getAvailableToppings();
/** /**
* Add new cake layer * Add new cake layer.
*/ */
void saveNewLayer(CakeLayerInfo layerInfo); void saveNewLayer(CakeLayerInfo layerInfo);
/** /**
* Get available cake layers * Get available cake layers.
*/ */
List<CakeLayerInfo> getAvailableLayers(); List<CakeLayerInfo> getAvailableLayers();
} }

View File

@ -37,9 +37,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
* * Implementation of CakeBakingService.
* Implementation of CakeBakingService
*
*/ */
@Service @Service
@Transactional @Transactional
@ -73,7 +71,8 @@ public class CakeBakingServiceImpl implements CakeBakingService {
} }
} }
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class); CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
Optional<CakeTopping> topping = toppingBean.findById(matchingToppings.iterator().next().getId()); Optional<CakeTopping> topping = toppingBean.findById(
matchingToppings.iterator().next().getId());
CakeDao cakeBean = context.getBean(CakeDao.class); CakeDao cakeBean = context.getBean(CakeDao.class);
if (topping.isPresent()) { if (topping.isPresent()) {
Cake cake = new Cake(); Cake cake = new Cake();

View File

@ -27,9 +27,7 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* * CRUD repository for cakes.
* CRUD repository for cakes
*
*/ */
@Repository @Repository
public interface CakeDao extends CrudRepository<Cake, Long> { public interface CakeDao extends CrudRepository<Cake, Long> {

View File

@ -27,9 +27,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
/** /**
* * DTO for cakes.
* DTO for cakes
*
*/ */
public class CakeInfo { public class CakeInfo {
@ -38,7 +36,7 @@ public class CakeInfo {
public final List<CakeLayerInfo> cakeLayerInfos; public final List<CakeLayerInfo> cakeLayerInfos;
/** /**
* Constructor * Constructor.
*/ */
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) { public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
this.id = Optional.of(id); this.id = Optional.of(id);
@ -47,7 +45,7 @@ public class CakeInfo {
} }
/** /**
* Constructor * Constructor.
*/ */
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) { public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
this.id = Optional.empty(); this.id = Optional.empty();
@ -56,7 +54,7 @@ public class CakeInfo {
} }
/** /**
* Calculate calories * Calculate calories.
*/ */
public int calculateTotalCalories() { public int calculateTotalCalories() {
int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0; int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;

View File

@ -30,9 +30,7 @@ import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
/** /**
* * CakeLayer entity.
* CakeLayer entity
*
*/ */
@Entity @Entity
public class CakeLayer { public class CakeLayer {
@ -48,7 +46,8 @@ public class CakeLayer {
@ManyToOne(cascade = CascadeType.ALL) @ManyToOne(cascade = CascadeType.ALL)
private Cake cake; private Cake cake;
public CakeLayer() {} public CakeLayer() {
}
public CakeLayer(String name, int calories) { public CakeLayer(String name, int calories) {
this.setName(name); this.setName(name);

View File

@ -27,9 +27,7 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* * CRUD repository for cake layers.
* CRUD repository for cake layers
*
*/ */
@Repository @Repository
public interface CakeLayerDao extends CrudRepository<CakeLayer, Long> { public interface CakeLayerDao extends CrudRepository<CakeLayer, Long> {

View File

@ -26,9 +26,7 @@ package com.iluwatar.layers;
import java.util.Optional; import java.util.Optional;
/** /**
* * DTO for cake layers.
* DTO for cake layers
*
*/ */
public class CakeLayerInfo { public class CakeLayerInfo {
@ -37,7 +35,7 @@ public class CakeLayerInfo {
public final int calories; public final int calories;
/** /**
* Constructor * Constructor.
*/ */
public CakeLayerInfo(Long id, String name, int calories) { public CakeLayerInfo(Long id, String name, int calories) {
this.id = Optional.of(id); this.id = Optional.of(id);
@ -46,7 +44,7 @@ public class CakeLayerInfo {
} }
/** /**
* Constructor * Constructor.
*/ */
public CakeLayerInfo(String name, int calories) { public CakeLayerInfo(String name, int calories) {
this.id = Optional.empty(); this.id = Optional.empty();

View File

@ -30,9 +30,7 @@ import javax.persistence.Id;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
/** /**
* * CakeTopping entity.
* CakeTopping entity
*
*/ */
@Entity @Entity
public class CakeTopping { public class CakeTopping {
@ -48,7 +46,8 @@ public class CakeTopping {
@OneToOne(cascade = CascadeType.ALL) @OneToOne(cascade = CascadeType.ALL)
private Cake cake; private Cake cake;
public CakeTopping() {} public CakeTopping() {
}
public CakeTopping(String name, int calories) { public CakeTopping(String name, int calories) {
this.setName(name); this.setName(name);

View File

@ -27,9 +27,7 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* * CRUD repository cake toppings.
* CRUD repository cake toppings
*
*/ */
@Repository @Repository
public interface CakeToppingDao extends CrudRepository<CakeTopping, Long> { public interface CakeToppingDao extends CrudRepository<CakeTopping, Long> {

View File

@ -26,9 +26,7 @@ package com.iluwatar.layers;
import java.util.Optional; import java.util.Optional;
/** /**
* * DTO for cake toppings.
* DTO for cake toppings
*
*/ */
public class CakeToppingInfo { public class CakeToppingInfo {
@ -37,7 +35,7 @@ public class CakeToppingInfo {
public final int calories; public final int calories;
/** /**
* Constructor * Constructor.
*/ */
public CakeToppingInfo(Long id, String name, int calories) { public CakeToppingInfo(Long id, String name, int calories) {
this.id = Optional.of(id); this.id = Optional.of(id);
@ -46,7 +44,7 @@ public class CakeToppingInfo {
} }
/** /**
* Constructor * Constructor.
*/ */
public CakeToppingInfo(String name, int calories) { public CakeToppingInfo(String name, int calories) {
this.id = Optional.empty(); this.id = Optional.empty();
@ -56,6 +54,7 @@ public class CakeToppingInfo {
@Override @Override
public String toString() { public String toString() {
return String.format("CakeToppingInfo id=%d name=%s calories=%d", id.orElse(-1L), name, calories); return String.format("CakeToppingInfo id=%d name=%s calories=%d",
id.orElse(-1L), name, calories);
} }
} }

View File

@ -27,9 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* * View implementation for displaying cakes.
* View implementation for displaying cakes
*
*/ */
public class CakeViewImpl implements View { public class CakeViewImpl implements View {

View File

@ -24,9 +24,7 @@
package com.iluwatar.layers; package com.iluwatar.layers;
/** /**
* * View interface.
* View interface
*
*/ */
public interface View { public interface View {