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 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 | 348 |
349 | 349 |
350 | 350 |
351 /** | 351 /** |
352 * Used by parsing constructors in generated classes. | 352 * Used by parsing constructors in generated classes. |
353 */ | 353 */ |
354 protected void makeExtensionsImmutable() { | 354 protected void makeExtensionsImmutable() { |
355 // Noop for messages without extensions. | 355 // Noop for messages without extensions. |
356 } | 356 } |
357 | 357 |
| 358 /** |
| 359 * TODO(xiaofeng): remove this after b/29368482 is fixed. We need to move this |
| 360 * interface to AbstractMessage in order to versioning GeneratedMessage but |
| 361 * this move breaks binary compatibility for AppEngine. After AppEngine is |
| 362 * fixed we can exlude this from google3. |
| 363 */ |
| 364 protected interface BuilderParent extends AbstractMessage.BuilderParent {} |
| 365 |
| 366 /** |
| 367 * TODO(xiaofeng): remove this together with GeneratedMessage.BuilderParent. |
| 368 */ |
358 protected abstract Message.Builder newBuilderForType(BuilderParent parent); | 369 protected abstract Message.Builder newBuilderForType(BuilderParent parent); |
359 | 370 |
360 /** | 371 @Override |
361 * Interface for the parent of a Builder that allows the builder to | 372 protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParen
t parent) { |
362 * communicate invalidations back to the parent for use when using nested | 373 return newBuilderForType(new BuilderParent() { |
363 * builders. | 374 @Override |
364 */ | 375 public void markDirty() { |
365 protected interface BuilderParent { | 376 parent.markDirty(); |
| 377 } |
| 378 }); |
| 379 } |
366 | 380 |
367 /** | |
368 * A builder becomes dirty whenever a field is modified -- including fields | |
369 * in nested builders -- and becomes clean when build() is called. Thus, | |
370 * when a builder becomes dirty, all its parents become dirty as well, and | |
371 * when it becomes clean, all its children become clean. The dirtiness | |
372 * state is used to invalidate certain cached values. | |
373 * <br> | |
374 * To this end, a builder calls markAsDirty() on its parent whenever it | |
375 * transitions from clean to dirty. The parent must propagate this call to | |
376 * its own parent, unless it was already dirty, in which case the | |
377 * grandparent must necessarily already be dirty as well. The parent can | |
378 * only transition back to "clean" after calling build() on all children. | |
379 */ | |
380 void markDirty(); | |
381 } | |
382 | 381 |
383 @SuppressWarnings("unchecked") | 382 @SuppressWarnings("unchecked") |
384 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> | 383 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> |
385 extends AbstractMessage.Builder<BuilderType> { | 384 extends AbstractMessage.Builder<BuilderType> { |
386 | 385 |
387 private BuilderParent builderParent; | 386 private BuilderParent builderParent; |
388 | 387 |
389 private BuilderParentImpl meAsParent; | 388 private BuilderParentImpl meAsParent; |
390 | 389 |
391 // Indicates that we've built a message and so we are now obligated | 390 // Indicates that we've built a message and so we are now obligated |
392 // to dispatch dirty invalidations. See GeneratedMessage.BuilderListener. | 391 // to dispatch dirty invalidations. See GeneratedMessage.BuilderListener. |
393 private boolean isClean; | 392 private boolean isClean; |
394 | 393 |
395 private UnknownFieldSet unknownFields = | 394 private UnknownFieldSet unknownFields = |
396 UnknownFieldSet.getDefaultInstance(); | 395 UnknownFieldSet.getDefaultInstance(); |
397 | 396 |
398 protected Builder() { | 397 protected Builder() { |
399 this(null); | 398 this(null); |
400 } | 399 } |
401 | 400 |
402 protected Builder(BuilderParent builderParent) { | 401 protected Builder(BuilderParent builderParent) { |
403 this.builderParent = builderParent; | 402 this.builderParent = builderParent; |
404 } | 403 } |
405 | 404 |
| 405 @Override |
406 void dispose() { | 406 void dispose() { |
407 builderParent = null; | 407 builderParent = null; |
408 } | 408 } |
409 | 409 |
410 /** | 410 /** |
411 * Called by the subclass when a message is built. | 411 * Called by the subclass when a message is built. |
412 */ | 412 */ |
413 protected void onBuilt() { | 413 protected void onBuilt() { |
414 if (builderParent != null) { | 414 if (builderParent != null) { |
415 markClean(); | 415 markClean(); |
416 } | 416 } |
417 } | 417 } |
418 | 418 |
419 /** | 419 /** |
420 * Called by the subclass or a builder to notify us that a message was | 420 * Called by the subclass or a builder to notify us that a message was |
421 * built and may be cached and therefore invalidations are needed. | 421 * built and may be cached and therefore invalidations are needed. |
422 */ | 422 */ |
| 423 @Override |
423 protected void markClean() { | 424 protected void markClean() { |
424 this.isClean = true; | 425 this.isClean = true; |
425 } | 426 } |
426 | 427 |
427 /** | 428 /** |
428 * Gets whether invalidations are needed | 429 * Gets whether invalidations are needed |
429 * | 430 * |
430 * @return whether invalidations are needed | 431 * @return whether invalidations are needed |
431 */ | 432 */ |
432 protected boolean isClean() { | 433 protected boolean isClean() { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 ExtensionLite<MessageType, List<Type>> extension); | 749 ExtensionLite<MessageType, List<Type>> extension); |
749 | 750 |
750 /** Get the value of an extension. */ | 751 /** Get the value of an extension. */ |
751 <Type> Type getExtension( | 752 <Type> Type getExtension( |
752 ExtensionLite<MessageType, Type> extension); | 753 ExtensionLite<MessageType, Type> extension); |
753 | 754 |
754 /** Get one element of a repeated extension. */ | 755 /** Get one element of a repeated extension. */ |
755 <Type> Type getExtension( | 756 <Type> Type getExtension( |
756 ExtensionLite<MessageType, List<Type>> extension, | 757 ExtensionLite<MessageType, List<Type>> extension, |
757 int index); | 758 int index); |
| 759 |
| 760 /** Check if a singular extension is present. */ |
| 761 <Type> boolean hasExtension( |
| 762 Extension<MessageType, Type> extension); |
| 763 /** Check if a singular extension is present. */ |
| 764 <Type> boolean hasExtension( |
| 765 GeneratedExtension<MessageType, Type> extension); |
| 766 /** Get the number of elements in a repeated extension. */ |
| 767 <Type> int getExtensionCount( |
| 768 Extension<MessageType, List<Type>> extension); |
| 769 /** Get the number of elements in a repeated extension. */ |
| 770 <Type> int getExtensionCount( |
| 771 GeneratedExtension<MessageType, List<Type>> extension); |
| 772 /** Get the value of an extension. */ |
| 773 <Type> Type getExtension( |
| 774 Extension<MessageType, Type> extension); |
| 775 /** Get the value of an extension. */ |
| 776 <Type> Type getExtension( |
| 777 GeneratedExtension<MessageType, Type> extension); |
| 778 /** Get one element of a repeated extension. */ |
| 779 <Type> Type getExtension( |
| 780 Extension<MessageType, List<Type>> extension, |
| 781 int index); |
| 782 /** Get one element of a repeated extension. */ |
| 783 <Type> Type getExtension( |
| 784 GeneratedExtension<MessageType, List<Type>> extension, |
| 785 int index); |
758 } | 786 } |
759 | 787 |
760 /** | 788 /** |
761 * Generated message classes for message types that contain extension ranges | 789 * Generated message classes for message types that contain extension ranges |
762 * subclass this. | 790 * subclass this. |
763 * | 791 * |
764 * <p>This class implements type-safe accessors for extensions. They | 792 * <p>This class implements type-safe accessors for extensions. They |
765 * implement all the same operations that you can do with normal fields -- | 793 * implement all the same operations that you can do with normal fields -- |
766 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions | 794 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions |
767 * are identified using instances of the class {@link GeneratedExtension}; | 795 * are identified using instances of the class {@link GeneratedExtension}; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 public final <Type> Type getExtension( | 902 public final <Type> Type getExtension( |
875 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { | 903 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { |
876 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | 904 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; |
877 | 905 |
878 verifyExtensionContainingType(extension); | 906 verifyExtensionContainingType(extension); |
879 FieldDescriptor descriptor = extension.getDescriptor(); | 907 FieldDescriptor descriptor = extension.getDescriptor(); |
880 return (Type) extension.singularFromReflectionType( | 908 return (Type) extension.singularFromReflectionType( |
881 extensions.getRepeatedField(descriptor, index)); | 909 extensions.getRepeatedField(descriptor, index)); |
882 } | 910 } |
883 | 911 |
| 912 /** Check if a singular extension is present. */ |
| 913 @Override |
| 914 public final <Type> boolean hasExtension(final Extension<MessageType, Type>
extension) { |
| 915 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 916 } |
| 917 /** Check if a singular extension is present. */ |
| 918 @Override |
| 919 public final <Type> boolean hasExtension( |
| 920 final GeneratedExtension<MessageType, Type> extension) { |
| 921 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 922 } |
| 923 /** Get the number of elements in a repeated extension. */ |
| 924 @Override |
| 925 public final <Type> int getExtensionCount( |
| 926 final Extension<MessageType, List<Type>> extension) { |
| 927 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 928 } |
| 929 /** Get the number of elements in a repeated extension. */ |
| 930 @Override |
| 931 public final <Type> int getExtensionCount( |
| 932 final GeneratedExtension<MessageType, List<Type>> extension) { |
| 933 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 934 } |
| 935 /** Get the value of an extension. */ |
| 936 @Override |
| 937 public final <Type> Type getExtension(final Extension<MessageType, Type> ext
ension) { |
| 938 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 939 } |
| 940 /** Get the value of an extension. */ |
| 941 @Override |
| 942 public final <Type> Type getExtension( |
| 943 final GeneratedExtension<MessageType, Type> extension) { |
| 944 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 945 } |
| 946 /** Get one element of a repeated extension. */ |
| 947 @Override |
| 948 public final <Type> Type getExtension( |
| 949 final Extension<MessageType, List<Type>> extension, final int index) { |
| 950 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 951 } |
| 952 /** Get one element of a repeated extension. */ |
| 953 @Override |
| 954 public final <Type> Type getExtension( |
| 955 final GeneratedExtension<MessageType, List<Type>> extension, final int i
ndex) { |
| 956 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 957 } |
| 958 |
884 /** Called by subclasses to check if all extensions are initialized. */ | 959 /** Called by subclasses to check if all extensions are initialized. */ |
885 protected boolean extensionsAreInitialized() { | 960 protected boolean extensionsAreInitialized() { |
886 return extensions.isInitialized(); | 961 return extensions.isInitialized(); |
887 } | 962 } |
888 | 963 |
889 @Override | 964 @Override |
890 public boolean isInitialized() { | 965 public boolean isInitialized() { |
891 return super.isInitialized() && extensionsAreInitialized(); | 966 return super.isInitialized() && extensionsAreInitialized(); |
892 } | 967 } |
893 | 968 |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1262 final ExtensionLite<MessageType, ?> extensionLite) { | 1337 final ExtensionLite<MessageType, ?> extensionLite) { |
1263 Extension<MessageType, ?> extension = checkNotLite(extensionLite); | 1338 Extension<MessageType, ?> extension = checkNotLite(extensionLite); |
1264 | 1339 |
1265 verifyExtensionContainingType(extension); | 1340 verifyExtensionContainingType(extension); |
1266 ensureExtensionsIsMutable(); | 1341 ensureExtensionsIsMutable(); |
1267 extensions.clearField(extension.getDescriptor()); | 1342 extensions.clearField(extension.getDescriptor()); |
1268 onChanged(); | 1343 onChanged(); |
1269 return (BuilderType) this; | 1344 return (BuilderType) this; |
1270 } | 1345 } |
1271 | 1346 |
| 1347 /** Check if a singular extension is present. */ |
| 1348 @Override |
| 1349 public final <Type> boolean hasExtension(final Extension<MessageType, Type>
extension) { |
| 1350 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 1351 } |
| 1352 /** Check if a singular extension is present. */ |
| 1353 @Override |
| 1354 public final <Type> boolean hasExtension( |
| 1355 final GeneratedExtension<MessageType, Type> extension) { |
| 1356 return hasExtension((ExtensionLite<MessageType, Type>) extension); |
| 1357 } |
| 1358 /** Get the number of elements in a repeated extension. */ |
| 1359 @Override |
| 1360 public final <Type> int getExtensionCount( |
| 1361 final Extension<MessageType, List<Type>> extension) { |
| 1362 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 1363 } |
| 1364 /** Get the number of elements in a repeated extension. */ |
| 1365 @Override |
| 1366 public final <Type> int getExtensionCount( |
| 1367 final GeneratedExtension<MessageType, List<Type>> extension) { |
| 1368 return getExtensionCount((ExtensionLite<MessageType, List<Type>>) extensio
n); |
| 1369 } |
| 1370 /** Get the value of an extension. */ |
| 1371 @Override |
| 1372 public final <Type> Type getExtension(final Extension<MessageType, Type> ext
ension) { |
| 1373 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 1374 } |
| 1375 /** Get the value of an extension. */ |
| 1376 @Override |
| 1377 public final <Type> Type getExtension( |
| 1378 final GeneratedExtension<MessageType, Type> extension) { |
| 1379 return getExtension((ExtensionLite<MessageType, Type>) extension); |
| 1380 } |
| 1381 /** Get the value of an extension. */ |
| 1382 @Override |
| 1383 public final <Type> Type getExtension( |
| 1384 final Extension<MessageType, List<Type>> extension, final int index) { |
| 1385 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 1386 } |
| 1387 /** Get the value of an extension. */ |
| 1388 @Override |
| 1389 public final <Type> Type getExtension( |
| 1390 final GeneratedExtension<MessageType, List<Type>> extension, final int i
ndex) { |
| 1391 return getExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex); |
| 1392 } |
| 1393 /** Set the value of an extension. */ |
| 1394 public final <Type> BuilderType setExtension( |
| 1395 final Extension<MessageType, Type> extension, final Type value) { |
| 1396 return setExtension((ExtensionLite<MessageType, Type>) extension, value); |
| 1397 } |
| 1398 /** Set the value of an extension. */ |
| 1399 public <Type> BuilderType setExtension( |
| 1400 final GeneratedExtension<MessageType, Type> extension, final Type value)
{ |
| 1401 return setExtension((ExtensionLite<MessageType, Type>) extension, value); |
| 1402 } |
| 1403 /** Set the value of one element of a repeated extension. */ |
| 1404 public final <Type> BuilderType setExtension( |
| 1405 final Extension<MessageType, List<Type>> extension, |
| 1406 final int index, final Type value) { |
| 1407 return setExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex, value); |
| 1408 } |
| 1409 /** Set the value of one element of a repeated extension. */ |
| 1410 public <Type> BuilderType setExtension( |
| 1411 final GeneratedExtension<MessageType, List<Type>> extension, |
| 1412 final int index, final Type value) { |
| 1413 return setExtension((ExtensionLite<MessageType, List<Type>>) extension, in
dex, value); |
| 1414 } |
| 1415 /** Append a value to a repeated extension. */ |
| 1416 public final <Type> BuilderType addExtension( |
| 1417 final Extension<MessageType, List<Type>> extension, final Type value) { |
| 1418 return addExtension((ExtensionLite<MessageType, List<Type>>) extension, va
lue); |
| 1419 } |
| 1420 /** Append a value to a repeated extension. */ |
| 1421 public <Type> BuilderType addExtension( |
| 1422 final GeneratedExtension<MessageType, List<Type>> extension, final Type
value) { |
| 1423 return addExtension((ExtensionLite<MessageType, List<Type>>) extension, va
lue); |
| 1424 } |
| 1425 /** Clear an extension. */ |
| 1426 public final <Type> BuilderType clearExtension( |
| 1427 final Extension<MessageType, ?> extension) { |
| 1428 return clearExtension((ExtensionLite<MessageType, ?>) extension); |
| 1429 } |
| 1430 /** Clear an extension. */ |
| 1431 public <Type> BuilderType clearExtension( |
| 1432 final GeneratedExtension<MessageType, ?> extension) { |
| 1433 return clearExtension((ExtensionLite<MessageType, ?>) extension); |
| 1434 } |
| 1435 |
1272 /** Called by subclasses to check if all extensions are initialized. */ | 1436 /** Called by subclasses to check if all extensions are initialized. */ |
1273 protected boolean extensionsAreInitialized() { | 1437 protected boolean extensionsAreInitialized() { |
1274 return extensions.isInitialized(); | 1438 return extensions.isInitialized(); |
1275 } | 1439 } |
1276 | 1440 |
1277 /** | 1441 /** |
1278 * Called by the build code path to create a copy of the extensions for | 1442 * Called by the build code path to create a copy of the extensions for |
1279 * building the message. | 1443 * building the message. |
1280 */ | 1444 */ |
1281 private FieldSet<FieldDescriptor> buildExtensions() { | 1445 private FieldSet<FieldDescriptor> buildExtensions() { |
(...skipping 1592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2874 | 3038 |
2875 protected static void writeStringNoTag( | 3039 protected static void writeStringNoTag( |
2876 CodedOutputStream output, final Object value) throws IOException { | 3040 CodedOutputStream output, final Object value) throws IOException { |
2877 if (value instanceof String) { | 3041 if (value instanceof String) { |
2878 output.writeStringNoTag((String) value); | 3042 output.writeStringNoTag((String) value); |
2879 } else { | 3043 } else { |
2880 output.writeBytesNoTag((ByteString) value); | 3044 output.writeBytesNoTag((ByteString) value); |
2881 } | 3045 } |
2882 } | 3046 } |
2883 } | 3047 } |
OLD | NEW |