Add generic product types

This commit is contained in:
Alexander Söderberg 2020-09-27 16:18:37 +02:00
parent 7036beb8ad
commit 0064093dbf
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
7 changed files with 896 additions and 0 deletions

View file

@ -0,0 +1,107 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.types.tuples;
import com.google.common.base.Objects;
import javax.annotation.Nonnull;
/**
* Immutable generic 2-tuple
*
* @param <U> First type
* @param <V> Second type
*/
public class Pair<U, V> {
@Nonnull
private final U first;
@Nonnull
private final V second;
protected Pair(@Nonnull final U first,
@Nonnull final V second) {
this.first = first;
this.second = second;
}
/**
* Create a new 2-tuple
*
* @param first First value
* @param second Second value
* @param <U> First type
* @param <V> Second type
* @return Created pair
*/
@Nonnull
public static <U, V> Pair<U, V> of(@Nonnull final U first,
@Nonnull final V second) {
return new Pair<>(first, second);
}
/**
* Get the first value
*
* @return First value
*/
@Nonnull
public final U getFirst() {
return this.first;
}
/**
* Get the second value
*
* @return Second value
*/
@Nonnull
public final V getSecond() {
return this.second;
}
@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equal(getFirst(), pair.getFirst())
&& Objects.equal(getSecond(), pair.getSecond());
}
@Override
public final int hashCode() {
return Objects.hashCode(getFirst(), getSecond());
}
@Override
public final String toString() {
return String.format("(%s, %s)", this.first, this.second);
}
}

View file

@ -0,0 +1,145 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.types.tuples;
import com.google.common.base.Objects;
import javax.annotation.Nonnull;
/**
* Immutable generic 5-tuple
*
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
*/
public class Quartet<U, V, W, X> {
@Nonnull
private final U first;
@Nonnull
private final V second;
@Nonnull
private final W third;
@Nonnull
private final X fourth;
protected Quartet(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
}
/**
* Create a new 4-tuple
*
* @param first First value
* @param second Second value
* @param third Third value
* @param fourth Fourth value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @return Created quartet
*/
@Nonnull
public static <U, V, W, X> Quartet<U, V, W, X> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth) {
return new Quartet<>(first, second, third, fourth);
}
/**
* Get the first value
*
* @return First value
*/
@Nonnull
public final U getFirst() {
return this.first;
}
/**
* Get the second value
*
* @return Second value
*/
@Nonnull
public final V getSecond() {
return this.second;
}
/**
* Get the third value
*
* @return Third value
*/
@Nonnull
public final W getThird() {
return this.third;
}
/**
* Get the fourth value
*
* @return Fourth value
*/
@Nonnull
public final X getFourth() {
return this.fourth;
}
@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Quartet<?, ?, ?, ?> quartet = (Quartet<?, ?, ?, ?>) o;
return Objects.equal(getFirst(), quartet.getFirst())
&& Objects.equal(getSecond(), quartet.getSecond())
&& Objects.equal(getThird(), quartet.getThird())
&& Objects.equal(getFourth(), quartet.getFourth());
}
@Override
public final int hashCode() {
return Objects.hashCode(getFirst(), getSecond(), getThird(), getFourth());
}
@Override
public final String toString() {
return String.format("(%s, %s, %s, %s)", this.first, this.second, this.third, this.fourth);
}
}

View file

@ -0,0 +1,164 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.types.tuples;
import com.google.common.base.Objects;
import javax.annotation.Nonnull;
/**
* Immutable generic 5-tuple
*
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @param <Y> Fifth type
*/
public class Quintet<U, V, W, X, Y> {
@Nonnull
private final U first;
@Nonnull
private final V second;
@Nonnull
private final W third;
@Nonnull
private final X fourth;
@Nonnull
private final Y fifth;
protected Quintet(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth,
@Nonnull final Y fifth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
this.fifth = fifth;
}
/**
* Create a new 5-tuple
*
* @param first First value
* @param second Second value
* @param third Third value
* @param fourth Fourth value
* @param fifth fifth value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @param <Y> Fifth type
* @return Created quintet
*/
@Nonnull
public static <U, V, W, X, Y> Quintet<U, V, W, X, Y> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth,
@Nonnull final Y fifth) {
return new Quintet<>(first, second, third, fourth, fifth);
}
/**
* Get the first value
*
* @return First value
*/
@Nonnull
public final U getFirst() {
return this.first;
}
/**
* Get the second value
*
* @return Second value
*/
@Nonnull
public final V getSecond() {
return this.second;
}
/**
* Get the third value
*
* @return Third value
*/
@Nonnull
public final W getThird() {
return this.third;
}
/**
* Get the fourth value
*
* @return Fourth value
*/
@Nonnull
public final X getFourth() {
return this.fourth;
}
/**
* Get the fifth value
*
* @return Fifth value
*/
@Nonnull
public final Y getFifth() {
return this.fifth;
}
@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Quintet<?, ?, ?, ?, ?> quintet = (Quintet<?, ?, ?, ?, ?>) o;
return Objects.equal(getFirst(), quintet.getFirst())
&& Objects.equal(getSecond(), quintet.getSecond())
&& Objects.equal(getThird(), quintet.getThird())
&& Objects.equal(getFourth(), quintet.getFourth())
&& Objects.equal(getFifth(), quintet.getFifth());
}
@Override
public final int hashCode() {
return Objects.hashCode(getFirst(), getSecond(), getThird(), getFourth(), getFifth());
}
@Override
public final String toString() {
return String.format("(%s, %s, %s, %s, %s)", this.first, this.second, this.third, this.fourth, this.fifth);
}
}

View file

@ -0,0 +1,184 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.types.tuples;
import com.google.common.base.Objects;
import javax.annotation.Nonnull;
/**
* Immutable generic 6-tuple
*
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @param <Y> Fifth type
* @param <Z> Sixth type
*/
public class Sextet<U, V, W, X, Y, Z> {
@Nonnull
private final U first;
@Nonnull
private final V second;
@Nonnull
private final W third;
@Nonnull
private final X fourth;
@Nonnull
private final Y fifth;
@Nonnull
private final Z sixth;
protected Sextet(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth,
@Nonnull final Y fifth,
@Nonnull final Z sixth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
this.fifth = fifth;
this.sixth = sixth;
}
/**
* Create a new 6-tuple
*
* @param first First value
* @param second Second value
* @param third Third value
* @param fourth Fourth value
* @param fifth fifth value
* @param sixth Sixth value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @param <Y> Fifth type
* @param <Z> Sixth type
* @return Created sextet
*/
@Nonnull
public static <U, V, W, X, Y, Z> Sextet<U, V, W, X, Y, Z> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth,
@Nonnull final Y fifth,
@Nonnull final Z sixth) {
return new Sextet<>(first, second, third, fourth, fifth, sixth);
}
/**
* Get the first value
*
* @return First value
*/
@Nonnull
public final U getFirst() {
return this.first;
}
/**
* Get the second value
*
* @return Second value
*/
@Nonnull
public final V getSecond() {
return this.second;
}
/**
* Get the third value
*
* @return Third value
*/
@Nonnull
public final W getThird() {
return this.third;
}
/**
* Get the fourth value
*
* @return Fourth value
*/
@Nonnull
public final X getFourth() {
return this.fourth;
}
/**
* Get the fifth value
*
* @return Fifth value
*/
@Nonnull
public final Y getFifth() {
return this.fifth;
}
/**
* Get the sixth value
*
* @return Sixth value
*/
@Nonnull
public final Z getSixth() {
return this.sixth;
}
@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Sextet<?, ?, ?, ?, ?, ?> sextet = (Sextet<?, ?, ?, ?, ?, ?>) o;
return Objects.equal(getFirst(), sextet.getFirst())
&& Objects.equal(getSecond(), sextet.getSecond())
&& Objects.equal(getThird(), sextet.getThird())
&& Objects.equal(getFourth(), sextet.getFourth())
&& Objects.equal(getFifth(), sextet.getFifth())
&& Objects.equal(getSixth(), sextet.getSixth());
}
@Override
public final int hashCode() {
return Objects.hashCode(getFirst(), getSecond(), getThird(), getFourth(), getFifth(), getSixth());
}
@Override
public final String toString() {
return String.format("(%s, %s, %s, %s, %s, %s)", this.first, this.second, this.third,
this.fourth, this.fifth, this.sixth);
}
}

View file

@ -0,0 +1,126 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.types.tuples;
import com.google.common.base.Objects;
import javax.annotation.Nonnull;
/**
* Immutable generic 3-tuple
*
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
*/
public class Triplet<U, V, W> {
@Nonnull
private final U first;
@Nonnull
private final V second;
@Nonnull
private final W third;
protected Triplet(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third) {
this.first = first;
this.second = second;
this.third = third;
}
/**
* Create a new 3-tuple
*
* @param first First value
* @param second Second value
* @param third Third value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @return Created triplet
*/
@Nonnull
public static <U, V, W> Triplet<U, V, W> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third) {
return new Triplet<>(first, second, third);
}
/**
* Get the first value
*
* @return First value
*/
@Nonnull
public final U getFirst() {
return this.first;
}
/**
* Get the second value
*
* @return Second value
*/
@Nonnull
public final V getSecond() {
return this.second;
}
/**
* Get the third value
*
* @return Third value
*/
@Nonnull
public final W getThird() {
return this.third;
}
@Override
public final boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Triplet<?, ?, ?> triplet = (Triplet<?, ?, ?>) o;
return Objects.equal(getFirst(), triplet.getFirst())
&& Objects.equal(getSecond(), triplet.getSecond())
&& Objects.equal(getThird(), triplet.getThird());
}
@Override
public final int hashCode() {
return Objects.hashCode(getFirst(), getSecond(), getThird());
}
@Override
public final String toString() {
return String.format("(%s, %s, %s)", this.first, this.second, this.third);
}
}

View file

@ -0,0 +1,141 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.types.tuples;
import javax.annotation.Nonnull;
/**
* Utility class for dealing with tuples
*/
public final class Tuples {
private Tuples() {
}
/**
* Create a new pair
*
* @param first First value
* @param second Second value
* @param <U> First type
* @param <V> Second type
* @return Created pair
*/
@Nonnull
public static <U, V> Pair<U, V> of(@Nonnull final U first,
@Nonnull final V second) {
return Pair.of(first, second);
}
/**
* Create a new triplet
*
* @param first First value
* @param second Second value
* @param third Third value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @return Created triplet
*/
@Nonnull
public static <U, V, W> Triplet<U, V, W> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third) {
return Triplet.of(first, second, third);
}
/**
* Create a new quartet
*
* @param first First value
* @param second Second value
* @param third Third value
* @param fourth Fourth value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @return Created quartet
*/
@Nonnull
public static <U, V, W, X> Quartet<U, V, W, X> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth) {
return Quartet.of(first, second, third, fourth);
}
/**
* Create a new quintet
*
* @param first First value
* @param second Second value
* @param third Third value
* @param fourth Fourth value
* @param fifth Fifth value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @param <Y> Fifth type
* @return Created quintet
*/
@Nonnull
public static <U, V, W, X, Y> Quintet<U, V, W, X, Y> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth,
@Nonnull final Y fifth) {
return Quintet.of(first, second, third, fourth, fifth);
}
/**
* Create a new sextet
*
* @param first First value
* @param second Second value
* @param third Third value
* @param fourth Fourth value
* @param fifth Fifth value
* @param sixth Sixth value
* @param <U> First type
* @param <V> Second type
* @param <W> Third type
* @param <X> Fourth type
* @param <Y> Fifth type
* @param <Z> Sixth type
* @return Created sextet
*/
@Nonnull
public static <U, V, W, X, Y, Z> Sextet<U, V, W, X, Y, Z> of(@Nonnull final U first,
@Nonnull final V second,
@Nonnull final W third,
@Nonnull final X fourth,
@Nonnull final Y fifth,
@Nonnull final Z sixth) {
return Sextet.of(first, second, third, fourth, fifth, sixth);
}
}

View file

@ -0,0 +1,29 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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.
//
/**
* Generic immutable n-tuples with non-null values
* for 1 &lt; n &ge; 6
*/
package com.intellectualsites.commands.types.tuples;