美文网首页
从Hmily思考Enum

从Hmily思考Enum

作者: jjjjxd | 来源:发表于2018-11-05 22:03 被阅读0次

一、Enum使用(1.5使用)

enum在Jdk1.5就已经引入了,在Hmily中很多地方都可以看到它的身影。在日常开发中enum可以替换很多常量,更符合面向对象的观念。记录下它的用法:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hmily.tcc.common.enums;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * The enum Blocking queue type enum.
 *
 * @author xiaoyu
 */
public enum BlockingQueueTypeEnum {

    /**
     * Linked blocking queue blocking queue type enum.
     */
    LINKED_BLOCKING_QUEUE("Linked"),
    /**
     * Array blocking queue blocking queue type enum.
     */
    ARRAY_BLOCKING_QUEUE("Array"),
    /**
     * Synchronous queue blocking queue type enum.
     */
    SYNCHRONOUS_QUEUE("SynchronousQueue");

    private String value;

    BlockingQueueTypeEnum(final String value) {
        this.value = value;
    }

    /**
     * Gets value.
     *
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * From string blocking queue type enum.
     *
     * @param value the value
     * @return the blocking queue type enum
     */
    public static BlockingQueueTypeEnum fromString(final String value) {
        Optional<BlockingQueueTypeEnum> blockingQueueTypeEnum =
                Arrays.stream(BlockingQueueTypeEnum.values())
                        .filter(v -> Objects.equals(v.getValue(), value))
                        .findFirst();
        return blockingQueueTypeEnum.orElse(BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE);
    }

    @Override
    public String toString() {
        return value;
    }
}


这个Enum非常有代表性,它定义了BlockingQueue的四种类型,需要注意的一点如果Enum有有参构造器,那么它一定为private,并且可以在枚举类中添加自己的方法,如本例子的fromString(lambda表达式写法->转换为下的非lambda写法)。

 
public static BlockingQueueTypeEnum fromString(final String value)
    {
        for(BlockingQueueTypeEnum blockingQueueTypeEnum:BlockingQueueTypeEnum.values())
        {
            if(StringUtils.equals(blockingQueueTypeEnum.getValue(),value))
            {
                return  blockingQueueTypeEnum;
            }
        }
        return BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE;
    }

相关文章

  • 从Hmily思考Enum

    一、Enum使用(1.5使用) enum在Jdk1.5就已经引入了,在Hmily中很多地方都可以看到它的身影。在日...

  • java enum关键字

    为什么思考enum关键字 在思考enum之前,同事问了我关于enum的两个问题: enum 类可以继承吗? vo里...

  • 分布式事务实践之hmily

    hmily简介Hmily 一款金融级的分布式事务解决方案,支持 Dubbo、Spring Cloud、Motan ...

  • Hmily

    我连中国的节日都没过全 更没精力在乎别国的狂欢 对你们的爱我都没说出口 怎会轻易对别人说爱 我很在意你们 哪怕现在...

  • Hmily大致原理记录

    项目启动时,Hmily相关配置流程: 项目运行时,对注解了@Hmily的方法执行时所涉及的内部流程: 另外,当利用...

  • Enum 类型转化

    enum & int enum -> int: int -> enum: enum & String enum -...

  • NS_ENUM VS. NS_OPTIONS

    NS_ENUM 从iOS6开始,苹果开始使用NS_ENUM和 NS_OPTIONS宏替代原来的C风格的enum进行...

  • 32. OOP-使用枚举类

    from enum import Enum Month = Enum('Month', ('Jan', 'Feb'...

  • 从 枚举(enum) 到 Swift

    枚举入门 基本定义 枚举作为 Swift 的 一等类型, 我们正好可以通过枚举, 来 一一列举 Swift 其它 ...

  • ★16.枚举类型

    Enum 简介 enum不能被继承。 enum的构造函数自动为私有。 除了以上两点,enum与类相同。 可以通过以...

网友评论

      本文标题:从Hmily思考Enum

      本文链接:https://www.haomeiwen.com/subject/ajpexqtx.html