From 991dfda653bef04ad34de0df67d7456c00af1a7e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 16 Sep 2017 21:59:19 +0100 Subject: [PATCH] TypeAlias --- src/main/java/net/kemitix/mon/TypeAlias.java | 75 +++++++++++++++++ .../java/net/kemitix/mon/TypeAliasTest.java | 81 +++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/main/java/net/kemitix/mon/TypeAlias.java create mode 100644 src/test/java/net/kemitix/mon/TypeAliasTest.java diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java new file mode 100644 index 0000000..2dd5e6e --- /dev/null +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -0,0 +1,75 @@ +/** + * 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; + +/** + * Type Alias for other types. + * + * @param the type of the alias + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +@SuppressWarnings("abstractclassname") +public abstract class TypeAlias { + + /** + * The value. + */ + private final T value; + + /** + * Constructor. + * + * @param value the value + */ + protected TypeAlias(final T value) { + this.value = value; + } + + @Override + public final int hashCode() { + return value.hashCode(); + } + + @Override + public final boolean equals(final Object o) { + if (o instanceof TypeAlias) { + return value.equals(((TypeAlias) o).value); + } + return value.equals(o); + } + + @Override + public final String toString() { + return value.toString(); + } + + /** + * Get the value of the type alias. + * + * @return the value + */ + public final T getValue() { + return value; + } + +} diff --git a/src/test/java/net/kemitix/mon/TypeAliasTest.java b/src/test/java/net/kemitix/mon/TypeAliasTest.java new file mode 100644 index 0000000..91687c3 --- /dev/null +++ b/src/test/java/net/kemitix/mon/TypeAliasTest.java @@ -0,0 +1,81 @@ +package net.kemitix.mon; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TypeAliasTest { + + @Test + public void shouldCreateATypeAliasAndGetTheValue() throws Exception { + //given + final String value = "value"; + //when + final TypeAlias typeAlias = new TypeAlias(value) { + }; + //then + assertThat(typeAlias.getValue()).isEqualTo(value); + } + + @Test + public void shouldCreateAnAliasedTypeAndGetTheValue() throws Exception { + //given + final String value = "value"; + //when + final AnAlias anAlias = AnAlias.of(value); + //then + assertThat(anAlias.getValue()).isEqualTo(value); + } + + @Test + public void shouldBeEqualWhenValuesAreTheSame() throws Exception { + //given + final String value = "value"; + final AnAlias anAlias1 = AnAlias.of(value); + final AnAlias anAlias2 = AnAlias.of(value); + //then + assertThat(anAlias1).isEqualTo(anAlias2); + } + + @Test + public void shouldBeEqualToUnAliasedValue() throws Exception { + //given + final String value = "value"; + final AnAlias anAlias = AnAlias.of(value); + //then + assertThat(anAlias).isEqualTo(value); + } + + @Test + public void shouldHaveHashCodeOfValue() throws Exception { + //given + final String value = "value"; + final AnAlias anAlias = AnAlias.of(value); + //then + assertThat(anAlias.hashCode()).isEqualTo(value.hashCode()); + } + + @Test + public void shouldHaveSameToStringAsAliasedType() throws Exception { + //given + final String value = "value"; + final AnAlias anAlias = AnAlias.of(value); + assertThat(anAlias.toString()).isEqualTo(value.toString()); + } + + private static class AnAlias extends TypeAlias { + + /** + * Constructor. + * + * @param value the value + */ + protected AnAlias(final String value) { + super(value); + } + + static AnAlias of(final String value) { + return new AnAlias(value); + } + } +}