Skip to content

Instantly share code, notes, and snippets.

@zaki50
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save zaki50/d09070c76f7532a7df1f to your computer and use it in GitHub Desktop.

Select an option

Save zaki50/d09070c76f7532a7df1f to your computer and use it in GitHub Desktop.

Revisions

  1. zaki50 revised this gist May 19, 2015. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion gistfile1.txt
    Original 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;
    }
    }
  2. zaki50 revised this gist May 19, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions gistfile1.txt
    Original 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);

  3. zaki50 created this gist May 19, 2015.
    23 changes: 23 additions & 0 deletions gistfile1.txt
    Original 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;
    }
    }