/*
 * Copyright 2013 Square Inc.
 *
 * 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
 *
 * 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.squareup.wire;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Converts values of an enum to and from integers.
 */
final class EnumAdapter<E extends ProtoEnum> {
  private final Map<Integer, E> fromInt = new LinkedHashMap<Integer, E>();

  EnumAdapter(Class<E> type) {
    for (E value : type.getEnumConstants()) {
      fromInt.put(value.getValue(), value);
    }
  }

  public int toInt(E e) {
    return e.getValue();
  }

  public E fromInt(int value) {
    return fromInt.get(value);
  }
}

@@ -629,29 +629,22 @@
 va.util.
-LinkedHashMap
+Arrays
 ;%0Aimport
@@ -654,19 +654,26 @@
 va.util.
-Map
+Comparator
 ;%0A%0A/**%0A 
@@ -788,174 +788,554 @@
 ate 
-final Map%3CInteger, E%3E fromInt = new LinkedHashMap%3CInteger, E%3E();%0A%0A  EnumAdapter(Class%3CE%3E type) %7B%0A    for (E value : type.getEnumConstants()) %7B%0A      fromInt.put(value
+static final Comparator%3CProtoEnum%3E COMPARATOR = new Comparator%3CProtoEnum%3E() %7B%0A    @Override public int compare(ProtoEnum o1, ProtoEnum o2) %7B%0A      return o1.getValue() - o2.getValue();%0A    %7D%0A  %7D;%0A%0A  private final Class%3CE%3E type;%0A%0A  private final int%5B%5D values;%0A  private final E%5B%5D constants;%0A%0A  EnumAdapter(Class%3CE%3E type) %7B%0A    this.type = type;%0A%0A    constants = type.getEnumConstants();%0A    Arrays.sort(constants, COMPARATOR);%0A%0A    values = new int%5Bconstants.length%5D;%0A    for (int i = 0; i %3C constants.length; i++) %7B%0A      values%5Bi%5D = constants%5Bi%5D
 .get
@@ -1345,16 +1345,8 @@
 ue()
-, value)
 ;%0A  
@@ -1450,33 +1450,228 @@
     
-return fromInt.get(value)
+int index = Arrays.binarySearch(values, value);%0A    if (index %3C 0) %7B%0A      throw new IllegalArgumentException(%0A          %22Unknown enum tag %22 + value + %22 for %22 + type.getCanonicalName());%0A    %7D%0A    return constants%5Bindex%5D
 ;%0A  

Replace the enum adapter tag map with an integer BST.