李锋镝的博客

  • 首页
  • 时间轴
  • 留言
  • 插件
  • 左邻右舍
  • 我的日常
  • 关于我
    • 关于我
    • 另一个网站
  • 知识库
  • 赞助
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

基于Java8的Either类

2020年6月3日 19215点热度 0人点赞 5条评论

主要作用就是一个方法可以返回两个不同类型的参数,源码如下:

package com.lifengdi.common;

import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 *
 * @author: 李锋镝
 * @date: 2020/06/03 10:29
 */
public abstract class Either<L, R> implements Serializable {

    public static <L, R> Either<L, R> either(Supplier<L> leftSupplier, Supplier<R> rightSupplier) {
        R rightValue = rightSupplier.get();
        if (rightValue != null) {
            return Either.right(rightValue);
        } else {
            return Either.left(leftSupplier.get());
        }
    }

    public static <L, R> Either<L, R> left(L left) {
        return new Left<>(left);
    }

    public static <L, R> Either<L, R> right(R right) {
        Objects.requireNonNull(right);
        return new Right<>(right);
    }

    public abstract L getLeft();

    public abstract R getRight();

    public abstract boolean isLeft();

    public abstract boolean isRight();

    public abstract <T> T fold(Function<L, T> transformLeft, Function<R, T> transformRight);

    public Either<R, L> swap() {
        return either(this::getRight, this::getLeft);
    }

    public abstract <T, U> Either<T, U> map(Function<L, T> transformLeft, Function<R, U> transformRight);

    public abstract <T> Either<L, T> mapRight(Function<? super R, ? extends T> rFunc);

    public abstract Either<L, R> peekRight(Consumer<? super R> action);

    public abstract void run(Consumer<L> runLeft, Consumer<R> runRight);

    public abstract Optional<R> toOptional();

    public static class Left<L, R> extends Either<L, R> {

        L leftValue;

        private Left(L left) {
            this.leftValue = left;
        }

        @Override
        public L getLeft() {
            return this.leftValue;
        }

        @Override
        public R getRight() {
            throw new NoSuchElementException("Tried to getRight from a Left");
        }

        @Override
        public boolean isLeft() {
            return true;
        }

        @Override
        public boolean isRight() {
            return false;
        }

        @Override
        public <T> T fold(Function<L, T> transformLeft, Function<R, T> transformRight) {
            return transformLeft.apply(this.leftValue);
        }

        @Override
        public <T, U> Either<T, U> map(Function<L, T> transformLeft, Function<R, U> transformRight) {
            return Either.left(transformLeft.apply(this.leftValue));
        }

        @Override
        public <T> Either<L, T> mapRight(Function<? super R, ? extends T> rFunc) {
            return Either.left(this.getLeft());
        }

        @Override
        public Either<L, R> peekRight(Consumer<? super R> action) {
            return Either.left(this.getLeft());
        }

        @Override
        public void run(Consumer<L> runLeft, Consumer<R> runRight) {
            runLeft.accept(this.leftValue);
        }

        @Override
        public Optional<R> toOptional() {
            return Optional.empty();
        }

        @Override
        public int hashCode() {
            return this.leftValue.hashCode();
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof Left<?, ?>) {
                final Left<?, ?> otherAsLeft = (Left<?, ?>) other;
                return this.leftValue.equals(otherAsLeft.leftValue);
            } else {
                return false;
            }
        }

    }

    public static class Right<L, R> extends Either<L, R> {

        R rightValue;

        private Right(R right) {
            this.rightValue = right;
        }

        @Override
        public L getLeft() {
            throw new NoSuchElementException("Tried to getLeft from a Right");
        }

        @Override
        public R getRight() {
            return rightValue;
        }

        @Override
        public boolean isLeft() {
            return false;
        }

        @Override
        public boolean isRight() {
            return true;
        }

        @Override
        public <T> T fold(Function<L, T> transformLeft, Function<R, T> transformRight) {
            Objects.requireNonNull(transformRight);
            return transformRight.apply(this.rightValue);
        }

        @Override
        public <T, U> Either<T, U> map(Function<L, T> transformLeft, Function<R, U> transformRight) {
            Objects.requireNonNull(transformRight);
            return Either.right(transformRight.apply(this.rightValue));
        }

        @Override
        public <T> Either<L, T> mapRight(Function<? super R, ? extends T> rFunc) {
            return Either.right(rFunc.apply(this.getRight()));
        }

        @Override
        public Either<L, R> peekRight(Consumer<? super R> action) {
            action.accept(getRight());
            return this;
        }

        @Override
        public void run(Consumer<L> runLeft, Consumer<R> runRight) {
            runRight.accept(this.rightValue);
        }

        @Override
        public Optional<R> toOptional() {
            return Optional.ofNullable(rightValue);
        }

        @Override
        public int hashCode() {
            return this.rightValue.hashCode();
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof Right<?, ?>) {
                final Right<?, ?> otherAsRight = (Right<?, ?>) other;
                return this.rightValue.equals(otherAsRight.rightValue);
            } else {
                return false;
            }
        }

    }
}
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/archives/article/1958

相关文章

  • Spring中的Aware接口
  • 双 Token 机制
  • Java 为什么有这么多 “O”?
  • 别再背线程池的七大参数了,现在面试官都这么问
  • 以面试官视角万字解读线程池10大经典面试题
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: JAVA
最后更新:2020年6月3日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

  • yangxuan_321@163.com

    不错,可以替代异常,消除代码中的副作用。

    2021年8月24日
    回复
  • 贺龙

    是否实用呢

    2021年5月26日
    回复
    • blank
      李锋镝

      @贺龙 结合了JDK1.8的新特性,可以让一个方法返回两个不同类型的对象,个人感觉还是比较实用的。

      2021年5月27日
      回复
    • blank
      yangxuan_321@163.com

      @贺龙 建议读一下,scala小红书。说不定你就不会这么想啦~ :ku:

      2021年8月24日
      回复
      • blank
        贺龙

        @yangxuan_321@163.com 好的,编程语言发展的真快 :haha:

        2021年8月25日
        回复
  • 1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
    取消回复

    COPYRIGHT © 2025 lifengdi.com. ALL RIGHTS RESERVED.

    Theme Kratos Made By Dylan

    津ICP备2024022503号-3