1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 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 com.google.protobuf.ArrayDecoders.Registers; 34 import java.io.IOException; 35 36 /** 37 * A runtime schema for a single protobuf message. A schema provides operations on message instances 38 * such as serialization/deserialization. 39 */ 40 @ExperimentalApi 41 interface Schema<T> { 42 /** Writes the given message to the target {@link Writer}. */ writeTo(T message, Writer writer)43 void writeTo(T message, Writer writer) throws IOException; 44 45 /** 46 * Reads fields from the given {@link Reader} and merges them into the message. It doesn't make 47 * the message immutable after parsing is done. To make the message immutable, use {@link 48 * #makeImmutable}. 49 */ mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry)50 void mergeFrom(T message, Reader reader, ExtensionRegistryLite extensionRegistry) 51 throws IOException; 52 53 /** 54 * Like the above but parses from a byte[] without extensions. Entry point of fast path. Note that 55 * this method may throw IndexOutOfBoundsException if the input data is not valid protobuf wire 56 * format. Protobuf public API methods should catch and convert that exception to 57 * InvalidProtocolBufferException. 58 */ mergeFrom(T message, byte[] data, int position, int limit, Registers registers)59 void mergeFrom(T message, byte[] data, int position, int limit, Registers registers) 60 throws IOException; 61 62 /** Marks repeated/map/extension/unknown fields as immutable. */ makeImmutable(T message)63 void makeImmutable(T message); 64 65 /** Checks whether all required fields are set. */ isInitialized(T message)66 boolean isInitialized(T message); 67 68 /** Creates a new instance of the message class. */ newInstance()69 T newInstance(); 70 71 /** Determine of the two messages are equal. */ equals(T message, T other)72 boolean equals(T message, T other); 73 74 /** Compute a hashCode for the message. */ hashCode(T message)75 int hashCode(T message); 76 77 /** 78 * Merge values from {@code other} into {@code message}. This method doesn't make the message 79 * immutable. To make the message immutable after merging, use {@link #makeImmutable}. 80 */ mergeFrom(T message, T other)81 void mergeFrom(T message, T other); 82 83 /** Compute the serialized size of the message. */ getSerializedSize(T message)84 int getSerializedSize(T message); 85 } 86