|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright 2015 Valentyn Kolesnikov |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.github.underscore; |
| 25 | + |
| 26 | +import java.lang.reflect.Method; |
| 27 | +import java.util.*; |
| 28 | +import org.junit.Test; |
| 29 | +import static java.util.Arrays.asList; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | + |
| 32 | +/** |
| 33 | + * Underscore library unit test. |
| 34 | + * |
| 35 | + * @author Valentyn Kolesnikov |
| 36 | + */ |
| 37 | +public class ChainingTest { |
| 38 | + |
| 39 | +/* |
| 40 | +var stooges = [{name: 'curly', age: 25}, {name: 'moe', age: 21}, {name: 'larry', age: 23}]; |
| 41 | +var youngest = _.chain(stooges) |
| 42 | + .sortBy(function(stooge){ return stooge.age; }) |
| 43 | + .map(function(stooge){ return stooge.name + ' is ' + stooge.age; }) |
| 44 | + .first() |
| 45 | + .value(); |
| 46 | +=> "moe is 21" |
| 47 | +*/ |
| 48 | + @Test |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + public void chain() { |
| 51 | + final List<Map<String, Object>> stooges = new ArrayList<Map<String, Object>>() { { |
| 52 | + add(new LinkedHashMap<String, Object>() { { put("name", "curly"); put("age", 25); } }); |
| 53 | + add(new LinkedHashMap<String, Object>() { { put("name", "moe"); put("age", 21); } }); |
| 54 | + add(new LinkedHashMap<String, Object>() { { put("name", "larry"); put("age", 23); } }); |
| 55 | + } }; |
| 56 | + final String youngest = $.chain(stooges) |
| 57 | + .sortBy((item) -> { |
| 58 | + return (Integer) ((Map<String, Object>) item).get("age"); |
| 59 | + } |
| 60 | + ) |
| 61 | + .map((item) -> { |
| 62 | + return item.get("name") + " is " + item.get("age"); |
| 63 | + } |
| 64 | + ) |
| 65 | + .first().item(); |
| 66 | + assertEquals("moe is 21", youngest); |
| 67 | + } |
| 68 | +} |
0 commit comments