-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBook.java
More file actions
55 lines (47 loc) · 1.56 KB
/
Copy pathBook.java
File metadata and controls
55 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package library.streamapi.example8;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Created by orifjon9 on 3/18/2017.
*/
@SuppressWarnings("unused")
public class Book {
private List<BookCopy> copies = new ArrayList<BookCopy>();
private String title;
private Function<List<BookCopy> ,Stream<BookCopy>> bookCopies = (copies) -> copies.stream()
.filter(f->f.isAvailable());
/*
BiFunction<List<BookCopy>,BookCopy, List<BookCopy>> combiner = (x,y) -> {
x.add(y);
return x;
};
private Function<List<BookCopy> ,List<BookCopy>> bookCopies = (copies) -> copies.stream()
.filter(f->f.isAvailable())
.reduce(new ArrayList<BookCopy>(), (x, y) -> {
List<BookCopy> list = new ArrayList<BookCopy>(x);
list.add(y);
return list;
});
*/
public Book(String title, int numCopies) {
if(numCopies < 1) throw new IllegalArgumentException(
"NumCopies must be positive");
this.title= title;
for(int i = 0; i < numCopies; ++i) {
addCopy();
}
}
public List<BookCopy> getCopies() {
return copies;
}
public void addCopy() {
BookCopy copy = new BookCopy(this, copies.size() + 1, true);
copies.add(copy);
}
public boolean isAvailable() {
//return bookCopies.apply(copies).count() > 0;
return copies.stream().map(m -> m.isAvailable()).reduce(false, (x, y) -> x || y);
}
}