Skip to content

Commit 8ef2d18

Browse files
committed
Implemented restriction operators.
1 parent bfd9bba commit 8ef2d18

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* RestrictionOperators.java Feb 2 2014, 03:01
3+
*
4+
* Copyright 2014 Drunken Dev.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.drunkendev.lambdas;
20+
21+
import com.drunkendev.lambdas.domain.DomainService;
22+
import com.drunkendev.lambdas.helper.IndexHolder;
23+
import java.math.BigDecimal;
24+
import java.time.format.DateTimeFormatter;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
28+
29+
/**
30+
*
31+
* @author Brett Ryan
32+
*/
33+
public class RestrictionOperators {
34+
35+
private final DomainService ds;
36+
37+
/**
38+
* Creates a new {@code RestrictionOperators} instance.
39+
*/
40+
public RestrictionOperators() {
41+
this.ds = new DomainService();
42+
}
43+
44+
public static void main(String[] args) {
45+
RestrictionOperators ro = new RestrictionOperators();
46+
ro.lambda1();
47+
ro.lambda2();
48+
ro.lambda3();
49+
ro.lambda4();
50+
ro.lambda5();
51+
}
52+
53+
/**
54+
* This sample uses a filter to find all elements of a list with a value
55+
* less than 5.
56+
*/
57+
public void lambda1() {
58+
System.out.println("\nNumbers < 5:");
59+
List<Integer> numbers = Arrays.asList(5, 4, 1, 3, 9, 8, 6, 7, 2, 0);
60+
numbers.stream()
61+
.filter(n -> n < 5)
62+
.forEach(System.out::println);
63+
}
64+
65+
/**
66+
* This sample uses a filter to find all products that are out of stock.
67+
*/
68+
public void lambda2() {
69+
System.out.println("\nSold out products:");
70+
ds.getProductList().stream()
71+
.filter(x -> x.getUnitsInStock() == 0)
72+
.forEach(x -> {
73+
System.out.println(String.format("%s is sold out!",
74+
x.getProductName()));
75+
});
76+
}
77+
78+
/**
79+
* This sample uses a filter to find all products that are in stock and cost
80+
* more than 3.00 per unit.
81+
*/
82+
public void lambda3() {
83+
System.out.println("\nIn-stock products that cost more than 3.00:");
84+
ds.getProductList().stream()
85+
.filter(x -> x.getUnitsInStock() > 0)
86+
.filter(x -> x.getUnitPrice().compareTo(BigDecimal.valueOf(3)) > 0)
87+
.forEach(x -> {
88+
System.out.println(String.format(
89+
"%s is in stock and costs more than 3.00.",
90+
x.getProductName()));
91+
});
92+
}
93+
94+
/**
95+
* This sample uses a filter to find all customers in Washington and then it
96+
* uses a forEach to iterate over the orders collection that belongs to each
97+
* customer.
98+
*
99+
*/
100+
public void lambda4() {
101+
System.out.println("\nCustomers from Washington and their orders:");
102+
ds.getCustomerList().stream()
103+
.filter(c -> "WA".equalsIgnoreCase(c.getRegion()))
104+
.forEach(c -> {
105+
System.out.println(String.format("Customer %s: %s", c.getCustomerId(), c.getCompanyName()));
106+
c.getOrders().getOrders().stream().forEach(o -> {
107+
System.out.println(String.format(
108+
" Order %d: %s",
109+
o.getOrderId(), o.getOrderDate().format(DateTimeFormatter.ISO_DATE)));
110+
111+
});
112+
});
113+
}
114+
115+
/**
116+
* This sample demonstrates an indexed filter that returns digits whose name is
117+
* shorter than their value.
118+
119+
* <strong>NOTE</strong>: Needs full conversion.
120+
*/
121+
public void lambda5() {
122+
System.out.println("\nShort digits:");
123+
List<String> digits = Arrays.asList("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
124+
IndexHolder i = new IndexHolder();
125+
digits.stream()
126+
.filter(x -> x.length() < i.postIncrement())
127+
.forEach(x -> System.out.println(x));
128+
}
129+
130+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* IndexHolder.java Feb 2 2014, 03:34
3+
*
4+
* Copyright 2014 Drunken Dev.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.drunkendev.lambdas.helper;
20+
21+
22+
/**
23+
* Represents an index to allow for incrementing/decrementing within a lambda
24+
* context as only (effective) final variables may be altered.
25+
*
26+
* @author Brett Ryan
27+
*/
28+
public class IndexHolder {
29+
30+
private int index;
31+
32+
/**
33+
* Creates a new {@code IndexHolder} instance.
34+
*/
35+
public IndexHolder() {
36+
}
37+
38+
public int getIndex() {
39+
return index;
40+
}
41+
42+
public int preIncrement() {
43+
return ++index;
44+
}
45+
46+
public int postIncrement() {
47+
return index++;
48+
}
49+
50+
public int preDecrement() {
51+
return --index;
52+
}
53+
54+
public int postDecrement() {
55+
return index--;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)