|
| 1 | +/* |
| 2 | + * PartitioningOperators.java Feb 3 2014, 03:44 |
| 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.domain.Order; |
| 23 | +import com.drunkendev.lambdas.helper.IndexHolder; |
| 24 | +import com.drunkendev.lambdas.helper.MutableBoolean; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * |
| 31 | + * @author Brett Ryan |
| 32 | + */ |
| 33 | +public class PartitioningOperators { |
| 34 | + |
| 35 | + private final DomainService ds; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new {@code PartitioningOperators} instance. |
| 39 | + */ |
| 40 | + public PartitioningOperators() { |
| 41 | + this.ds = new DomainService(); |
| 42 | + } |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + PartitioningOperators po = new PartitioningOperators(); |
| 46 | + po.lambda20(); |
| 47 | + po.lambda21(); |
| 48 | + po.lambda22(); |
| 49 | + po.lambda23(); |
| 50 | + po.lambda24(); |
| 51 | + po.lambda25(); |
| 52 | + po.lambda26(); |
| 53 | + po.lambda27(); |
| 54 | + } |
| 55 | + |
| 56 | + public void lambda20() { |
| 57 | + System.out.println("\nFirst 3 numbers:"); |
| 58 | + int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; |
| 59 | + Arrays.stream(numbers) |
| 60 | + .limit(3) |
| 61 | + .forEach(System.out::println); |
| 62 | + } |
| 63 | + |
| 64 | + public void lambda21() { |
| 65 | + System.out.println("\nFirst 3 orders in WA:"); |
| 66 | + ds.getCustomerList().stream() |
| 67 | + .filter(c -> "WA".equalsIgnoreCase(c.getRegion())) |
| 68 | + .flatMap(c -> c.getOrders().stream() |
| 69 | + .map(n -> new CustOrder(c.getCustomerId(), n)) |
| 70 | + ).limit(3) |
| 71 | + .forEach(System.out::println); |
| 72 | + } |
| 73 | + |
| 74 | + public void lambda22() { |
| 75 | + System.out.println("\nAll but first 4 numbers:"); |
| 76 | + int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; |
| 77 | + Arrays.stream(numbers) |
| 78 | + .skip(4) |
| 79 | + .forEach(System.out::println); |
| 80 | + } |
| 81 | + |
| 82 | + public void lambda23() { |
| 83 | + System.out.println("\nAll but first 2 orders in WA:"); |
| 84 | + ds.getCustomerList().stream() |
| 85 | + .filter(c -> "WA".equalsIgnoreCase(c.getRegion())) |
| 86 | + .flatMap(c -> c.getOrders().stream() |
| 87 | + .map(n -> new CustOrder(c.getCustomerId(), n)) |
| 88 | + ).skip(2) |
| 89 | + .forEach(System.out::println); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Unfortunately this method will not short circuit and will continue to |
| 94 | + * iterate until the end of the stream. I need to figure out a better way to |
| 95 | + * handle this. |
| 96 | + */ |
| 97 | + public void lambda24() { |
| 98 | + System.out.println("\nFirst numbers less than 6:"); |
| 99 | + int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; |
| 100 | + MutableBoolean mb = new MutableBoolean(true); |
| 101 | + Arrays.stream(numbers) |
| 102 | + .collect(ArrayList<Integer>::new, |
| 103 | + (output, v) -> { |
| 104 | + if (mb.isTrue()) { |
| 105 | + if (v < 6) { |
| 106 | + output.add(v); |
| 107 | + } else { |
| 108 | + mb.flip(); |
| 109 | + } |
| 110 | + } |
| 111 | + }, |
| 112 | + (c1, c2) -> c1.addAll(c2)) |
| 113 | + .forEach(System.out::println); |
| 114 | + } |
| 115 | + |
| 116 | + public void lambda25() { |
| 117 | + System.out.println("\nFirst numbers not less than their position:"); |
| 118 | + int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; |
| 119 | + IndexHolder i = new IndexHolder(); |
| 120 | + MutableBoolean mb = new MutableBoolean(true); |
| 121 | + Arrays.stream(numbers) |
| 122 | + .collect(ArrayList<Integer>::new, |
| 123 | + (output, v) -> { |
| 124 | + if (mb.isTrue()) { |
| 125 | + if (v > i.postIncrement()) { |
| 126 | + output.add(v); |
| 127 | + } else { |
| 128 | + mb.flip(); |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | + (c1, c2) -> c1.addAll(c2)) |
| 133 | + .forEach(System.out::println); |
| 134 | + } |
| 135 | + |
| 136 | + public void lambda26() { |
| 137 | + System.out.println("\nAll elements starting from first element divisible by 3:"); |
| 138 | + int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; |
| 139 | + MutableBoolean mb = new MutableBoolean(false); |
| 140 | + Arrays.stream(numbers) |
| 141 | + .collect(ArrayList<Integer>::new, |
| 142 | + (output, v) -> { |
| 143 | + if (mb.isTrue()) { |
| 144 | + output.add(v); |
| 145 | + } else if (v % 3 == 0) { |
| 146 | + output.add(v); |
| 147 | + mb.flip(); |
| 148 | + } |
| 149 | + }, |
| 150 | + (c1, c2) -> c1.addAll(c2)) |
| 151 | + .forEach(System.out::println); |
| 152 | + } |
| 153 | + |
| 154 | + public void lambda27() { |
| 155 | + System.out.println("\nAll elements starting from first element less than its position:"); |
| 156 | + int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; |
| 157 | + IndexHolder i = new IndexHolder(); |
| 158 | + MutableBoolean mb = new MutableBoolean(false); |
| 159 | + Arrays.stream(numbers) |
| 160 | + .collect(ArrayList<Integer>::new, |
| 161 | + (output, v) -> { |
| 162 | + if (mb.isTrue()) { |
| 163 | + output.add(v); |
| 164 | + } else if (v < i.postIncrement()) { |
| 165 | + output.add(v); |
| 166 | + mb.flip(); |
| 167 | + } |
| 168 | + }, |
| 169 | + (c1, c2) -> c1.addAll(c2) |
| 170 | + ) |
| 171 | + .forEach(System.out::println); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + private static class CustOrder { |
| 176 | + |
| 177 | + private final String customerId; |
| 178 | + private final Order order; |
| 179 | + |
| 180 | + public CustOrder(String customerId, Order order) { |
| 181 | + this.customerId = customerId; |
| 182 | + this.order = order; |
| 183 | + } |
| 184 | + |
| 185 | + public String getCustomerId() { |
| 186 | + return customerId; |
| 187 | + } |
| 188 | + |
| 189 | + public Order getOrder() { |
| 190 | + return order; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public String toString() { |
| 195 | + return String.format("CustOrder[customerId=%s,orderId=%d,orderDate=%s]", |
| 196 | + customerId, order.getOrderId(), order.getOrderDate()); |
| 197 | + } |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments