diff options
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java')
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java | 88 |
1 files changed, 39 insertions, 49 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java index f3d48d3a..0ce5f549 100644 --- a/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java +++ b/java/core/src/main/java/com/google/protobuf/ExtensionRegistryLite.java @@ -36,22 +36,20 @@ import java.util.Map; /** * Equivalent to {@link ExtensionRegistry} but supports only "lite" types. - * <p> - * If all of your types are lite types, then you only need to use - * {@code ExtensionRegistryLite}. Similarly, if all your types are regular - * types, then you only need {@link ExtensionRegistry}. Typically it does not - * make sense to mix the two, since if you have any regular types in your - * program, you then require the full runtime and lose all the benefits of - * the lite runtime, so you might as well make all your types be regular types. - * However, in some cases (e.g. when depending on multiple third-party libraries - * where one uses lite types and one uses regular), you may find yourself - * wanting to mix the two. In this case things get more complicated. - * <p> - * There are three factors to consider: Whether the type being extended is - * lite, whether the embedded type (in the case of a message-typed extension) - * is lite, and whether the extension itself is lite. Since all three are - * declared in different files, they could all be different. Here are all - * the combinations and which type of registry to use: + * + * <p>If all of your types are lite types, then you only need to use {@code ExtensionRegistryLite}. + * Similarly, if all your types are regular types, then you only need {@link ExtensionRegistry}. + * Typically it does not make sense to mix the two, since if you have any regular types in your + * program, you then require the full runtime and lose all the benefits of the lite runtime, so you + * might as well make all your types be regular types. However, in some cases (e.g. when depending + * on multiple third-party libraries where one uses lite types and one uses regular), you may find + * yourself wanting to mix the two. In this case things get more complicated. + * + * <p>There are three factors to consider: Whether the type being extended is lite, whether the + * embedded type (in the case of a message-typed extension) is lite, and whether the extension + * itself is lite. Since all three are declared in different files, they could all be different. + * Here are all the combinations and which type of registry to use: + * * <pre> * Extended type Inner type Extension Use registry * ======================================================================= @@ -60,13 +58,12 @@ import java.util.Map; * regular regular regular ExtensionRegistry * all other combinations not supported * </pre> - * <p> - * Note that just as regular types are not allowed to contain lite-type fields, - * they are also not allowed to contain lite-type extensions. This is because - * regular types must be fully accessible via reflection, which in turn means - * that all the inner messages must also support reflection. On the other hand, - * since regular types implement the entire lite interface, there is no problem - * with embedding regular types inside lite types. + * + * <p>Note that just as regular types are not allowed to contain lite-type fields, they are also not + * allowed to contain lite-type extensions. This is because regular types must be fully accessible + * via reflection, which in turn means that all the inner messages must also support reflection. On + * the other hand, since regular types implement the entire lite interface, there is no problem with + * embedding regular types inside lite types. * * @author kenton@google.com Kenton Varda */ @@ -114,8 +111,8 @@ public class ExtensionRegistryLite { } /** - * Get the unmodifiable singleton empty instance of either ExtensionRegistryLite or - * {@code ExtensionRegistry} (if the full (non-Lite) proto libraries are available). + * Get the unmodifiable singleton empty instance of either ExtensionRegistryLite or {@code + * ExtensionRegistry} (if the full (non-Lite) proto libraries are available). */ public static ExtensionRegistryLite getEmptyRegistry() { return ExtensionRegistryFactory.createEmpty(); @@ -130,32 +127,27 @@ public class ExtensionRegistryLite { /** * Find an extension by containing type and field number. * - * @return Information about the extension if found, or {@code null} - * otherwise. + * @return Information about the extension if found, or {@code null} otherwise. */ @SuppressWarnings("unchecked") public <ContainingType extends MessageLite> - GeneratedMessageLite.GeneratedExtension<ContainingType, ?> - findLiteExtensionByNumber( - final ContainingType containingTypeDefaultInstance, - final int fieldNumber) { + GeneratedMessageLite.GeneratedExtension<ContainingType, ?> findLiteExtensionByNumber( + final ContainingType containingTypeDefaultInstance, final int fieldNumber) { return (GeneratedMessageLite.GeneratedExtension<ContainingType, ?>) - extensionsByNumber.get( - new ObjectIntPair(containingTypeDefaultInstance, fieldNumber)); + extensionsByNumber.get(new ObjectIntPair(containingTypeDefaultInstance, fieldNumber)); } /** Add an extension from a lite generated file to the registry. */ - public final void add( - final GeneratedMessageLite.GeneratedExtension<?, ?> extension) { + public final void add(final GeneratedMessageLite.GeneratedExtension<?, ?> extension) { extensionsByNumber.put( - new ObjectIntPair(extension.getContainingTypeDefaultInstance(), - extension.getNumber()), - extension); + new ObjectIntPair(extension.getContainingTypeDefaultInstance(), extension.getNumber()), + extension); } /** - * Add an extension from a lite generated file to the registry only if it is - * a non-lite extension i.e. {@link GeneratedMessageLite.GeneratedExtension}. */ + * Add an extension from a lite generated file to the registry only if it is a non-lite extension + * i.e. {@link GeneratedMessageLite.GeneratedExtension}. + */ public final void add(ExtensionLite<?, ?> extension) { if (GeneratedMessageLite.GeneratedExtension.class.isAssignableFrom(extension.getClass())) { add((GeneratedMessageLite.GeneratedExtension<?, ?>) extension); @@ -178,23 +170,20 @@ public class ExtensionRegistryLite { ExtensionRegistryLite() { this.extensionsByNumber = - new HashMap<ObjectIntPair, - GeneratedMessageLite.GeneratedExtension<?, ?>>(); + new HashMap<ObjectIntPair, GeneratedMessageLite.GeneratedExtension<?, ?>>(); } - static final ExtensionRegistryLite EMPTY_REGISTRY_LITE = - new ExtensionRegistryLite(true); + + static final ExtensionRegistryLite EMPTY_REGISTRY_LITE = new ExtensionRegistryLite(true); ExtensionRegistryLite(ExtensionRegistryLite other) { if (other == EMPTY_REGISTRY_LITE) { this.extensionsByNumber = Collections.emptyMap(); } else { - this.extensionsByNumber = - Collections.unmodifiableMap(other.extensionsByNumber); + this.extensionsByNumber = Collections.unmodifiableMap(other.extensionsByNumber); } } - private final Map<ObjectIntPair, - GeneratedMessageLite.GeneratedExtension<?, ?>> + private final Map<ObjectIntPair, GeneratedMessageLite.GeneratedExtension<?, ?>> extensionsByNumber; ExtensionRegistryLite(boolean empty) { @@ -215,12 +204,13 @@ public class ExtensionRegistryLite { public int hashCode() { return System.identityHashCode(object) * ((1 << 16) - 1) + number; } + @Override public boolean equals(final Object obj) { if (!(obj instanceof ObjectIntPair)) { return false; } - final ObjectIntPair other = (ObjectIntPair)obj; + final ObjectIntPair other = (ObjectIntPair) obj; return object == other.object && number == other.number; } } |