From 51ceef97bc5c1608f79dc89e6cbab7e412112b84 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Tue, 21 Nov 2023 00:13:35 +0100 Subject: [PATCH] Switch from baked-in functional/Either previously needed for Java 1.6 compat to our dependency that contains these now that we have Java 11 compat --- pom.xml | 5 + .../com/evolvedbinary/functional/Either.java | 196 ------------------ .../evolvedbinary/functional/Function.java | 37 ---- .../xpath/parser/XPathParser.java | 2 +- .../xpath/parser/ast/DocumentTest.java | 2 +- .../ast/partial/PartialDocumentTest.java | 2 +- .../xpath/parser/XPathParserTest.java | 2 +- 7 files changed, 9 insertions(+), 237 deletions(-) delete mode 100644 src/main/java/com/evolvedbinary/functional/Either.java delete mode 100644 src/main/java/com/evolvedbinary/functional/Function.java diff --git a/pom.xml b/pom.xml index f3da411..7d93895 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,11 @@ jsr305 3.0.2 + + com.evolvedbinary.j8fu + j8fu + 1.23.0 + junit junit diff --git a/src/main/java/com/evolvedbinary/functional/Either.java b/src/main/java/com/evolvedbinary/functional/Either.java deleted file mode 100644 index c0eef4b..0000000 --- a/src/main/java/com/evolvedbinary/functional/Either.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * XPath 2 Parser - * A Parser for XPath 2 - * Copyright (C) 2016 Evolved Binary Ltd. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ -package com.evolvedbinary.functional; - -import java.util.NoSuchElementException; - -/** - * A disjunction, more basic than but similar to {@link scala.util.Either} - * in addition it is also right-biased in a similar way to {@link scalaz.\/} - * - * @param Type of left-hand-side parameter - * @param Type of right-hand-side parameter - * - * @author Adam Retter - */ -public abstract class Either { - - private final boolean isLeft; - - Either(final boolean isLeft) { - this.isLeft = isLeft; - } - - public final boolean isLeft() { - return isLeft; - } - - public final boolean isRight() { - return !isLeft; - } - - public final LeftProjection left() { - return new LeftProjection(this); - } - - public final RightProjection right() { - return new RightProjection(this); - } - - /** - * Map on the right-hand-side of the disjunction - * - * @param f The function to map with - */ - public final Either map(final Function f) { - if(isLeft()) { - return (Left)this; - } else { - return Right(f.apply(((Right)this).value)); - } - } - - /** - * Bind through on the right-hand-side of this disjunction - * - * @param f the function to bind through - */ - public final Either flatMap(final Function> f) { - if(isLeft) { - return (Left)this; - } else { - return f.apply(((Right)this).value); - } - } - - /** - * Map on the left-hand-side of the disjunction - * - * @param f The function to map with - */ - public final Either leftMap(final Function f) { - if(isLeft) { - return Left(f.apply(((Left)this).value)); - } else { - return (Right)this; - } - } - - /** - * Catamorphism. Run the first given function if left, - * otherwise the second given function - * - * - * @param The result type from performing the fold - * @param lf A function that may be applied to the left-hand-side - * @param rf A function that may be applied to the right-hand-side - */ - public final T fold(final Function lf, final Function rf) { - if(isLeft) { - return lf.apply(((Left)this).value); - } else { - return rf.apply(((Right)this).value); - } - } - - /** - * Return the value from the right-hand-side of this disjunction or - * run the function on the left-hand-side - * - * @param The result type - * @param lf A function that may be applied to the left-hand-side - */ - public final RR valueOr(final Function lf) { - if(isLeft) { - return lf.apply(((Left)this).value); - } else { - return ((Right)this).value; - } - } - - @Override - public boolean equals(final Object obj) { - if(obj != null && obj instanceof Either) { - final Either other = (Either)obj; - - if(other.isLeft && this.isLeft) { - return other.left().get().equals(this.left().get()); - } else if((!other.isLeft) && (!this.isLeft)) { - return other.right().get().equals(this.right().get()); - } - } - - return false; - } - - public final static Either Left(final L value) { - return new Left(value); - } - - public final static Either Right(final R value) { - return new Right(value); - } - - public final static class Left extends Either { - final L value; - private Left(final L value) { - super(true); - this.value = value; - } - } - - public final static class Right extends Either { - final R value; - private Right(final R value) { - super(false); - this.value = value; - } - } - - public final class LeftProjection { - final Either e; - private LeftProjection(final Either e) { - this.e = e; - } - - public final L get() { - if(e.isLeft()) { - return ((Left)e).value; - } else { - throw new NoSuchElementException("Either.left value on Right"); - } - } - } - - public final class RightProjection { - final Either e; - private RightProjection(final Either e) { - this.e = e; - } - - public final R get() { - if(e.isRight()) { - return ((Right)e).value; - } else { - throw new NoSuchElementException("Either.right value on Left"); - } - } - } -} diff --git a/src/main/java/com/evolvedbinary/functional/Function.java b/src/main/java/com/evolvedbinary/functional/Function.java deleted file mode 100644 index 3cb49b4..0000000 --- a/src/main/java/com/evolvedbinary/functional/Function.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * XPath 2 Parser - * A Parser for XPath 2 - * Copyright (C) 2016 Evolved Binary Ltd. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ -package com.evolvedbinary.functional; - -/** - * Replacement interface for Java 8 {@link java.util.function.Function} - * - * @param the type of the input to the function - * @param the type of the result of the function - */ -public interface Function { - - /** - * Applies this function to the given argument. - * - * @param t the function argument - * @return the function result - */ - R apply(final T t); -} diff --git a/src/main/java/com/evolvedbinary/xpath/parser/XPathParser.java b/src/main/java/com/evolvedbinary/xpath/parser/XPathParser.java index 1e71c0c..3781299 100644 --- a/src/main/java/com/evolvedbinary/xpath/parser/XPathParser.java +++ b/src/main/java/com/evolvedbinary/xpath/parser/XPathParser.java @@ -19,7 +19,7 @@ */ package com.evolvedbinary.xpath.parser; -import com.evolvedbinary.functional.Either; +import com.evolvedbinary.j8fu.Either; import com.evolvedbinary.xpath.parser.ast.partial.*; import org.parboiled.BaseParser; import org.parboiled.Rule; diff --git a/src/main/java/com/evolvedbinary/xpath/parser/ast/DocumentTest.java b/src/main/java/com/evolvedbinary/xpath/parser/ast/DocumentTest.java index 5c82227..e5a1854 100644 --- a/src/main/java/com/evolvedbinary/xpath/parser/ast/DocumentTest.java +++ b/src/main/java/com/evolvedbinary/xpath/parser/ast/DocumentTest.java @@ -19,7 +19,7 @@ */ package com.evolvedbinary.xpath.parser.ast; -import com.evolvedbinary.functional.Either; +import com.evolvedbinary.j8fu.Either; import javax.annotation.Nullable; /** diff --git a/src/main/java/com/evolvedbinary/xpath/parser/ast/partial/PartialDocumentTest.java b/src/main/java/com/evolvedbinary/xpath/parser/ast/partial/PartialDocumentTest.java index c440a65..eb82721 100644 --- a/src/main/java/com/evolvedbinary/xpath/parser/ast/partial/PartialDocumentTest.java +++ b/src/main/java/com/evolvedbinary/xpath/parser/ast/partial/PartialDocumentTest.java @@ -19,7 +19,7 @@ */ package com.evolvedbinary.xpath.parser.ast.partial; -import com.evolvedbinary.functional.Either; +import com.evolvedbinary.j8fu.Either; import com.evolvedbinary.xpath.parser.ast.ElementTest; import com.evolvedbinary.xpath.parser.ast.DocumentTest; import com.evolvedbinary.xpath.parser.ast.SchemaElementTest; diff --git a/src/test/java/com/evolvedbinary/xpath/parser/XPathParserTest.java b/src/test/java/com/evolvedbinary/xpath/parser/XPathParserTest.java index 3f19447..f8482df 100644 --- a/src/test/java/com/evolvedbinary/xpath/parser/XPathParserTest.java +++ b/src/test/java/com/evolvedbinary/xpath/parser/XPathParserTest.java @@ -19,7 +19,7 @@ */ package com.evolvedbinary.xpath.parser; -import com.evolvedbinary.functional.Either; +import com.evolvedbinary.j8fu.Either; import com.evolvedbinary.xpath.parser.ast.*; import org.junit.Test; import org.parboiled.Parboiled;