|
| 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 | +} |
0 commit comments