Add Wrapper (#179)

* Update copyright year

* Alias: added

* bump release-drafter from 5.11.0 to 5.14.0

* Version set to 2.3.0

* Rename Alias as Wrapper and drop map method

Simplify tests.
This commit is contained in:
Paul Campbell 2021-03-12 20:28:38 +00:00 committed by GitHub
parent 19e3a06ac2
commit a501c506a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 220 additions and 94 deletions

View file

@ -9,6 +9,6 @@ jobs:
update_draft_release:
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5.11.0
- uses: release-drafter/release-drafter@v5.14.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,11 +1,12 @@
* Mon
* TypeAlias, Maybe and Result for Java.
* Alias, TypeAlias, Maybe and Result for Java.
[[https://oss.sonatype.org/content/repositories/releases/net/kemitix/mon][file:https://img.shields.io/nexus/r/https/oss.sonatype.org/net.kemitix/mon.svg?style=for-the-badge]]
[[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22mon%22][file:https://img.shields.io/maven-central/v/net.kemitix/mon.svg?style=for-the-badge]]
[[http://i.jpeek.org/net.kemitix/mon/index.html][file:http://i.jpeek.org/net.kemitix/mon/badge.svg]]
- [Maven Usage]
- [Alias]
- [TypeAlias]
- [Maybe]
- [Result]
@ -26,7 +27,10 @@
The latest version should be shown above with the nexus and maven-central
badges or can be found on [[https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22net.kemitix%22%20AND%20a%3A%22mon%22][Maven Central]].
* TypeAlias
* Wrapper
A simple FunctionalInterface that contains a value. Can be used to implement
a form of type-alias in Java.
In Haskell it is possible to create an alias for a Type, and to then use
that alias with the same behaviour as the original, except that the compiler
@ -39,20 +43,62 @@
newtype PhoneBookEntry = PhoneBookEntry (Name, PhoneNumber)
newtype PhoneBook = PhoneBook [PhoneBookEntry]
#+END_SRC
In Java we don't have the ability to have that true alias, so TypeAlias is
more of a type-wrapper. It's as close as I could get to a Haskell type alias
in Java.
The benefits of using TypeAlias are:
In Java we don't have the ability to have that true alias, so Wrapper simply
wraps the value within a new type. It's as close as I could get to a Haskell
type alias in Java.
The benefits of using Wrapper are:
- encapsulation of the wrapped type when passing references through code
that doesn't need to access the actual value, but only to pass it on
- type-safe parameters where you would otherwise be passing Strings,
Integers, Lists, or other general classes
- equality and hashcode
- less verbose than implementing your own
*Wrapper Example:*
#+BEGIN_SRC java
interface PhoneNumber extends Wrapper<String> {}
PhoneNumber pn = () -> "01234 567890";
String v = pn.value();
#+END_SRC
*Roll your own:*
#+BEGIN_SRC java
class PhoneNumber {
private final String value;
public PhoneNumber(final String value) {
this.value = value;
}
public String value() {
return value;
}
}
#+END_SRC
*Lombok:*
Using Lombok we can achieve it in 8 lines, compared to 24 for rolling your
own, or 1 for Alias:
#+BEGIN_SRC java
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
class PhoneNumber {
private final String value;
public static PhoneNumber of(final String phoneNumber) {
return new PhoneNumber(phoneNumber);
}
}
#+END_SRC
* TypeAlias
Note: this is a precursor to `Wrapper` and should be considered deprecated.
*TypeAlias Example:*
#+BEGIN_SRC java
@ -66,51 +112,6 @@
}
#+END_SRC
*Roll your own:*
#+BEGIN_SRC java
class PhoneNumber {
private final String value;
private PhoneNumber(final String value) {
this.value = value;
}
public static PhoneNumber of(final String phoneNumber) {
return new PhoneNumber(phoneNumber);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PhoneNumber that = (PhoneNumber) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
public String getValue() {
return value;
}
}
#+END_SRC
*Lombok:*
Although, if you are using Lombok, that can be equally terse, both it and
TypeAlias<String> coming in at 8 lines each, compared to 24 for rolling your
own:
#+BEGIN_SRC java
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
class PhoneNumber {
private final String value;
public static PhoneNumber of(final String phoneNumber) {
return new PhoneNumber(phoneNumber);
}
}
#+END_SRC
** =TypeAlias= *can* be a Monad
#+BEGIN_SRC java

View file

@ -11,7 +11,7 @@
</parent>
<artifactId>mon</artifactId>
<version>2.2.1-SNAPSHOT</version>
<version>2.3.0</version>
<name>Mon</name>
<description>TypeAlias, Result and Maybe for Java</description>

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -0,0 +1,42 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2021 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;
/**
* A wrapper for other types.
*
* <p>Can be used as a form of type-alias, where the value is held in the wrapper.</p>
*
* @param <T> the type of the wrapper's value
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@FunctionalInterface
public interface Wrapper<T> {
/**
* The wrapped value.
*
* @return the value
*/
public abstract T value();
}

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
* Copyright (c) 2021 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,

View file

@ -0,0 +1,83 @@
package net.kemitix.mon;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class WrapperTest {
interface WrappedString extends Wrapper<String> {}
interface WrappedInteger extends Wrapper<Integer> {}
interface WrappedIterableString extends Wrapper<Iterable<String>> { }
interface WrappedListInteger extends Wrapper<List<Integer>> {}
interface AWrapper extends Wrapper<String> {}
@Test
void shouldCreateAWrapperAndGetTheValue() {
//given
final String value = "value";
//when
// - anonymous class syntax
final WrappedString wrappedString = new WrappedString() {
@Override
public String value() {
return value;
}
};
//then
assertThat(wrappedString.value()).isSameAs(value);
}
@Test
void shouldCreateAWrapperWithNestedGenericTypes() {
//given
final Iterable<String> iterable = Collections.emptyList();
//when
// - functional interface / lambda syntax
final WrappedIterableString wrappedIterableString = () -> iterable;
//then
assertThat(wrappedIterableString.value()).isSameAs(iterable);
}
@Test
void shouldCreateAWrapperSubclassAndGetTheValue() {
//given
final String value = "value";
//when
final AWrapper aWrapper = () -> value;
//then
assertThat(aWrapper.value()).isSameAs(value);
}
@Test
void shouldNotBeEqualWhenValueTypesAreDifferent() {
//given
final WrappedString wrappedString = () -> "1";
final WrappedInteger wrappedInteger = () -> 1;
//then
assertThat(wrappedString).isNotEqualTo(wrappedInteger);
}
@Test
void shouldNotBeEqualEvenWhenValuesAreTheSame() {
//given
final String value = "value";
final AWrapper aWrapper1 = () -> value;
final AWrapper aWrapper2 = () -> value;
//then
assertThat(aWrapper1).isNotEqualTo(aWrapper2);
// instead compare `.value()`s
assertThat(aWrapper1.value()).isEqualTo(aWrapper2.value());
}
}