• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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
14 // distribution.
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.
18 //
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.
30 
31 package com.google.protobuf;
32 
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.Map;
36 
37 /**
38  * Equivalent to {@link ExtensionRegistry} but supports only "lite" types.
39  * <p>
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-party 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.
49  * <p>
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:
55  * <pre>
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
62  * </pre>
63  * <p>
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.
70  *
71  * @author kenton@google.com Kenton Varda
72  */
73 public class ExtensionRegistryLite {
74 
75   // Set true to enable lazy parsing feature for MessageSet.
76   //
77   // TODO(xiangl): Now we use a global flag to control whether enable lazy
78   // parsing feature for MessageSet, which may be too crude for some
79   // applications. Need to support this feature on smaller granularity.
80   private static volatile boolean eagerlyParseMessageSets = false;
81 
isEagerlyParseMessageSets()82   public static boolean isEagerlyParseMessageSets() {
83     return eagerlyParseMessageSets;
84   }
85 
setEagerlyParseMessageSets(boolean isEagerlyParse)86   public static void setEagerlyParseMessageSets(boolean isEagerlyParse) {
87     eagerlyParseMessageSets = isEagerlyParse;
88   }
89 
90   /** Construct a new, empty instance. */
newInstance()91   public static ExtensionRegistryLite newInstance() {
92     return new ExtensionRegistryLite();
93   }
94 
95   /** Get the unmodifiable singleton empty instance. */
getEmptyRegistry()96   public static ExtensionRegistryLite getEmptyRegistry() {
97     return EMPTY;
98   }
99 
100   /** Returns an unmodifiable view of the registry. */
getUnmodifiable()101   public ExtensionRegistryLite getUnmodifiable() {
102     return new ExtensionRegistryLite(this);
103   }
104 
105   /**
106    * Find an extension by containing type and field number.
107    *
108    * @return Information about the extension if found, or {@code null}
109    *         otherwise.
110    */
111   @SuppressWarnings("unchecked")
112   public <ContainingType extends MessageLite>
113       GeneratedMessageLite.GeneratedExtension<ContainingType, ?>
findLiteExtensionByNumber( final ContainingType containingTypeDefaultInstance, final int fieldNumber)114         findLiteExtensionByNumber(
115           final ContainingType containingTypeDefaultInstance,
116           final int fieldNumber) {
117     return (GeneratedMessageLite.GeneratedExtension<ContainingType, ?>)
118       extensionsByNumber.get(
119         new ObjectIntPair(containingTypeDefaultInstance, fieldNumber));
120   }
121 
122   /** Add an extension from a lite generated file to the registry. */
add( final GeneratedMessageLite.GeneratedExtension<?, ?> extension)123   public final void add(
124       final GeneratedMessageLite.GeneratedExtension<?, ?> extension) {
125     extensionsByNumber.put(
126       new ObjectIntPair(extension.getContainingTypeDefaultInstance(),
127                         extension.getNumber()),
128       extension);
129   }
130 
131   // =================================================================
132   // Private stuff.
133 
134   // Constructors are package-private so that ExtensionRegistry can subclass
135   // this.
136 
ExtensionRegistryLite()137   ExtensionRegistryLite() {
138     this.extensionsByNumber =
139         new HashMap<ObjectIntPair,
140                     GeneratedMessageLite.GeneratedExtension<?, ?>>();
141   }
142 
ExtensionRegistryLite(ExtensionRegistryLite other)143   ExtensionRegistryLite(ExtensionRegistryLite other) {
144     if (other == EMPTY) {
145       this.extensionsByNumber = Collections.emptyMap();
146     } else {
147       this.extensionsByNumber =
148         Collections.unmodifiableMap(other.extensionsByNumber);
149     }
150   }
151 
152   private final Map<ObjectIntPair,
153                     GeneratedMessageLite.GeneratedExtension<?, ?>>
154       extensionsByNumber;
155 
ExtensionRegistryLite(boolean empty)156   private ExtensionRegistryLite(boolean empty) {
157     this.extensionsByNumber = Collections.emptyMap();
158   }
159   private static final ExtensionRegistryLite EMPTY =
160     new ExtensionRegistryLite(true);
161 
162   /** A (Object, int) pair, used as a map key. */
163   private static final class ObjectIntPair {
164     private final Object object;
165     private final int number;
166 
ObjectIntPair(final Object object, final int number)167     ObjectIntPair(final Object object, final int number) {
168       this.object = object;
169       this.number = number;
170     }
171 
172     @Override
hashCode()173     public int hashCode() {
174       return System.identityHashCode(object) * ((1 << 16) - 1) + number;
175     }
176     @Override
equals(final Object obj)177     public boolean equals(final Object obj) {
178       if (!(obj instanceof ObjectIntPair)) {
179         return false;
180       }
181       final ObjectIntPair other = (ObjectIntPair)obj;
182       return object == other.object && number == other.number;
183     }
184   }
185 }
186