BAEL-5819: java collections pecs
This commit is contained in:
parent
d54d4768c0
commit
d804060040
|
@ -0,0 +1,59 @@
|
||||||
|
package com.baeldung.collections.pecs;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.baeldung.collections.pecs.model.Customer;
|
||||||
|
import com.baeldung.collections.pecs.model.Operator;
|
||||||
|
import com.baeldung.collections.pecs.model.User;
|
||||||
|
|
||||||
|
public class ProducerExtendsConsumerSupers {
|
||||||
|
|
||||||
|
public void producerExtends() {
|
||||||
|
List<Operator> operators = Arrays.asList(new Operator("sam"), new Operator("daniel"));
|
||||||
|
List<Customer> customers = Arrays.asList(new Customer("arys"), new Customer("cristiana"));
|
||||||
|
|
||||||
|
// sendEmails(operators); --> will not compile!
|
||||||
|
sendEmailsFixed(operators);
|
||||||
|
sendEmailsFixed(customers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendEmails(List<User> users) {
|
||||||
|
for (User user : users) {
|
||||||
|
System.out.println("sending email to " + user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendEmailsFixed(List<? extends User> users) {
|
||||||
|
for (User user : users) {
|
||||||
|
System.out.println("sending email to " + user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void consumerSupers() {
|
||||||
|
List<Operator> allOperators = Arrays.asList(new Operator("tom"));
|
||||||
|
List<User> allUsers = Arrays.asList(new Operator("tom"), new Customer("spencer"));
|
||||||
|
|
||||||
|
// addUsersFromMarketingDepartment(allUsers); --> will not compile!
|
||||||
|
addUsersFromMarketingDepartmentFixed(allOperators);
|
||||||
|
addUsersFromMarketingDepartmentFixed(allUsers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addUsersFromMarketingDepartment(List<Operator> users) {
|
||||||
|
users.add(new Operator("john doe"));
|
||||||
|
users.add(new Operator("jane doe"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addUsersFromMarketingDepartmentFixed(List<? super Operator> users) {
|
||||||
|
users.add(new Operator("john doe"));
|
||||||
|
users.add(new Operator("jane doe"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addUsersAndSendEmails(List<User> users) {
|
||||||
|
users.add(new Operator("john doe"));
|
||||||
|
for (User user : users) {
|
||||||
|
System.out.println("sending email to: " + user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.collections.pecs.model;
|
||||||
|
|
||||||
|
public class Customer extends User {
|
||||||
|
public Customer(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.collections.pecs.model;
|
||||||
|
|
||||||
|
public class Operator extends User {
|
||||||
|
public Operator(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.collections.pecs.model;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public User(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue