| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 package com.google.protobuf; | 31 package com.google.protobuf; |
| 32 | 32 |
| 33 import com.google.protobuf.Descriptors.Descriptor; | 33 import com.google.protobuf.Descriptors.Descriptor; |
| 34 import com.google.protobuf.Descriptors.FieldDescriptor; | 34 import com.google.protobuf.Descriptors.FieldDescriptor; |
| 35 |
| 35 import java.util.Collection; | 36 import java.util.Collection; |
| 36 import java.util.Collections; | 37 import java.util.Collections; |
| 37 import java.util.HashMap; | 38 import java.util.HashMap; |
| 38 import java.util.HashSet; | 39 import java.util.HashSet; |
| 39 import java.util.Map; | 40 import java.util.Map; |
| 40 import java.util.Set; | 41 import java.util.Set; |
| 41 | 42 |
| 42 /** | 43 /** |
| 43 * A table of known extensions, searchable by name or field number. When | 44 * A table of known extensions, searchable by name or field number. When |
| 44 * parsing a protocol message that might have extensions, you must provide | 45 * parsing a protocol message that might have extensions, you must provide |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 * @author kenton@google.com Kenton Varda | 94 * @author kenton@google.com Kenton Varda |
| 94 */ | 95 */ |
| 95 public class ExtensionRegistry extends ExtensionRegistryLite { | 96 public class ExtensionRegistry extends ExtensionRegistryLite { |
| 96 /** Construct a new, empty instance. */ | 97 /** Construct a new, empty instance. */ |
| 97 public static ExtensionRegistry newInstance() { | 98 public static ExtensionRegistry newInstance() { |
| 98 return new ExtensionRegistry(); | 99 return new ExtensionRegistry(); |
| 99 } | 100 } |
| 100 | 101 |
| 101 /** Get the unmodifiable singleton empty instance. */ | 102 /** Get the unmodifiable singleton empty instance. */ |
| 102 public static ExtensionRegistry getEmptyRegistry() { | 103 public static ExtensionRegistry getEmptyRegistry() { |
| 103 return EMPTY_REGISTRY; | 104 return EMPTY; |
| 104 } | 105 } |
| 105 | 106 |
| 106 | 107 |
| 107 /** Returns an unmodifiable view of the registry. */ | 108 /** Returns an unmodifiable view of the registry. */ |
| 108 @Override | 109 @Override |
| 109 public ExtensionRegistry getUnmodifiable() { | 110 public ExtensionRegistry getUnmodifiable() { |
| 110 return new ExtensionRegistry(this); | 111 return new ExtensionRegistry(this); |
| 111 } | 112 } |
| 112 | 113 |
| 113 /** A (Descriptor, Message) pair, returned by lookup methods. */ | 114 /** A (Descriptor, Message) pair, returned by lookup methods. */ |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 /** Add an extension from a generated file to the registry. */ | 236 /** Add an extension from a generated file to the registry. */ |
| 236 public void add(final Extension<?, ?> extension) { | 237 public void add(final Extension<?, ?> extension) { |
| 237 if (extension.getExtensionType() != Extension.ExtensionType.IMMUTABLE && | 238 if (extension.getExtensionType() != Extension.ExtensionType.IMMUTABLE && |
| 238 extension.getExtensionType() != Extension.ExtensionType.MUTABLE) { | 239 extension.getExtensionType() != Extension.ExtensionType.MUTABLE) { |
| 239 // do not support other extension types. ignore | 240 // do not support other extension types. ignore |
| 240 return; | 241 return; |
| 241 } | 242 } |
| 242 add(newExtensionInfo(extension), extension.getExtensionType()); | 243 add(newExtensionInfo(extension), extension.getExtensionType()); |
| 243 } | 244 } |
| 244 | 245 |
| 245 /** Add an extension from a generated file to the registry. */ | |
| 246 public void add(final GeneratedMessage.GeneratedExtension<?, ?> extension) { | |
| 247 add((Extension<?, ?>) extension); | |
| 248 } | |
| 249 | |
| 250 static ExtensionInfo newExtensionInfo(final Extension<?, ?> extension) { | 246 static ExtensionInfo newExtensionInfo(final Extension<?, ?> extension) { |
| 251 if (extension.getDescriptor().getJavaType() == | 247 if (extension.getDescriptor().getJavaType() == |
| 252 FieldDescriptor.JavaType.MESSAGE) { | 248 FieldDescriptor.JavaType.MESSAGE) { |
| 253 if (extension.getMessageDefaultInstance() == null) { | 249 if (extension.getMessageDefaultInstance() == null) { |
| 254 throw new IllegalStateException( | 250 throw new IllegalStateException( |
| 255 "Registered message-type extension had null default instance: " + | 251 "Registered message-type extension had null default instance: " + |
| 256 extension.getDescriptor().getFullName()); | 252 extension.getDescriptor().getFullName()); |
| 257 } | 253 } |
| 258 return new ExtensionInfo(extension.getDescriptor(), | 254 return new ExtensionInfo(extension.getDescriptor(), |
| 259 (Message) extension.getMessageDefaultInstance()); | 255 (Message) extension.getMessageDefaultInstance()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 this.mutableExtensionsByNumber = | 304 this.mutableExtensionsByNumber = |
| 309 Collections.unmodifiableMap(other.mutableExtensionsByNumber); | 305 Collections.unmodifiableMap(other.mutableExtensionsByNumber); |
| 310 } | 306 } |
| 311 | 307 |
| 312 private final Map<String, ExtensionInfo> immutableExtensionsByName; | 308 private final Map<String, ExtensionInfo> immutableExtensionsByName; |
| 313 private final Map<String, ExtensionInfo> mutableExtensionsByName; | 309 private final Map<String, ExtensionInfo> mutableExtensionsByName; |
| 314 private final Map<DescriptorIntPair, ExtensionInfo> immutableExtensionsByNumbe
r; | 310 private final Map<DescriptorIntPair, ExtensionInfo> immutableExtensionsByNumbe
r; |
| 315 private final Map<DescriptorIntPair, ExtensionInfo> mutableExtensionsByNumber; | 311 private final Map<DescriptorIntPair, ExtensionInfo> mutableExtensionsByNumber; |
| 316 | 312 |
| 317 ExtensionRegistry(boolean empty) { | 313 ExtensionRegistry(boolean empty) { |
| 318 super(EMPTY_REGISTRY_LITE); | 314 super(ExtensionRegistryLite.getEmptyRegistry()); |
| 319 this.immutableExtensionsByName = | 315 this.immutableExtensionsByName = |
| 320 Collections.<String, ExtensionInfo>emptyMap(); | 316 Collections.<String, ExtensionInfo>emptyMap(); |
| 321 this.mutableExtensionsByName = | 317 this.mutableExtensionsByName = |
| 322 Collections.<String, ExtensionInfo>emptyMap(); | 318 Collections.<String, ExtensionInfo>emptyMap(); |
| 323 this.immutableExtensionsByNumber = | 319 this.immutableExtensionsByNumber = |
| 324 Collections.<DescriptorIntPair, ExtensionInfo>emptyMap(); | 320 Collections.<DescriptorIntPair, ExtensionInfo>emptyMap(); |
| 325 this.mutableExtensionsByNumber = | 321 this.mutableExtensionsByNumber = |
| 326 Collections.<DescriptorIntPair, ExtensionInfo>emptyMap(); | 322 Collections.<DescriptorIntPair, ExtensionInfo>emptyMap(); |
| 327 } | 323 } |
| 328 static final ExtensionRegistry EMPTY_REGISTRY = new ExtensionRegistry(true); | 324 private static final ExtensionRegistry EMPTY = new ExtensionRegistry(true); |
| 329 | 325 |
| 330 private void add( | 326 private void add( |
| 331 final ExtensionInfo extension, | 327 final ExtensionInfo extension, |
| 332 final Extension.ExtensionType extensionType) { | 328 final Extension.ExtensionType extensionType) { |
| 333 if (!extension.descriptor.isExtension()) { | 329 if (!extension.descriptor.isExtension()) { |
| 334 throw new IllegalArgumentException( | 330 throw new IllegalArgumentException( |
| 335 "ExtensionRegistry.add() was given a FieldDescriptor for a regular " + | 331 "ExtensionRegistry.add() was given a FieldDescriptor for a regular " + |
| 336 "(non-extension) field."); | 332 "(non-extension) field."); |
| 337 } | 333 } |
| 338 | 334 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 @Override | 383 @Override |
| 388 public boolean equals(final Object obj) { | 384 public boolean equals(final Object obj) { |
| 389 if (!(obj instanceof DescriptorIntPair)) { | 385 if (!(obj instanceof DescriptorIntPair)) { |
| 390 return false; | 386 return false; |
| 391 } | 387 } |
| 392 final DescriptorIntPair other = (DescriptorIntPair)obj; | 388 final DescriptorIntPair other = (DescriptorIntPair)obj; |
| 393 return descriptor == other.descriptor && number == other.number; | 389 return descriptor == other.descriptor && number == other.number; |
| 394 } | 390 } |
| 395 } | 391 } |
| 396 } | 392 } |
| OLD | NEW |