diff --git a/java/core/src/main/java/com/google/protobuf/Internal.java b/java/core/src/main/java/com/google/protobuf/Internal.java index 3024aa9dfb..07bec5ef29 100644 --- a/java/core/src/main/java/com/google/protobuf/Internal.java +++ b/java/core/src/main/java/com/google/protobuf/Internal.java @@ -371,6 +371,36 @@ public final class Internal { return ((MessageLite) destination).toBuilder().mergeFrom((MessageLite) source).buildPartial(); } + /** + * Provides an immutable view of {@code List} around an {@code IntList}. + * + *

Protobuf internal. Used in protobuf generated code only. + */ + public static class IntListAdapter extends AbstractList { + /** Convert individual elements of the List from int to T. */ + public interface IntConverter { + T convert(int from); + } + + private final IntList fromList; + private final IntConverter converter; + + public IntListAdapter(IntList fromList, IntConverter converter) { + this.fromList = fromList; + this.converter = converter; + } + + @Override + public T get(int index) { + return converter.convert(fromList.getInt(index)); + } + + @Override + public int size() { + return fromList.size(); + } + } + /** * Provides an immutable view of {@code List} around a {@code List}. * diff --git a/src/google/protobuf/compiler/java/lite/enum_field.cc b/src/google/protobuf/compiler/java/lite/enum_field.cc index f2fb340b1b..3e86e1942f 100644 --- a/src/google/protobuf/compiler/java/lite/enum_field.cc +++ b/src/google/protobuf/compiler/java/lite/enum_field.cc @@ -597,12 +597,12 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers( variables_, "private com.google.protobuf.Internal.IntList $name$_;\n" "private static final " - "com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$> $name$_converter_ =\n" - " new com.google.protobuf.Internal.ListAdapter.Converter<\n" - " java.lang.Integer, $type$>() {\n" + "com.google.protobuf.Internal.IntListAdapter.IntConverter<\n" + " $type$> $name$_converter_ =\n" + " new com.google.protobuf.Internal.IntListAdapter.IntConverter<\n" + " $type$>() {\n" " @java.lang.Override\n" - " public $type$ convert(java.lang.Integer from) {\n" + " public $type$ convert(int from) {\n" " $type$ result = $type$.forNumber(from);\n" " return result == null ? $unknown$ : result;\n" " }\n" @@ -610,14 +610,13 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers( PrintExtraFieldInfo(variables_, printer); WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER, context_->options()); - printer->Print( - variables_, - "@java.lang.Override\n" - "$deprecation$public java.util.List<$type$> " - "${$get$capitalized_name$List$}$() {\n" - " return new com.google.protobuf.Internal.ListAdapter<\n" - " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" - "}\n"); + printer->Print(variables_, + "@java.lang.Override\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" + " return new com.google.protobuf.Internal.IntListAdapter<\n" + " $type$>($name$_, $name$_converter_);\n" + "}\n"); printer->Annotate("{", "}", descriptor_); WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT, context_->options());