/*
 * Copyright 2019 Google LLC
 *
 * Licensed 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
 *
 *     https://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.google.android.gnd.persistence.local.room.models;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.TypeConverter;
import com.google.android.gnd.model.form.Field;
import com.google.android.gnd.model.form.Field.Type;
import com.google.android.gnd.persistence.local.room.IntEnum;

public enum FieldEntityType implements IntEnum {
  UNKNOWN(0),
  TEXT(1),
  MULTIPLE_CHOICE(2),
  PHOTO(3),
  NUMBER(4);

  private final int intValue;

  FieldEntityType(int intValue) {
    this.intValue = intValue;
  }

  @Override
  public int intValue() {
    return intValue;
  }

  public static FieldEntityType fromFieldType(Field.Type type) {
    switch (type) {
      case TEXT_FIELD:
        return TEXT;
      case MULTIPLE_CHOICE:
        return MULTIPLE_CHOICE;
      case PHOTO:
        return PHOTO;
      case NUMBER:
        return NUMBER;
      default:
        return UNKNOWN;
    }
  }

  public Field.Type toFieldType() {
    switch (this) {
      case TEXT:
        return Field.Type.TEXT_FIELD;
      case MULTIPLE_CHOICE:
        return Field.Type.MULTIPLE_CHOICE;
      case PHOTO:
        return Type.PHOTO;
      case NUMBER:
        return Type.NUMBER;
      default:
        throw new IllegalArgumentException("Unknown field type");
    }
  }

  @TypeConverter
  public static int toInt(@Nullable FieldEntityType value) {
    return IntEnum.toInt(value, UNKNOWN);
  }

  @NonNull
  @TypeConverter
  public static FieldEntityType fromInt(int intValue) {
    return IntEnum.fromInt(values(), intValue, UNKNOWN);
  }
}

@@ -923,16 +923,65 @@
 IntEnum;
+%0Aimport com.google.common.collect.ImmutableBiMap;
 %0A%0Apublic
@@ -1133,455 +1133,544 @@
 %0A%0A  
-FieldEntityType(int intValue) %7B%0A    this.intValue = intValue;%0A  %7D%0A%0A  @Override%0A  public int intValue() %7B%0A    return intValue;%0A  %7D%0A%0A  public static FieldEntityType fromFieldType(Field.Type type) %7B%0A    switch (type) %7B%0A      case TEXT_FIELD:%0A        return TEXT;%0A      case MULTIPLE_CHOICE:%0A        return MULTIPLE_CHOICE;%0A      case PHOTO:%0A        return PHOTO;%0A      case NUMBER:%0A        return NUMBER;%0A      default:%0A        return 
+private static ImmutableBiMap%3CFieldEntityType, Type%3E FIELD_TYPES =%0A      ImmutableBiMap.of(%0A          TEXT, Field.Type.TEXT_FIELD,%0A          MULTIPLE_CHOICE, Field.Type.MULTIPLE_CHOICE,%0A          PHOTO, Field.Type.PHOTO,%0A          NUMBER, Field.Type.NUMBER);%0A%0A  FieldEntityType(int intValue) %7B%0A    this.intValue = intValue;%0A  %7D%0A%0A  @Override%0A  public int intValue() %7B%0A    return intValue;%0A  %7D%0A%0A  public static FieldEntityType fromFieldType(Field.Type type) %7B%0A    return FIELD_TYPES.inverse().getOrDefault(type, FieldEntityType.
 UNKNOWN
+)
 ;%0A
-    %7D%0A
   %7D%0A
@@ -1714,328 +1714,66 @@
     
-switch (this) %7B%0A      case TEXT:%0A        return Field.Type.TEXT_FIELD;%0A      case MULTIPLE_CHOICE:%0A        return Field.Type.MULTIPLE_CHOICE;%0A      case PHOTO:%0A        return Type.PHOTO;%0A      case NUMBER:%0A        return Type.NUMBER;%0A      default:%0A        throw new IllegalArgumentException(%22Unknown f
+return FIELD_TYPES.getOrDefault(this, F
 ield
- type%22);%0A    %7D
+.Type.UNKNOWN);
 %0A  %7D

Use BiMap for field type conversion