Last active
August 29, 2015 14:21
-
-
Save zaki50/d09070c76f7532a7df1f to your computer and use it in GitHub Desktop.
Revisions
-
zaki50 revised this gist
May 19, 2015 . 1 changed file with 27 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ package com.example; import android.support.annotation.IntDef; import android.util.SparseArray; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -13,6 +14,8 @@ import static com.example.Size.ValidSize.SIZE_S; public enum Size { L(SIZE_L), M(SIZE_M), S(SIZE_S); private volatile static SparseArray<Size> rawValueToEnum; @ValidSize private final int rawValue; @@ -32,4 +35,27 @@ public enum Size { int SIZE_M = 2; int SIZE_S = 3; } public static Size fromRawValue(int rawValue) { if (rawValueToEnum == null) { final SparseArray<Size> sizeSparseArray = new SparseArray<>(); final Size[] values = values(); for (Size size : values) { sizeSparseArray.append(size.getRawValue(), size); } // おまけ。値の重複チェックをしておく if (sizeSparseArray.size() != values.length) { throw new IllegalStateException("duplicate rawValues in Size enum"); } rawValueToEnum = sizeSparseArray; } final Size size = rawValueToEnum.get(rawValue); if (size == null) { throw new IllegalArgumentException("invalid rawValue: " + rawValue); } return size; } } -
zaki50 revised this gist
May 19, 2015 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,15 @@ package com.example; import android.support.annotation.IntDef; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import static com.example.Size.ValidSize.SIZE_L; import static com.example.Size.ValidSize.SIZE_M; import static com.example.Size.ValidSize.SIZE_S; public enum Size { L(SIZE_L), M(SIZE_M), S(SIZE_S); -
zaki50 created this gist
May 19, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ public enum Size { L(SIZE_L), M(SIZE_M), S(SIZE_S); @ValidSize private final int rawValue; Size(@ValidSize int rawValue) { this.rawValue = rawValue; } @ValidSize public int getRawValue() { return rawValue; } @Retention(RetentionPolicy.SOURCE) @IntDef({SIZE_L, SIZE_M, SIZE_S}) public @interface ValidSize { int SIZE_L = 1; int SIZE_M = 2; int SIZE_S = 3; } }