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 /** | 358 protected abstract Message.Builder newBuilderForType(BuilderParent parent); |
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 | 359 |
366 /** | 360 /** |
367 * TODO(xiaofeng): remove this together with GeneratedMessage.BuilderParent. | 361 * Interface for the parent of a Builder that allows the builder to |
| 362 * communicate invalidations back to the parent for use when using nested |
| 363 * builders. |
368 */ | 364 */ |
369 protected abstract Message.Builder newBuilderForType(BuilderParent parent); | 365 protected interface BuilderParent { |
370 | 366 |
371 @Override | 367 /** |
372 protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParen
t parent) { | 368 * A builder becomes dirty whenever a field is modified -- including fields |
373 return newBuilderForType(new BuilderParent() { | 369 * in nested builders -- and becomes clean when build() is called. Thus, |
374 @Override | 370 * when a builder becomes dirty, all its parents become dirty as well, and |
375 public void markDirty() { | 371 * when it becomes clean, all its children become clean. The dirtiness |
376 parent.markDirty(); | 372 * state is used to invalidate certain cached values. |
377 } | 373 * <br> |
378 }); | 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(); |
379 } | 381 } |
380 | 382 |
381 | |
382 @SuppressWarnings("unchecked") | 383 @SuppressWarnings("unchecked") |
383 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> | 384 public abstract static class Builder <BuilderType extends Builder<BuilderType>
> |
384 extends AbstractMessage.Builder<BuilderType> { | 385 extends AbstractMessage.Builder<BuilderType> { |
385 | 386 |
386 private BuilderParent builderParent; | 387 private BuilderParent builderParent; |
387 | 388 |
388 private BuilderParentImpl meAsParent; | 389 private BuilderParentImpl meAsParent; |
389 | 390 |
390 // Indicates that we've built a message and so we are now obligated | 391 // Indicates that we've built a message and so we are now obligated |
391 // to dispatch dirty invalidations. See GeneratedMessage.BuilderListener. | 392 // to dispatch dirty invalidations. See GeneratedMessage.BuilderListener. |
392 private boolean isClean; | 393 private boolean isClean; |
393 | 394 |
394 private UnknownFieldSet unknownFields = | 395 private UnknownFieldSet unknownFields = |
395 UnknownFieldSet.getDefaultInstance(); | 396 UnknownFieldSet.getDefaultInstance(); |
396 | 397 |
397 protected Builder() { | 398 protected Builder() { |
398 this(null); | 399 this(null); |
399 } | 400 } |
400 | 401 |
401 protected Builder(BuilderParent builderParent) { | 402 protected Builder(BuilderParent builderParent) { |
402 this.builderParent = builderParent; | 403 this.builderParent = builderParent; |
403 } | 404 } |
404 | 405 |
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 | |
424 protected void markClean() { | 423 protected void markClean() { |
425 this.isClean = true; | 424 this.isClean = true; |
426 } | 425 } |
427 | 426 |
428 /** | 427 /** |
429 * Gets whether invalidations are needed | 428 * Gets whether invalidations are needed |
430 * | 429 * |
431 * @return whether invalidations are needed | 430 * @return whether invalidations are needed |
432 */ | 431 */ |
433 protected boolean isClean() { | 432 protected boolean isClean() { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 ExtensionLite<MessageType, List<Type>> extension); | 748 ExtensionLite<MessageType, List<Type>> extension); |
750 | 749 |
751 /** Get the value of an extension. */ | 750 /** Get the value of an extension. */ |
752 <Type> Type getExtension( | 751 <Type> Type getExtension( |
753 ExtensionLite<MessageType, Type> extension); | 752 ExtensionLite<MessageType, Type> extension); |
754 | 753 |
755 /** Get one element of a repeated extension. */ | 754 /** Get one element of a repeated extension. */ |
756 <Type> Type getExtension( | 755 <Type> Type getExtension( |
757 ExtensionLite<MessageType, List<Type>> extension, | 756 ExtensionLite<MessageType, List<Type>> extension, |
758 int index); | 757 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); | |
786 } | 758 } |
787 | 759 |
788 /** | 760 /** |
789 * Generated message classes for message types that contain extension ranges | 761 * Generated message classes for message types that contain extension ranges |
790 * subclass this. | 762 * subclass this. |
791 * | 763 * |
792 * <p>This class implements type-safe accessors for extensions. They | 764 * <p>This class implements type-safe accessors for extensions. They |
793 * implement all the same operations that you can do with normal fields -- | 765 * implement all the same operations that you can do with normal fields -- |
794 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions | 766 * e.g. "has", "get", and "getCount" -- but for extensions. The extensions |
795 * are identified using instances of the class {@link GeneratedExtension}; | 767 * are identified using instances of the class {@link GeneratedExtension}; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 public final <Type> Type getExtension( | 874 public final <Type> Type getExtension( |
903 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { | 875 final ExtensionLite<MessageType, List<Type>> extensionLite, final int in
dex) { |
904 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; | 876 Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite)
; |
905 | 877 |
906 verifyExtensionContainingType(extension); | 878 verifyExtensionContainingType(extension); |
907 FieldDescriptor descriptor = extension.getDescriptor(); | 879 FieldDescriptor descriptor = extension.getDescriptor(); |
908 return (Type) extension.singularFromReflectionType( | 880 return (Type) extension.singularFromReflectionType( |
909 extensions.getRepeatedField(descriptor, index)); | 881 extensions.getRepeatedField(descriptor, index)); |
910 } | 882 } |
911 | 883 |
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 | |
959 /** Called by subclasses to check if all extensions are initialized. */ | 884 /** Called by subclasses to check if all extensions are initialized. */ |
960 protected boolean extensionsAreInitialized() { | 885 protected boolean extensionsAreInitialized() { |
961 return extensions.isInitialized(); | 886 return extensions.isInitialized(); |
962 } | 887 } |
963 | 888 |
964 @Override | 889 @Override |
965 public boolean isInitialized() { | 890 public boolean isInitialized() { |
966 return super.isInitialized() && extensionsAreInitialized(); | 891 return super.isInitialized() && extensionsAreInitialized(); |
967 } | 892 } |
968 | 893 |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1337 final ExtensionLite<MessageType, ?> extensionLite) { | 1262 final ExtensionLite<MessageType, ?> extensionLite) { |
1338 Extension<MessageType, ?> extension = checkNotLite(extensionLite); | 1263 Extension<MessageType, ?> extension = checkNotLite(extensionLite); |
1339 | 1264 |
1340 verifyExtensionContainingType(extension); | 1265 verifyExtensionContainingType(extension); |
1341 ensureExtensionsIsMutable(); | 1266 ensureExtensionsIsMutable(); |
1342 extensions.clearField(extension.getDescriptor()); | 1267 extensions.clearField(extension.getDescriptor()); |
1343 onChanged(); | 1268 onChanged(); |
1344 return (BuilderType) this; | 1269 return (BuilderType) this; |
1345 } | 1270 } |
1346 | 1271 |
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 | |
1436 /** Called by subclasses to check if all extensions are initialized. */ | 1272 /** Called by subclasses to check if all extensions are initialized. */ |
1437 protected boolean extensionsAreInitialized() { | 1273 protected boolean extensionsAreInitialized() { |
1438 return extensions.isInitialized(); | 1274 return extensions.isInitialized(); |
1439 } | 1275 } |
1440 | 1276 |
1441 /** | 1277 /** |
1442 * Called by the build code path to create a copy of the extensions for | 1278 * Called by the build code path to create a copy of the extensions for |
1443 * building the message. | 1279 * building the message. |
1444 */ | 1280 */ |
1445 private FieldSet<FieldDescriptor> buildExtensions() { | 1281 private FieldSet<FieldDescriptor> buildExtensions() { |
(...skipping 1592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3038 | 2874 |
3039 protected static void writeStringNoTag( | 2875 protected static void writeStringNoTag( |
3040 CodedOutputStream output, final Object value) throws IOException { | 2876 CodedOutputStream output, final Object value) throws IOException { |
3041 if (value instanceof String) { | 2877 if (value instanceof String) { |
3042 output.writeStringNoTag((String) value); | 2878 output.writeStringNoTag((String) value); |
3043 } else { | 2879 } else { |
3044 output.writeBytesNoTag((ByteString) value); | 2880 output.writeBytesNoTag((ByteString) value); |
3045 } | 2881 } |
3046 } | 2882 } |
3047 } | 2883 } |
OLD | NEW |