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.List; 34 import java.util.Map; 35 36 /** 37 * Base interface for methods common to {@link Message} and 38 * {@link Message.Builder} to provide type equivalency. 39 * 40 * @author jonp (at) google.com (Jon Perlow) 41 */ 42 public interface MessageOrBuilder extends MessageLiteOrBuilder { 43 44 // (From MessageLite, re-declared here only for return type covariance.) 45 //@Override (Java 1.6 override semantics, but we must support 1.5) 46 Message getDefaultInstanceForType(); 47 48 /** 49 * Returns a list of field paths (e.g. "foo.bar.baz") of required fields 50 * which are not set in this message. You should call 51 * {@link MessageLiteOrBuilder#isInitialized()} first to check if there 52 * are any missing fields, as that method is likely to be much faster 53 * than this one even when the message is fully-initialized. 54 */ 55 List<String> findInitializationErrors(); 56 57 /** 58 * Returns a comma-delimited list of required fields which are not set 59 * in this message object. You should call 60 * {@link MessageLiteOrBuilder#isInitialized()} first to check if there 61 * are any missing fields, as that method is likely to be much faster 62 * than this one even when the message is fully-initialized. 63 */ 64 String getInitializationErrorString(); 65 66 /** 67 * Get the message's type's descriptor. This differs from the 68 * {@code getDescriptor()} method of generated message classes in that 69 * this method is an abstract method of the {@code Message} interface 70 * whereas {@code getDescriptor()} is a static method of a specific class. 71 * They return the same thing. 72 */ 73 Descriptors.Descriptor getDescriptorForType(); 74 75 /** 76 * Returns a collection of all the fields in this message which are set 77 * and their corresponding values. A singular ("required" or "optional") 78 * field is set iff hasField() returns true for that field. A "repeated" 79 * field is set iff getRepeatedFieldSize() is greater than zero. The 80 * values are exactly what would be returned by calling 81 * {@link #getField(Descriptors.FieldDescriptor)} for each field. The map 82 * is guaranteed to be a sorted map, so iterating over it will return fields 83 * in order by field number. 84 * <br> 85 * If this is for a builder, the returned map may or may not reflect future 86 * changes to the builder. Either way, the returned map is itself 87 * unmodifiable. 88 */ 89 Map<Descriptors.FieldDescriptor, Object> getAllFields(); 90 91 /** 92 * Returns true if the given field is set. This is exactly equivalent to 93 * calling the generated "has" accessor method corresponding to the field. 94 * @throws IllegalArgumentException The field is a repeated field, or 95 * {@code field.getContainingType() != getDescriptorForType()}. 96 */ 97 boolean hasField(Descriptors.FieldDescriptor field); 98 99 /** 100 * Obtains the value of the given field, or the default value if it is 101 * not set. For primitive fields, the boxed primitive value is returned. 102 * For enum fields, the EnumValueDescriptor for the value is returned. For 103 * embedded message fields, the sub-message is returned. For repeated 104 * fields, a java.util.List is returned. 105 */ 106 Object getField(Descriptors.FieldDescriptor field); 107 108 /** 109 * Gets the number of elements of a repeated field. This is exactly 110 * equivalent to calling the generated "Count" accessor method corresponding 111 * to the field. 112 * @throws IllegalArgumentException The field is not a repeated field, or 113 * {@code field.getContainingType() != getDescriptorForType()}. 114 */ 115 int getRepeatedFieldCount(Descriptors.FieldDescriptor field); 116 117 /** 118 * Gets an element of a repeated field. For primitive fields, the boxed 119 * primitive value is returned. For enum fields, the EnumValueDescriptor 120 * for the value is returned. For embedded message fields, the sub-message 121 * is returned. 122 * @throws IllegalArgumentException The field is not a repeated field, or 123 * {@code field.getContainingType() != getDescriptorForType()}. 124 */ 125 Object getRepeatedField(Descriptors.FieldDescriptor field, int index); 126 127 /** Get the {@link UnknownFieldSet} for this message. */ 128 UnknownFieldSet getUnknownFields(); 129 } 130