@Override
  public void write(JsonWriter out, T value) throws IOException {
    if (value == null) {
      out.nullValue();
      return;
    }
    for (int i = 0; i < subtypes.length; i++) {
      TypeToken<? extends T> subtype = subtypes[i];
      if (subtype.getRawType().isInstance(value)) {
        // safe unchecked, type is checked at runtime
        @SuppressWarnings("unchecked") TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) adapters.get(i);
        typeAdapter.write(out, value);
        return;
      }
    }
    gson.toJson(value, value.getClass(), out);
  }
Creates adapter from {@link Gson} and type tokens.
@param <T> the generic type
@param gson Gson instance
@param type supertype token
@param subtypes subtype tokens
@return subtype adapter
gson/src/org/immutables/gson/adapter/ExpectedSubtypesAdapter.java