#1021 enforce Checkstyle rules in the build
This commit is contained in:
@ -20,13 +20,15 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
import static com.iluwatar.roleobject.Role.Borrower;
|
||||
import static com.iluwatar.roleobject.Role.Investor;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.iluwatar.roleobject.Role.*;
|
||||
|
||||
/**
|
||||
* The Role Object pattern suggests to model context-specific views
|
||||
* of an object as separate role objects which are
|
||||
@ -39,55 +41,60 @@ import static com.iluwatar.roleobject.Role.*;
|
||||
* investor, respectively. Both roles could as well be played by a single {@link Customer} object.
|
||||
* The common superclass for customer-specific roles is provided by {@link CustomerRole},
|
||||
* which also supports the {@link Customer} interface.
|
||||
* <p>
|
||||
* The {@link CustomerRole} class is abstract and not meant to be instantiated.
|
||||
* Concrete subclasses of {@link CustomerRole}, for example {@link BorrowerRole} or {@link InvestorRole},
|
||||
* define and implement the interface for specific roles. It is only
|
||||
*
|
||||
* <p>The {@link CustomerRole} class is abstract and not meant to be instantiated.
|
||||
* Concrete subclasses of {@link CustomerRole}, for example {@link BorrowerRole}
|
||||
* or {@link InvestorRole}, define and implement the interface for specific roles. It is only
|
||||
* these subclasses which are instantiated at runtime.
|
||||
* The {@link BorrowerRole} class defines the context-specific view of {@link Customer} objects as needed by the loan department.
|
||||
* The {@link BorrowerRole} class defines the context-specific view of {@link Customer}
|
||||
* objects as needed by the loan department.
|
||||
* It defines additional operations to manage the customer’s
|
||||
* credits and securities. Similarly, the {@link InvestorRole} class adds operations specific
|
||||
* to the investment department’s view of customers.
|
||||
* A client like the loan application may either work with objects of the {@link CustomerRole} class, using the interface class
|
||||
* {@link Customer}, or with objects of concrete {@link CustomerRole} subclasses. Suppose the loan application knows a particular
|
||||
* {@link Customer} instance through its {@link Customer} interface. The loan application may want to check whether the {@link Customer}
|
||||
* object plays the role of Borrower.
|
||||
* To this end it calls {@link Customer#hasRole(Role)} with a suitable role specification. For the purpose of
|
||||
* our example, let’s assume we can name roles with enum.
|
||||
* If the {@link Customer} object can play the role named “Borrower,” the loan application will ask it
|
||||
* to return a reference to the corresponding object.
|
||||
* A client like the loan application may either work with objects of the {@link CustomerRole}
|
||||
* class, using the interface class {@link Customer}, or with objects of concrete
|
||||
* {@link CustomerRole} subclasses. Suppose the loan application knows a particular
|
||||
* {@link Customer} instance through its {@link Customer} interface. The loan application
|
||||
* may want to check whether the {@link Customer} object plays the role of Borrower.
|
||||
* To this end it calls {@link Customer#hasRole(Role)} with a suitable role specification. For
|
||||
* the purpose of our example, let’s assume we can name roles with enum.
|
||||
* If the {@link Customer} object can play the role named “Borrower,” the loan application will
|
||||
* ask it to return a reference to the corresponding object.
|
||||
* The loan application may now use this reference to call Borrower-specific operations.
|
||||
*/
|
||||
public class ApplicationRoleObject {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Role.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(Role.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Customer customer = Customer.newCustomer(Borrower, Investor);
|
||||
/**
|
||||
* Main entry point.
|
||||
*
|
||||
* @param args program arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Customer customer = Customer.newCustomer(Borrower, Investor);
|
||||
|
||||
logger.info(" the new customer created : {}", customer);
|
||||
logger.info(" the new customer created : {}", customer);
|
||||
|
||||
boolean hasBorrowerRole = customer.hasRole(Borrower);
|
||||
logger.info(" customer has a borrowed role - {}", hasBorrowerRole);
|
||||
boolean hasInvestorRole = customer.hasRole(Investor);
|
||||
logger.info(" customer has an investor role - {}", hasInvestorRole);
|
||||
boolean hasBorrowerRole = customer.hasRole(Borrower);
|
||||
logger.info(" customer has a borrowed role - {}", hasBorrowerRole);
|
||||
boolean hasInvestorRole = customer.hasRole(Investor);
|
||||
logger.info(" customer has an investor role - {}", hasInvestorRole);
|
||||
|
||||
customer.getRole(Investor, InvestorRole.class)
|
||||
.ifPresent(inv -> {
|
||||
inv.setAmountToInvest(1000);
|
||||
inv.setName("Billy");
|
||||
});
|
||||
customer.getRole(Borrower, BorrowerRole.class)
|
||||
.ifPresent(inv -> inv.setName("Johny"));
|
||||
|
||||
customer.getRole(Investor, InvestorRole.class)
|
||||
.map(InvestorRole::invest)
|
||||
.ifPresent(logger::info);
|
||||
|
||||
customer.getRole(Borrower, BorrowerRole.class)
|
||||
.map(BorrowerRole::borrow)
|
||||
.ifPresent(logger::info);
|
||||
}
|
||||
customer.getRole(Investor, InvestorRole.class)
|
||||
.ifPresent(inv -> {
|
||||
inv.setAmountToInvest(1000);
|
||||
inv.setName("Billy");
|
||||
});
|
||||
customer.getRole(Borrower, BorrowerRole.class)
|
||||
.ifPresent(inv -> inv.setName("Johny"));
|
||||
|
||||
customer.getRole(Investor, InvestorRole.class)
|
||||
.map(InvestorRole::invest)
|
||||
.ifPresent(logger::info);
|
||||
|
||||
customer.getRole(Borrower, BorrowerRole.class)
|
||||
.map(BorrowerRole::borrow)
|
||||
.ifPresent(logger::info);
|
||||
}
|
||||
}
|
||||
|
@ -20,21 +20,23 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
public class BorrowerRole extends CustomerRole{
|
||||
private String name;
|
||||
public class BorrowerRole extends CustomerRole {
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
private String name;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String borrow(){
|
||||
return String.format("Borrower %s wants to get some money.",name);
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String borrow() {
|
||||
return String.format("Borrower %s wants to get some money.", name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
import java.util.Optional;
|
||||
@ -29,51 +30,52 @@ import java.util.Optional;
|
||||
*/
|
||||
public abstract class Customer {
|
||||
|
||||
/**
|
||||
* Add specific role @see {@link Role}
|
||||
*
|
||||
* @param role to add
|
||||
* @return true if the operation has been successful otherwise false
|
||||
*/
|
||||
public abstract boolean addRole(Role role);
|
||||
/**
|
||||
* Add specific role @see {@link Role}.
|
||||
* @param role to add
|
||||
* @return true if the operation has been successful otherwise false
|
||||
*/
|
||||
public abstract boolean addRole(Role role);
|
||||
|
||||
/**
|
||||
* Check specific role @see {@link Role}
|
||||
*
|
||||
* @param role to check
|
||||
* @return true if the role exists otherwise false
|
||||
*/
|
||||
/**
|
||||
* Check specific role @see {@link Role}.
|
||||
* @param role to check
|
||||
* @return true if the role exists otherwise false
|
||||
*/
|
||||
|
||||
public abstract boolean hasRole(Role role);
|
||||
public abstract boolean hasRole(Role role);
|
||||
|
||||
/**
|
||||
* Remove specific role @see {@link Role}
|
||||
*
|
||||
* @param role to remove
|
||||
* @return true if the operation has been successful otherwise false
|
||||
*/
|
||||
public abstract boolean remRole(Role role);
|
||||
/**
|
||||
* Remove specific role @see {@link Role}.
|
||||
* @param role to remove
|
||||
* @return true if the operation has been successful otherwise false
|
||||
*/
|
||||
public abstract boolean remRole(Role role);
|
||||
|
||||
/**
|
||||
* Get specific instance associated with this role @see {@link Role}
|
||||
*
|
||||
* @param role to get
|
||||
* @param expectedRole instance class expected to get
|
||||
* @return optional with value if the instance exists and corresponds expected class
|
||||
*/
|
||||
public abstract <T extends Customer> Optional<T> getRole(Role role, Class<T> expectedRole);
|
||||
/**
|
||||
* Get specific instance associated with this role @see {@link Role}.
|
||||
* @param role to get
|
||||
* @param expectedRole instance class expected to get
|
||||
* @return optional with value if the instance exists and corresponds expected class
|
||||
*/
|
||||
public abstract <T extends Customer> Optional<T> getRole(Role role, Class<T> expectedRole);
|
||||
|
||||
|
||||
public static Customer newCustomer() {
|
||||
return new CustomerCore();
|
||||
}
|
||||
|
||||
public static Customer newCustomer(Role... role) {
|
||||
Customer customer = newCustomer();
|
||||
for (Role r : role) {
|
||||
customer.addRole(r);
|
||||
}
|
||||
return customer;
|
||||
public static Customer newCustomer() {
|
||||
return new CustomerCore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Customer} with given roles.
|
||||
* @param role roles
|
||||
* @return Customer
|
||||
*/
|
||||
public static Customer newCustomer(Role... role) {
|
||||
Customer customer = newCustomer();
|
||||
for (Role r : role) {
|
||||
customer.addRole(r);
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,56 +20,60 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Core class to store different customer roles
|
||||
* Core class to store different customer roles.
|
||||
*
|
||||
* @see CustomerRole
|
||||
* Note: not thread safe
|
||||
* @see CustomerRole Note: not thread safe
|
||||
*/
|
||||
public class CustomerCore extends Customer {
|
||||
|
||||
private Map<Role, CustomerRole> roles;
|
||||
private Map<Role, CustomerRole> roles;
|
||||
|
||||
public CustomerCore() {
|
||||
roles = new HashMap<>();
|
||||
}
|
||||
public CustomerCore() {
|
||||
roles = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addRole(Role role) {
|
||||
return role
|
||||
.instance()
|
||||
.map(inst -> {
|
||||
roles.put(role, inst);
|
||||
return true;
|
||||
})
|
||||
.orElse(false);
|
||||
}
|
||||
@Override
|
||||
public boolean addRole(Role role) {
|
||||
return role
|
||||
.instance()
|
||||
.map(inst -> {
|
||||
roles.put(role, inst);
|
||||
return true;
|
||||
})
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasRole(Role role) {
|
||||
return roles.containsKey(role);
|
||||
}
|
||||
@Override
|
||||
public boolean hasRole(Role role) {
|
||||
return roles.containsKey(role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remRole(Role role) {
|
||||
return Objects.nonNull(roles.remove(role));
|
||||
}
|
||||
@Override
|
||||
public boolean remRole(Role role) {
|
||||
return Objects.nonNull(roles.remove(role));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Customer> Optional<T> getRole(Role role, Class<T> expectedRole) {
|
||||
return Optional
|
||||
.ofNullable(roles.get(role))
|
||||
.filter(expectedRole::isInstance)
|
||||
.map(expectedRole::cast);
|
||||
}
|
||||
@Override
|
||||
public <T extends Customer> Optional<T> getRole(Role role, Class<T> expectedRole) {
|
||||
return Optional
|
||||
.ofNullable(roles.get(role))
|
||||
.filter(expectedRole::isInstance)
|
||||
.map(expectedRole::cast);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String roles = Arrays.toString(this.roles.keySet().toArray());
|
||||
return "Customer{roles=" + roles + "}";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
String roles = Arrays.toString(this.roles.keySet().toArray());
|
||||
return "Customer{roles=" + roles + "}";
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,11 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
/**
|
||||
* Key abstraction for segregated roles
|
||||
* Key abstraction for segregated roles.
|
||||
*/
|
||||
public abstract class CustomerRole extends CustomerCore{}
|
||||
public abstract class CustomerRole extends CustomerCore{
|
||||
}
|
||||
|
@ -20,29 +20,32 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
public class InvestorRole extends CustomerRole {
|
||||
private String name;
|
||||
private long amountToInvest;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
private String name;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
private long amountToInvest;
|
||||
|
||||
public long getAmountToInvest() {
|
||||
return amountToInvest;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setAmountToInvest(long amountToInvest) {
|
||||
this.amountToInvest = amountToInvest;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String invest() {
|
||||
return String.format("Investor %s has invested %d dollars", name, amountToInvest);
|
||||
}
|
||||
public long getAmountToInvest() {
|
||||
return amountToInvest;
|
||||
}
|
||||
|
||||
public void setAmountToInvest(long amountToInvest) {
|
||||
this.amountToInvest = amountToInvest;
|
||||
}
|
||||
|
||||
public String invest() {
|
||||
return String.format("Investor %s has invested %d dollars", name, amountToInvest);
|
||||
}
|
||||
}
|
||||
|
@ -20,36 +20,41 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Possible roles
|
||||
* Possible roles.
|
||||
*/
|
||||
public enum Role {
|
||||
Borrower(BorrowerRole.class), Investor(InvestorRole.class);
|
||||
|
||||
private Class<? extends CustomerRole> typeCst;
|
||||
Borrower(BorrowerRole.class), Investor(InvestorRole.class);
|
||||
|
||||
Role(Class<? extends CustomerRole> typeCst) {
|
||||
this.typeCst = typeCst;
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Role.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends CustomerRole> Optional<T> instance() {
|
||||
Class<? extends CustomerRole> typeCst = this.typeCst;
|
||||
try {
|
||||
return (Optional<T>) Optional.of(typeCst.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("error creating an object", e);
|
||||
}
|
||||
return Optional.empty();
|
||||
private Class<? extends CustomerRole> typeCst;
|
||||
|
||||
Role(Class<? extends CustomerRole> typeCst) {
|
||||
this.typeCst = typeCst;
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Role.class);
|
||||
|
||||
/**
|
||||
* Get instance.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends CustomerRole> Optional<T> instance() {
|
||||
Class<? extends CustomerRole> typeCst = this.typeCst;
|
||||
try {
|
||||
return (Optional<T>) Optional.of(typeCst.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("error creating an object", e);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user