Java 11 migrate remaining q-r (#1121)

* Moves queue-load-leveling to Java 11

* Moves reactor to Java 11

* Moves reader-writer-lock to Java 11

* Moves repository to Java 11

* Moves resource-acquisition-is-initialization to Java 11

* Moves retry to Java 11

* Moves role-object to Java 11
This commit is contained in:
Anurag Agarwal
2020-01-04 22:13:12 +05:30
committed by Ilkka Seppälä
parent cd2a2e7711
commit 20ea465b7f
52 changed files with 424 additions and 554 deletions

View File

@ -30,37 +30,31 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Role Object pattern suggests to model context-specific views
* of an object as separate role objects which are
* dynamically attached to and removed from the core object.
* We call the resulting composite object structure,
* consisting of the core and its role objects, a subject.
* A subject often plays several roles and the same role is likely to
* be played by different subjects.
* As an example consider two different customers playing the role of borrower and
* 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.
* The Role Object pattern suggests to model context-specific views of an object as separate role
* objects which are dynamically attached to and removed from the core object. We call the resulting
* composite object structure, consisting of the core and its role objects, a subject. A subject
* often plays several roles and the same role is likely to be played by different subjects. As an
* example consider two different customers playing the role of borrower and 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
* 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.
* It defines additional operations to manage the customers
* credits and securities. Similarly, the {@link InvestorRole} class adds operations specific
* to the investment departments 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, lets 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.
* 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. It defines additional
* operations to manage the customers credits and securities. Similarly, the {@link InvestorRole}
* class adds operations specific to the investment departments 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, lets 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 {
@ -72,13 +66,13 @@ public class ApplicationRoleObject {
* @param args program arguments
*/
public static void main(String[] args) {
Customer customer = Customer.newCustomer(Borrower, Investor);
var customer = Customer.newCustomer(Borrower, Investor);
logger.info(" the new customer created : {}", customer);
boolean hasBorrowerRole = customer.hasRole(Borrower);
var hasBorrowerRole = customer.hasRole(Borrower);
logger.info(" customer has a borrowed role - {}", hasBorrowerRole);
boolean hasInvestorRole = customer.hasRole(Investor);
var hasInvestorRole = customer.hasRole(Investor);
logger.info(" customer has an investor role - {}", hasInvestorRole);
customer.getRole(Investor, InvestorRole.class)

View File

@ -23,6 +23,7 @@
package com.iluwatar.roleobject;
import java.util.Arrays;
import java.util.Optional;
/**
@ -32,6 +33,7 @@ public abstract class Customer {
/**
* Add specific role @see {@link Role}.
*
* @param role to add
* @return true if the operation has been successful otherwise false
*/
@ -39,6 +41,7 @@ public abstract class Customer {
/**
* Check specific role @see {@link Role}.
*
* @param role to check
* @return true if the role exists otherwise false
*/
@ -47,6 +50,7 @@ public abstract class Customer {
/**
* Remove specific role @see {@link Role}.
*
* @param role to remove
* @return true if the operation has been successful otherwise false
*/
@ -54,6 +58,7 @@ public abstract class Customer {
/**
* 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
@ -67,14 +72,13 @@ public abstract class Customer {
/**
* 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);
}
var customer = newCustomer();
Arrays.stream(role).forEach(customer::addRole);
return customer;
}

View File

@ -73,7 +73,7 @@ public class CustomerCore extends Customer {
@Override
public String toString() {
String roles = Arrays.toString(this.roles.keySet().toArray());
var roles = Arrays.toString(this.roles.keySet().toArray());
return "Customer{roles=" + roles + "}";
}
}

View File

@ -26,5 +26,5 @@ package com.iluwatar.roleobject;
/**
* Key abstraction for segregated roles.
*/
public abstract class CustomerRole extends CustomerCore{
public abstract class CustomerRole extends CustomerCore {
}

View File

@ -24,7 +24,6 @@
package com.iluwatar.roleobject;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -48,7 +47,7 @@ public enum Role {
*/
@SuppressWarnings("unchecked")
public <T extends CustomerRole> Optional<T> instance() {
Class<? extends CustomerRole> typeCst = this.typeCst;
var typeCst = this.typeCst;
try {
return (Optional<T>) Optional.of(typeCst.newInstance());
} catch (InstantiationException | IllegalAccessException e) {

View File

@ -26,8 +26,8 @@ import org.junit.Test;
public class ApplicationRoleObjectTest {
@Test
public void mainTest() {
ApplicationRoleObject.main(new String[]{});
}
@Test
public void mainTest() {
ApplicationRoleObject.main(new String[]{});
}
}

View File

@ -25,16 +25,12 @@ package com.iluwatar.roleobject;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
public class BorrowerRoleTest {
@Test
public void borrowTest() {
BorrowerRole borrowerRole = new BorrowerRole();
borrowerRole.setName("test");
String res = "Borrower test wants to get some money.";
Assert.assertEquals(borrowerRole.borrow(),res);
}
@Test
public void borrowTest() {
var borrowerRole = new BorrowerRole();
borrowerRole.setName("test");
Assert.assertEquals(borrowerRole.borrow(), "Borrower test wants to get some money.");
}
}

View File

@ -22,82 +22,72 @@
*/
package com.iluwatar.roleobject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.*;
public class CustomerCoreTest {
@Test
public void addRole() {
CustomerCore core = new CustomerCore();
boolean add = core.addRole(Role.Borrower);
assertTrue(add);
@Test
public void addRole() {
var core = new CustomerCore();
assertTrue(core.addRole(Role.Borrower));
}
}
@Test
public void hasRole() {
var core = new CustomerCore();
core.addRole(Role.Borrower);
assertTrue(core.hasRole(Role.Borrower));
assertFalse(core.hasRole(Role.Investor));
}
@Test
public void hasRole() {
CustomerCore core = new CustomerCore();
core.addRole(Role.Borrower);
@Test
public void remRole() {
var core = new CustomerCore();
core.addRole(Role.Borrower);
boolean has = core.hasRole(Role.Borrower);
assertTrue(has);
var bRole = core.getRole(Role.Borrower, BorrowerRole.class);
assertTrue(bRole.isPresent());
boolean notHas = core.hasRole(Role.Investor);
assertFalse(notHas);
}
assertTrue(core.remRole(Role.Borrower));
@Test
public void remRole() {
CustomerCore core = new CustomerCore();
core.addRole(Role.Borrower);
var empt = core.getRole(Role.Borrower, BorrowerRole.class);
assertFalse(empt.isPresent());
}
Optional<BorrowerRole> bRole = core.getRole(Role.Borrower, BorrowerRole.class);
assertTrue(bRole.isPresent());
@Test
public void getRole() {
var core = new CustomerCore();
core.addRole(Role.Borrower);
boolean res = core.remRole(Role.Borrower);
assertTrue(res);
var bRole = core.getRole(Role.Borrower, BorrowerRole.class);
assertTrue(bRole.isPresent());
Optional<BorrowerRole> empt = core.getRole(Role.Borrower, BorrowerRole.class);
assertFalse(empt.isPresent());
var nonRole = core.getRole(Role.Borrower, InvestorRole.class);
assertFalse(nonRole.isPresent());
}
@Test
public void getRole() {
CustomerCore core = new CustomerCore();
core.addRole(Role.Borrower);
Optional<BorrowerRole> bRole = core.getRole(Role.Borrower, BorrowerRole.class);
assertTrue(bRole.isPresent());
Optional<InvestorRole> nonRole = core.getRole(Role.Borrower, InvestorRole.class);
assertFalse(nonRole.isPresent());
Optional<InvestorRole> invRole = core.getRole(Role.Investor, InvestorRole.class);
assertFalse(invRole.isPresent());
var invRole = core.getRole(Role.Investor, InvestorRole.class);
assertFalse(invRole.isPresent());
}
}
@Test
public void toStringTest() {
var core = new CustomerCore();
core.addRole(Role.Borrower);
assertEquals(core.toString(), "Customer{roles=[Borrower]}");
core = new CustomerCore();
core.addRole(Role.Investor);
assertEquals(core.toString(), "Customer{roles=[Investor]}");
core = new CustomerCore();
assertEquals(core.toString(), "Customer{roles=[]}");
@Test
public void toStringTest() {
CustomerCore core = new CustomerCore();
core.addRole(Role.Borrower);
assertEquals(core.toString(), "Customer{roles=[Borrower]}");
core = new CustomerCore();
core.addRole(Role.Investor);
assertEquals(core.toString(), "Customer{roles=[Investor]}");
core = new CustomerCore();
assertEquals(core.toString(), "Customer{roles=[]}");
}
}
}

View File

@ -27,12 +27,11 @@ import org.junit.Test;
public class InvestorRoleTest {
@Test
public void investTest() {
InvestorRole investorRole = new InvestorRole();
investorRole.setName("test");
investorRole.setAmountToInvest(10);
String res = "Investor test has invested 10 dollars";
Assert.assertEquals(investorRole.invest(), res);
}
@Test
public void investTest() {
var investorRole = new InvestorRole();
investorRole.setName("test");
investorRole.setAmountToInvest(10);
Assert.assertEquals(investorRole.invest(), "Investor test has invested 10 dollars");
}
}

View File

@ -25,16 +25,12 @@ package com.iluwatar.roleobject;
import org.junit.Assert;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.*;
public class RoleTest {
@Test
public void instanceTest() {
Optional<CustomerRole> instance = Role.Borrower.instance();
Assert.assertTrue(instance.isPresent());
Assert.assertEquals(instance.get().getClass(),BorrowerRole.class);
}
@Test
public void instanceTest() {
var instance = Role.Borrower.instance();
Assert.assertTrue(instance.isPresent());
Assert.assertEquals(instance.get().getClass(), BorrowerRole.class);
}
}