From ad8abbd84e018ddbdd4634157a477c347bae4d01 Mon Sep 17 00:00:00 2001 From: Gaddict Date: Wed, 27 Jan 2016 14:37:20 +0900 Subject: [PATCH] =?UTF-8?q?Enum=E3=81=AE=E4=BD=BF=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit これだけゴチャゴチャ書けるのはJavaだけっぽい --- java/enum/enumMember.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 java/enum/enumMember.java diff --git a/java/enum/enumMember.java b/java/enum/enumMember.java new file mode 100644 index 0000000..e0343ec --- /dev/null +++ b/java/enum/enumMember.java @@ -0,0 +1,26 @@ +// かっこいいEnumの活用 + + public enum FileType { + SystemCd("sys", 10), + DcfDrCd("dcf", 6); + public final String paramName; + public final int cdLength; + private FileType(final String paramName, final int cdLength) { + this.paramName = paramName; + this.cdLength = cdLength; + } + public static Optional paramNameOf(final String paramName) { + return Stream.of(values()).filter(f -> f.paramName.equals(paramName)).findAny(); + } + } + +// enumはこんなに拡張できる(Javaのみらしい) +// 以下サンプル + +// fileTypeで与えられた文字列が定義されてるかどうかを一撃で判定できる +final String fileType = "hoge"; +if (!FileType.paramNameOf(fileType).isPresent()) + +// enumの各項目に紐づく値を取れる +FileType filetype = FileType.SystemCd; +fileType.cdLength;