惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
C
Check Point Blog
V
V2EX
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
A
About on SuperTechFans
D
DataBreaches.Net
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
博客园_首页
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

李锋镝的博客

LiteLLM 本地代理搭建 Claude-HUD 使用文档 Kratos+ —— Kratos 主题二次开发记录 译文:如何将单体应用拆解为微服务 codebase-memory-mcp 极简完整使用指南 Claude Haiku 4.5、Claude Sonnet 4.6、Claude Opus 4.7 区别以及各自的新特性 SchedulingConfigurer详解 踩坑60+次后,我终于搞懂 Claude Skill 怎么写才会真的触发 Everything Claude Code 详细使用文档 配置Jackson使用字段而不是getter/setter来序列化和反序列化 这个域名注册整整十年了,十年时间,真快啊 Claude Code全维度实战指南:从入门到精通,解锁AI编程新范式 Apollo配置中心中的protalDB的作用是什么 org.apache.ibatis.plugin.Interceptor类详细介绍及使用 岁末 Excel2016右键新建工作表,打开时提示“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决办法 wordpress增加说说功能 玩博客的人是不是越来越少了? Java 为什么有这么多 “O”? 别再背线程池的七大参数了,现在面试官都这么问 2024年11月1号 农历十月初一 我的第一个WordPress插件:Dylan Custom Plugin上线了 推荐一款比较养眼的Xshell配色方案 hnswlib installation failed 开工啦~ 阳了... MybatisCodeHelperPro激活 @Async注解的坑 新买的笔记本发货啦…… 这个中秋节感觉过的好累啊
基于Java8的Either类
李锋镝 · 2021-08-24 · via 李锋镝的博客

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

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/article/1958