Skip to content

Commit b2ea831

Browse files
bahtipivovarit
authored andcommitted
inner interface examples added (eugenp#3151)
1 parent 9046955 commit b2ea831

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.interfaces;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class CommaSeparatedCustomers implements Customer.List {
8+
9+
private List<Customer> customers = new ArrayList<Customer>();
10+
11+
@Override
12+
public void Add(Customer customer) {
13+
customers.add(customer);
14+
}
15+
16+
@Override
17+
public String getCustomerNames() {
18+
return customers.stream().map(customer -> customer.getName()).collect(Collectors.joining(","));
19+
}
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.interfaces;
2+
3+
public class Customer {
4+
public interface List {
5+
void Add(Customer customer);
6+
7+
String getCustomerNames();
8+
}
9+
10+
private String name;
11+
12+
public Customer(String name) {
13+
this.name = name;
14+
}
15+
16+
String getName() {
17+
return name;
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.interfaces;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.JUnit4;
8+
9+
@RunWith(JUnit4.class)
10+
public class InnerInterfaceTests {
11+
@Test
12+
public void whenCustomerListJoined_thenReturnsJoinedNames() {
13+
Customer.List customerList = new CommaSeparatedCustomers();
14+
customerList.Add(new Customer("customer1"));
15+
customerList.Add(new Customer("customer2"));
16+
assertEquals("customer1,customer2", customerList.getCustomerNames());
17+
}
18+
}

0 commit comments

Comments
 (0)