@SuppressWarnings("unchecked")
  static Object readObjectFrom(
      Input input, Schema<?> schema, Object owner, IdStrategy strategy, final int number)
      throws IOException {
    final boolean graph = input instanceof GraphInput;
    Object ret = null;
    switch (number) {
      case ID_EMPTY_SET:
        if (0 != input.readUInt32()) throw new ProtostuffException("Corrupt input.");

        if (graph) {
          // update the actual reference.
          ((GraphInput) input).updateLast(Collections.EMPTY_SET, owner);
        }

        ret = Collections.EMPTY_SET;
        break;

      case ID_EMPTY_LIST:
        if (0 != input.readUInt32()) throw new ProtostuffException("Corrupt input.");

        if (graph) {
          // update the actual reference.
          ((GraphInput) input).updateLast(Collections.EMPTY_LIST, owner);
        }

        ret = Collections.EMPTY_LIST;
        break;

      case ID_SINGLETON_SET:
        {
          if (0 != input.readUInt32()) throw new ProtostuffException("Corrupt input.");

          final Object collection = iSingletonSet.newInstance();
          if (graph) {
            // update the actual reference.
            ((GraphInput) input).updateLast(collection, owner);
          }

          final int next = input.readFieldNumber(schema);
          if (next == 0) {
            // null element
            return collection;
          }

          if (next != 1) throw new ProtostuffException("Corrupt input");

          final Wrapper wrapper = new Wrapper();
          Object element = input.mergeObject(wrapper, strategy.OBJECT_SCHEMA);
          if (!graph || !((GraphInput) input).isCurrentMessageReference()) element = wrapper.value;

          try {
            fSingletonSet_element.set(collection, element);
          } catch (Exception e) {
            throw new RuntimeException(e);
          }

          ret = collection;
          break;
        }

      case ID_SINGLETON_LIST:
        {
          if (0 != input.readUInt32()) throw new ProtostuffException("Corrupt input.");

          final Object collection = iSingletonList.newInstance();
          if (graph) {
            // update the actual reference.
            ((GraphInput) input).updateLast(collection, owner);
          }

          final int next = input.readFieldNumber(schema);
          if (next == 0) {
            // null element
            return collection;
          }

          if (next != 1) throw new ProtostuffException("Corrupt input.");

          final Wrapper wrapper = new Wrapper();
          Object element = input.mergeObject(wrapper, strategy.OBJECT_SCHEMA);
          if (!graph || !((GraphInput) input).isCurrentMessageReference()) element = wrapper.value;

          try {
            fSingletonList_element.set(collection, element);
          } catch (Exception e) {
            throw new RuntimeException(e);
          }

          ret = collection;
          break;
        }

      case ID_SET_FROM_MAP:
        {
          final Object collection = iSetFromMap.newInstance();
          if (graph) {
            // update the actual reference.
            ((GraphInput) input).updateLast(collection, owner);
          }

          final Wrapper wrapper = new Wrapper();
          Object m = input.mergeObject(wrapper, strategy.POLYMORPHIC_MAP_SCHEMA);
          if (!graph || !((GraphInput) input).isCurrentMessageReference()) m = wrapper.value;

          try {
            fSetFromMap_m.set(collection, m);
            fSetFromMap_s.set(collection, ((Map<?, ?>) m).keySet());
          } catch (Exception e) {
            throw new RuntimeException(e);
          }

          ret = collection;
          break;
        }

      case ID_COPIES_LIST:
        {
          if (0 != input.readUInt32()) throw new ProtostuffException("Corrupt input.");

          final Object collection = iCopiesList.newInstance();
          if (graph) {
            // update the actual reference.
            ((GraphInput) input).updateLast(collection, owner);
          }

          if (1 != input.readFieldNumber(schema)) throw new ProtostuffException("Corrupt input.");

          final int n = input.readUInt32(), next = input.readFieldNumber(schema);

          if (next == 0) {
            // null element
            try {
              fCopiesList_n.setInt(collection, n);
            } catch (Exception e) {
              throw new RuntimeException(e);
            }

            return collection;
          }

          if (next != 2) throw new ProtostuffException("Corrupt input.");

          final Wrapper wrapper = new Wrapper();
          Object element = input.mergeObject(wrapper, strategy.OBJECT_SCHEMA);
          if (!graph || !((GraphInput) input).isCurrentMessageReference()) element = wrapper.value;

          try {
            fCopiesList_n.setInt(collection, n);
            fCopiesList_element.set(collection, element);
          } catch (Exception e) {
            throw new RuntimeException(e);
          }

          ret = collection;
          break;
        }

      case ID_UNMODIFIABLE_COLLECTION:
        ret =
            readUnmodifiableCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iUnmodifiableCollection.newInstance(),
                false,
                false);
        break;
      case ID_UNMODIFIABLE_SET:
        ret =
            readUnmodifiableCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iUnmodifiableSet.newInstance(),
                false,
                false);
        break;
      case ID_UNMODIFIABLE_SORTED_SET:
        ret =
            readUnmodifiableCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iUnmodifiableSortedSet.newInstance(),
                true,
                false);
        break;
      case ID_UNMODIFIABLE_LIST:
        ret =
            readUnmodifiableCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iUnmodifiableList.newInstance(),
                false,
                true);
        break;
      case ID_UNMODIFIABLE_RANDOM_ACCESS_LIST:
        ret =
            readUnmodifiableCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iUnmodifiableRandomAccessList.newInstance(),
                false,
                true);
        break;

      case ID_SYNCHRONIZED_COLLECTION:
        ret =
            readSynchronizedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iSynchronizedCollection.newInstance(),
                false,
                false);
        break;
      case ID_SYNCHRONIZED_SET:
        ret =
            readSynchronizedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iSynchronizedSet.newInstance(),
                false,
                false);
        break;
      case ID_SYNCHRONIZED_SORTED_SET:
        ret =
            readSynchronizedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iSynchronizedSortedSet.newInstance(),
                true,
                false);
        break;
      case ID_SYNCHRONIZED_LIST:
        ret =
            readSynchronizedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iSynchronizedList.newInstance(),
                false,
                true);
        break;
      case ID_SYNCHRONIZED_RANDOM_ACCESS_LIST:
        ret =
            readSynchronizedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iSynchronizedRandomAccessList.newInstance(),
                false,
                true);
        break;

      case ID_CHECKED_COLLECTION:
        ret =
            readCheckedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iCheckedCollection.newInstance(),
                false,
                false);
        break;
      case ID_CHECKED_SET:
        ret =
            readCheckedCollectionFrom(
                input, schema, owner, strategy, graph, iCheckedSet.newInstance(), false, false);
        break;
      case ID_CHECKED_SORTED_SET:
        ret =
            readCheckedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iCheckedSortedSet.newInstance(),
                true,
                false);
        break;
      case ID_CHECKED_LIST:
        ret =
            readCheckedCollectionFrom(
                input, schema, owner, strategy, graph, iCheckedList.newInstance(), false, true);
        break;
      case ID_CHECKED_RANDOM_ACCESS_LIST:
        ret =
            readCheckedCollectionFrom(
                input,
                schema,
                owner,
                strategy,
                graph,
                iCheckedRandomAccessList.newInstance(),
                false,
                true);
        break;

      case ID_ENUM_SET:
        {
          final Collection<?> es = strategy.resolveEnumFrom(input).newEnumSet();

          if (graph) {
            // update the actual reference.
            ((GraphInput) input).updateLast(es, owner);
          }

          // TODO enum schema
          strategy.COLLECTION_SCHEMA.mergeFrom(input, (Collection<Object>) es);
          return es;
        }

      case ID_COLLECTION:
        {
          final Collection<Object> collection = strategy.resolveCollectionFrom(input).newMessage();

          if (graph) {
            // update the actual reference.
            ((GraphInput) input).updateLast(collection, owner);
          }

          strategy.COLLECTION_SCHEMA.mergeFrom(input, collection);

          return collection;
        }

      default:
        throw new ProtostuffException("Corrupt input.");
    }

    if (0 != input.readFieldNumber(schema)) throw new ProtostuffException("Corrupt input.");

    return ret;
  }
  static Object instanceFrom(final int id) {
    switch (id) {
      case ID_EMPTY_SET:
        return Collections.EMPTY_SET;
      case ID_EMPTY_LIST:
        return Collections.EMPTY_LIST;

      case ID_SINGLETON_SET:
        return iSingletonSet.newInstance();
      case ID_SINGLETON_LIST:
        return iSingletonList.newInstance();

      case ID_SET_FROM_MAP:
        return iSetFromMap.newInstance();
      case ID_COPIES_LIST:
        return iCopiesList.newInstance();

      case ID_UNMODIFIABLE_COLLECTION:
        return iUnmodifiableCollection.newInstance();
      case ID_UNMODIFIABLE_SET:
        return iUnmodifiableSet.newInstance();
      case ID_UNMODIFIABLE_SORTED_SET:
        return iUnmodifiableSortedSet.newInstance();
      case ID_UNMODIFIABLE_LIST:
        return iUnmodifiableList.newInstance();
      case ID_UNMODIFIABLE_RANDOM_ACCESS_LIST:
        return iUnmodifiableRandomAccessList.newInstance();

      case ID_SYNCHRONIZED_COLLECTION:
        return iSynchronizedCollection.newInstance();
      case ID_SYNCHRONIZED_SET:
        return iSynchronizedSet.newInstance();
      case ID_SYNCHRONIZED_SORTED_SET:
        return iSynchronizedSortedSet.newInstance();
      case ID_SYNCHRONIZED_LIST:
        return iSynchronizedList.newInstance();
      case ID_SYNCHRONIZED_RANDOM_ACCESS_LIST:
        return iSynchronizedRandomAccessList.newInstance();

      case ID_CHECKED_COLLECTION:
        return iCheckedCollection.newInstance();
      case ID_CHECKED_SET:
        return iCheckedSet.newInstance();
      case ID_CHECKED_SORTED_SET:
        return iCheckedSortedSet.newInstance();
      case ID_CHECKED_LIST:
        return iCheckedList.newInstance();
      case ID_CHECKED_RANDOM_ACCESS_LIST:
        return iCheckedRandomAccessList.newInstance();

      default:
        throw new RuntimeException("Unknown id: " + id);
    }
  }