1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
9
// * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
15
// * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
package com.google.protobuf;
33
import java.util.Collections;
34
import java.util.HashMap;
38
* Equivalent to {@link ExtensionRegistry} but supports only "lite" types.
40
* If all of your types are lite types, then you only need to use
41
* {@code ExtensionRegistryLite}. Similarly, if all your types are regular
42
* types, then you only need {@link ExtensionRegistry}. Typically it does not
43
* make sense to mix the two, since if you have any regular types in your
44
* program, you then require the full runtime and lose all the benefits of
45
* the lite runtime, so you might as well make all your types be regular types.
46
* However, in some cases (e.g. when depending on multiple third-patry libraries
47
* where one uses lite types and one uses regular), you may find yourself
48
* wanting to mix the two. In this case things get more complicated.
50
* There are three factors to consider: Whether the type being extended is
51
* lite, whether the embedded type (in the case of a message-typed extension)
52
* is lite, and whether the extension itself is lite. Since all three are
53
* declared in different files, they could all be different. Here are all
54
* the combinations and which type of registry to use:
56
* Extended type Inner type Extension Use registry
57
* =======================================================================
58
* lite lite lite ExtensionRegistryLite
59
* lite regular lite ExtensionRegistry
60
* regular regular regular ExtensionRegistry
61
* all other combinations not supported
64
* Note that just as regular types are not allowed to contain lite-type fields,
65
* they are also not allowed to contain lite-type extensions. This is because
66
* regular types must be fully accessible via reflection, which in turn means
67
* that all the inner messages must also support reflection. On the other hand,
68
* since regular types implement the entire lite interface, there is no problem
69
* with embedding regular types inside lite types.
71
* @author kenton@google.com Kenton Varda
73
public class ExtensionRegistryLite {
74
/** Construct a new, empty instance. */
75
public static ExtensionRegistryLite newInstance() {
76
return new ExtensionRegistryLite();
79
/** Get the unmodifiable singleton empty instance. */
80
public static ExtensionRegistryLite getEmptyRegistry() {
84
/** Returns an unmodifiable view of the registry. */
85
public ExtensionRegistryLite getUnmodifiable() {
86
return new ExtensionRegistryLite(this);
90
* Find an extension by containing type and field number.
92
* @return Information about the extension if found, or {@code null}
95
@SuppressWarnings("unchecked")
96
public <ContainingType extends MessageLite>
97
GeneratedMessageLite.GeneratedExtension<ContainingType, ?>
98
findLiteExtensionByNumber(
99
final ContainingType containingTypeDefaultInstance,
100
final int fieldNumber) {
101
return (GeneratedMessageLite.GeneratedExtension<ContainingType, ?>)
102
extensionsByNumber.get(
103
new ObjectIntPair(containingTypeDefaultInstance, fieldNumber));
106
/** Add an extension from a lite generated file to the registry. */
107
public final void add(
108
final GeneratedMessageLite.GeneratedExtension<?, ?> extension) {
109
extensionsByNumber.put(
110
new ObjectIntPair(extension.getContainingTypeDefaultInstance(),
111
extension.getNumber()),
115
// =================================================================
118
// Constructors are package-private so that ExtensionRegistry can subclass
121
ExtensionRegistryLite() {
122
this.extensionsByNumber =
123
new HashMap<ObjectIntPair,
124
GeneratedMessageLite.GeneratedExtension<?, ?>>();
127
ExtensionRegistryLite(ExtensionRegistryLite other) {
128
if (other == EMPTY) {
129
this.extensionsByNumber = Collections.emptyMap();
131
this.extensionsByNumber =
132
Collections.unmodifiableMap(other.extensionsByNumber);
136
private final Map<ObjectIntPair,
137
GeneratedMessageLite.GeneratedExtension<?, ?>>
140
private ExtensionRegistryLite(boolean empty) {
141
this.extensionsByNumber = Collections.emptyMap();
143
private static final ExtensionRegistryLite EMPTY =
144
new ExtensionRegistryLite(true);
146
/** A (Object, int) pair, used as a map key. */
147
private static final class ObjectIntPair {
148
private final Object object;
149
private final int number;
151
ObjectIntPair(final Object object, final int number) {
152
this.object = object;
153
this.number = number;
157
public int hashCode() {
158
return System.identityHashCode(object) * ((1 << 16) - 1) + number;
161
public boolean equals(final Object obj) {
162
if (!(obj instanceof ObjectIntPair)) {
165
final ObjectIntPair other = (ObjectIntPair)obj;
166
return object == other.object && number == other.number;