inner interface examples added (#3151)

This commit is contained in:
bahti 2017-12-11 20:31:52 +03:00 committed by Grzegorz Piwowarek
parent 9046955197
commit b2ea831d83
3 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.interfaces;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CommaSeparatedCustomers implements Customer.List {
private List<Customer> customers = new ArrayList<Customer>();
@Override
public void Add(Customer customer) {
customers.add(customer);
}
@Override
public String getCustomerNames() {
return customers.stream().map(customer -> customer.getName()).collect(Collectors.joining(","));
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.interfaces;
public class Customer {
public interface List {
void Add(Customer customer);
String getCustomerNames();
}
private String name;
public Customer(String name) {
this.name = name;
}
String getName() {
return name;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.interfaces;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class InnerInterfaceTests {
@Test
public void whenCustomerListJoined_thenReturnsJoinedNames() {
Customer.List customerList = new CommaSeparatedCustomers();
customerList.Add(new Customer("customer1"));
customerList.Add(new Customer("customer2"));
assertEquals("customer1,customer2", customerList.getCustomerNames());
}
}