1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 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-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. 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 (at) google.com Kenton Varda 72 */ 73 public class ExtensionRegistryLite { 74 /** Construct a new, empty instance. */ 75 public static ExtensionRegistryLite newInstance() { 76 return new ExtensionRegistryLite(); 77 } 78 79 /** Get the unmodifiable singleton empty instance. */ 80 public static ExtensionRegistryLite getEmptyRegistry() { 81 return EMPTY; 82 } 83 84 /** Returns an unmodifiable view of the registry. */ 85 public ExtensionRegistryLite getUnmodifiable() { 86 return new ExtensionRegistryLite(this); 87 } 88 89 /** 90 * Find an extension by containing type and field number. 91 * 92 * @return Information about the extension if found, or {@code null} 93 * otherwise. 94 */ 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)); 104 } 105 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()), 112 extension); 113 } 114 115 // ================================================================= 116 // Private stuff. 117 118 // Constructors are package-private so that ExtensionRegistry can subclass 119 // this. 120 121 ExtensionRegistryLite() { 122 this.extensionsByNumber = 123 new HashMap<ObjectIntPair, 124 GeneratedMessageLite.GeneratedExtension<?, ?>>(); 125 } 126 127 ExtensionRegistryLite(ExtensionRegistryLite other) { 128 if (other == EMPTY) { 129 this.extensionsByNumber = Collections.emptyMap(); 130 } else { 131 this.extensionsByNumber = 132 Collections.unmodifiableMap(other.extensionsByNumber); 133 } 134 } 135 136 private final Map<ObjectIntPair, 137 GeneratedMessageLite.GeneratedExtension<?, ?>> 138 extensionsByNumber; 139 140 private ExtensionRegistryLite(boolean empty) { 141 this.extensionsByNumber = Collections.emptyMap(); 142 } 143 private static final ExtensionRegistryLite EMPTY = 144 new ExtensionRegistryLite(true); 145 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; 150 151 ObjectIntPair(final Object object, final int number) { 152 this.object = object; 153 this.number = number; 154 } 155 156 @Override 157 public int hashCode() { 158 return System.identityHashCode(object) * ((1 << 16) - 1) + number; 159 } 160 @Override 161 public boolean equals(final Object obj) { 162 if (!(obj instanceof ObjectIntPair)) { 163 return false; 164 } 165 final ObjectIntPair other = (ObjectIntPair)obj; 166 return object == other.object && number == other.number; 167 } 168 } 169 } 170