一、Enum使用(1.5使用)
enum在Jdk1.5就已經(jīng)引入了天吓,在Hmily中很多地方都可以看到它的身影贿肩。在日常開發(fā)中enum可以替換很多常量,更符合面向?qū)ο蟮挠^念龄寞。記錄下它的用法:
/*
* 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;
}
}
這個(gè)Enum非常有代表性汰规,它定義了BlockingQueue的四種類型,需要注意的一點(diǎn)如果Enum有有參構(gòu)造器物邑,那么它一定為private溜哮,并且可以在枚舉類中添加自己的方法,如本例子的fromString(lambda表達(dá)式寫法->轉(zhuǎn)換為下的非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;
}