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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 /** | 47 /** |
48 * A partial implementation of the {@link Message} interface which implements | 48 * A partial implementation of the {@link Message} interface which implements |
49 * as many methods of that interface as possible in terms of other methods. | 49 * as many methods of that interface as possible in terms of other methods. |
50 * | 50 * |
51 * @author kenton@google.com Kenton Varda | 51 * @author kenton@google.com Kenton Varda |
52 */ | 52 */ |
53 public abstract class AbstractMessage | 53 public abstract class AbstractMessage |
54 // TODO(dweis): Update GeneratedMessage to parameterize with MessageType and
BuilderType. | 54 // TODO(dweis): Update GeneratedMessage to parameterize with MessageType and
BuilderType. |
55 extends AbstractMessageLite | 55 extends AbstractMessageLite |
56 implements Message { | 56 implements Message { |
57 | 57 |
58 @Override | 58 @Override |
59 public boolean isInitialized() { | 59 public boolean isInitialized() { |
60 return MessageReflection.isInitialized(this); | 60 return MessageReflection.isInitialized(this); |
61 } | 61 } |
62 | 62 |
63 /** | |
64 * Interface for the parent of a Builder that allows the builder to | |
65 * communicate invalidations back to the parent for use when using nested | |
66 * builders. | |
67 */ | |
68 protected interface BuilderParent { | |
69 | |
70 /** | |
71 * A builder becomes dirty whenever a field is modified -- including fields | |
72 * in nested builders -- and becomes clean when build() is called. Thus, | |
73 * when a builder becomes dirty, all its parents become dirty as well, and | |
74 * when it becomes clean, all its children become clean. The dirtiness | |
75 * state is used to invalidate certain cached values. | |
76 * <br> | |
77 * To this end, a builder calls markDirty() on its parent whenever it | |
78 * transitions from clean to dirty. The parent must propagate this call to | |
79 * its own parent, unless it was already dirty, in which case the | |
80 * grandparent must necessarily already be dirty as well. The parent can | |
81 * only transition back to "clean" after calling build() on all children. | |
82 */ | |
83 void markDirty(); | |
84 } | |
85 | |
86 /** Create a nested builder. */ | |
87 protected Message.Builder newBuilderForType(BuilderParent parent) { | |
88 throw new UnsupportedOperationException("Nested builder is not supported for
this type."); | |
89 } | |
90 | |
91 | 63 |
92 @Override | 64 @Override |
93 public List<String> findInitializationErrors() { | 65 public List<String> findInitializationErrors() { |
94 return MessageReflection.findMissingFields(this); | 66 return MessageReflection.findMissingFields(this); |
95 } | 67 } |
96 | 68 |
97 @Override | 69 @Override |
98 public String getInitializationErrorString() { | 70 public String getInitializationErrorString() { |
99 return MessageReflection.delimitWithCommas(findInitializationErrors()); | 71 return MessageReflection.delimitWithCommas(findInitializationErrors()); |
100 } | 72 } |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 /** | 453 /** |
482 * Construct an UninitializedMessageException reporting missing fields in | 454 * Construct an UninitializedMessageException reporting missing fields in |
483 * the given message. | 455 * the given message. |
484 */ | 456 */ |
485 protected static UninitializedMessageException | 457 protected static UninitializedMessageException |
486 newUninitializedMessageException(Message message) { | 458 newUninitializedMessageException(Message message) { |
487 return new UninitializedMessageException( | 459 return new UninitializedMessageException( |
488 MessageReflection.findMissingFields(message)); | 460 MessageReflection.findMissingFields(message)); |
489 } | 461 } |
490 | 462 |
491 /** | |
492 * Used to support nested builders and called to mark this builder as clean. | |
493 * Clean builders will propagate the {@link BuilderParent#markDirty()} event | |
494 * to their parent builders, while dirty builders will not, as their parents | |
495 * should be dirty already. | |
496 * | |
497 * NOTE: Implementations that don't support nested builders don't need to | |
498 * override this method. | |
499 */ | |
500 void markClean() { | |
501 throw new IllegalStateException("Should be overridden by subclasses."); | |
502 } | |
503 | |
504 /** | |
505 * Used to support nested builders and called when this nested builder is | |
506 * no longer used by its parent builder and should release the reference | |
507 * to its parent builder. | |
508 * | |
509 * NOTE: Implementations that don't support nested builders don't need to | |
510 * override this method. | |
511 */ | |
512 void dispose() { | |
513 throw new IllegalStateException("Should be overridden by subclasses."); | |
514 } | |
515 | |
516 // =============================================================== | 463 // =============================================================== |
517 // The following definitions seem to be required in order to make javac | 464 // The following definitions seem to be required in order to make javac |
518 // not produce weird errors like: | 465 // not produce weird errors like: |
519 // | 466 // |
520 // java/com/google/protobuf/DynamicMessage.java:203: types | 467 // java/com/google/protobuf/DynamicMessage.java:203: types |
521 // com.google.protobuf.AbstractMessage.Builder< | 468 // com.google.protobuf.AbstractMessage.Builder< |
522 // com.google.protobuf.DynamicMessage.Builder> and | 469 // com.google.protobuf.DynamicMessage.Builder> and |
523 // com.google.protobuf.AbstractMessage.Builder< | 470 // com.google.protobuf.AbstractMessage.Builder< |
524 // com.google.protobuf.DynamicMessage.Builder> are incompatible; both | 471 // com.google.protobuf.DynamicMessage.Builder> are incompatible; both |
525 // define mergeFrom(com.google.protobuf.ByteString), but with unrelated | 472 // define mergeFrom(com.google.protobuf.ByteString), but with unrelated |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 } | 543 } |
597 | 544 |
598 @Override | 545 @Override |
599 public boolean mergeDelimitedFrom( | 546 public boolean mergeDelimitedFrom( |
600 final InputStream input, | 547 final InputStream input, |
601 final ExtensionRegistryLite extensionRegistry) | 548 final ExtensionRegistryLite extensionRegistry) |
602 throws IOException { | 549 throws IOException { |
603 return super.mergeDelimitedFrom(input, extensionRegistry); | 550 return super.mergeDelimitedFrom(input, extensionRegistry); |
604 } | 551 } |
605 } | 552 } |
606 | |
607 /** | |
608 * @deprecated from v3.0.0-beta-3+, for compatibility with v2.5.0 and v2.6.1 | |
609 * generated code. | |
610 */ | |
611 @Deprecated | |
612 protected static int hashLong(long n) { | |
613 return (int) (n ^ (n >>> 32)); | |
614 } | |
615 // | |
616 /** | |
617 * @deprecated from v3.0.0-beta-3+, for compatibility with v2.5.0 and v2.6.1 | |
618 * generated code. | |
619 */ | |
620 @Deprecated | |
621 protected static int hashBoolean(boolean b) { | |
622 return b ? 1231 : 1237; | |
623 } | |
624 // | |
625 /** | |
626 * @deprecated from v3.0.0-beta-3+, for compatibility with v2.5.0 and v2.6.1 | |
627 * generated code. | |
628 */ | |
629 @Deprecated | |
630 protected static int hashEnum(EnumLite e) { | |
631 return e.getNumber(); | |
632 } | |
633 // | |
634 /** | |
635 * @deprecated from v3.0.0-beta-3+, for compatibility with v2.5.0 and v2.6.1 | |
636 * generated code. | |
637 */ | |
638 @Deprecated | |
639 protected static int hashEnumList(List<? extends EnumLite> list) { | |
640 int hash = 1; | |
641 for (EnumLite e : list) { | |
642 hash = 31 * hash + hashEnum(e); | |
643 } | |
644 return hash; | |
645 } | |
646 } | 553 } |
OLD | NEW |