|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * Copyright © 2014-2019 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.combinator; |
| 25 | + |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * The functional pattern representing a style of organizing libraries |
| 32 | + * centered around the idea of combining functions. |
| 33 | + * Putting it simply, there is some type T, some functions |
| 34 | + * for constructing "primitive" values of type T, |
| 35 | + * and some "combinators" which can combine values of type T |
| 36 | + * in various ways to build up more complex values of type T. |
| 37 | + * The class {@link Finder} defines a simple function {@link Finder#find(String)} |
| 38 | + * and connected functions |
| 39 | + * {@link Finder#or(Finder)}, |
| 40 | + * {@link Finder#not(Finder)}, |
| 41 | + * {@link Finder#and(Finder)} |
| 42 | + * Using them the became possible to get more complex functions {@link Finders} |
| 43 | + */ |
| 44 | +public class CombinatorApp { |
| 45 | + |
| 46 | + /** |
| 47 | + * Logger. |
| 48 | + */ |
| 49 | + private static final Logger LOGGER = LoggerFactory.getLogger(CombinatorApp.class); |
| 50 | + |
| 51 | + /** |
| 52 | + * main. |
| 53 | + * @param args args |
| 54 | + */ |
| 55 | + public static void main(String[] args) { |
| 56 | + var queriesOr = new String[]{"many", "Annabel"}; |
| 57 | + var finder = Finders.expandedFinder(queriesOr); |
| 58 | + var res = finder.find(text()); |
| 59 | + LOGGER.info("the result of expanded(or) query[{}] is {}", queriesOr, res); |
| 60 | + |
| 61 | + var queriesAnd = new String[]{"Annabel", "my"}; |
| 62 | + finder = Finders.specializedFinder(queriesAnd); |
| 63 | + res = finder.find(text()); |
| 64 | + LOGGER.info("the result of specialized(and) query[{}] is {}", queriesAnd, res); |
| 65 | + |
| 66 | + finder = Finders.advancedFinder("it was","kingdom","sea"); |
| 67 | + res = finder.find(text()); |
| 68 | + LOGGER.info("the result of advanced query is {}", res); |
| 69 | + |
| 70 | + res = Finders.filteredFinder(" was ", "many", "child").find(text()); |
| 71 | + LOGGER.info("the result of filtered query is {}", res); |
| 72 | + |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + private static String text() { |
| 77 | + return |
| 78 | + "It was many and many a year ago,\n" |
| 79 | + + "In a kingdom by the sea,\n" |
| 80 | + + "That a maiden there lived whom you may know\n" |
| 81 | + + "By the name of ANNABEL LEE;\n" |
| 82 | + + "And this maiden she lived with no other thought\n" |
| 83 | + + "Than to love and be loved by me.\n" |
| 84 | + + "I was a child and she was a child,\n" |
| 85 | + + "In this kingdom by the sea;\n" |
| 86 | + + "But we loved with a love that was more than love-\n" |
| 87 | + + "I and my Annabel Lee;\n" |
| 88 | + + "With a love that the winged seraphs of heaven\n" |
| 89 | + + "Coveted her and me."; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments