From a14d87fd18818e0583376ec823220a5310afab2d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Sep 2017 20:31:28 +0100 Subject: [PATCH] Identity implements Functor --- src/main/java/net/kemitix/mon/Identity.java | 45 +++++++++++++++++++ src/test/java/net/kemitix/mon/Address.java | 39 ++++++++++++++++ src/test/java/net/kemitix/mon/Customer.java | 40 +++++++++++++++++ .../java/net/kemitix/mon/IdentityTest.java | 37 +++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 src/main/java/net/kemitix/mon/Identity.java create mode 100644 src/test/java/net/kemitix/mon/Address.java create mode 100644 src/test/java/net/kemitix/mon/Customer.java create mode 100644 src/test/java/net/kemitix/mon/IdentityTest.java diff --git a/src/main/java/net/kemitix/mon/Identity.java b/src/main/java/net/kemitix/mon/Identity.java new file mode 100644 index 0000000..1300d7d --- /dev/null +++ b/src/main/java/net/kemitix/mon/Identity.java @@ -0,0 +1,45 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon; + +import lombok.RequiredArgsConstructor; + +import java.util.function.Function; + +/** + * Identity. + * + * @param the type of the identity content + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +@RequiredArgsConstructor +class Identity implements Functor { + + private final T value; + + @Override + public Identity map(final Function f) { + return new Identity<>(f.apply(value)); + } + +} diff --git a/src/test/java/net/kemitix/mon/Address.java b/src/test/java/net/kemitix/mon/Address.java new file mode 100644 index 0000000..8bfbd37 --- /dev/null +++ b/src/test/java/net/kemitix/mon/Address.java @@ -0,0 +1,39 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon; + +import lombok.RequiredArgsConstructor; + +/** + * . + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +@RequiredArgsConstructor +class Address { + + private final String street; + + String getStreet() { + return street; + } +} diff --git a/src/test/java/net/kemitix/mon/Customer.java b/src/test/java/net/kemitix/mon/Customer.java new file mode 100644 index 0000000..594abd7 --- /dev/null +++ b/src/test/java/net/kemitix/mon/Customer.java @@ -0,0 +1,40 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.mon; + +import lombok.RequiredArgsConstructor; + +/** + * . + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +@RequiredArgsConstructor +class Customer { + + private final Address address; + + Address getAddress() { + return address; + } + +} diff --git a/src/test/java/net/kemitix/mon/IdentityTest.java b/src/test/java/net/kemitix/mon/IdentityTest.java new file mode 100644 index 0000000..c93a2e9 --- /dev/null +++ b/src/test/java/net/kemitix/mon/IdentityTest.java @@ -0,0 +1,37 @@ +package net.kemitix.mon; + +import org.assertj.core.api.WithAssertions; +import org.junit.Test; + +/** + * Tests for {@link Identity}. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class IdentityTest implements WithAssertions { + + @Test + public void canMapIdentityFromStringToInteger() { + //given + final Identity idString = new Identity<>("abc"); + //when + final Identity idInt = idString.map(String::length); + //then + idInt.map(id -> assertThat(id).isEqualTo(3)); + } + + @Test + public void canFluentlyComposeFunctions() { + //given + final Customer customer = new Customer(new Address("Park Place")); + //when + final Identity idBytes = new Identity<>(customer).map(Customer::getAddress) + .map(Address::getStreet) + .map((String s) -> s.substring(0, 3)) + .map(String::toLowerCase) + .map(String::getBytes); + //then + idBytes.map(bytes -> assertThat(bytes).isEqualTo("par".getBytes())); + } + +}