/**
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.waveprotocol.wave.federation.proto;
import org.waveprotocol.wave.federation.ProtocolHashedVersion;
import org.waveprotocol.wave.federation.ProtocolWaveletOperation;
import org.waveprotocol.wave.federation.proto.ProtocolHashedVersionProtoImpl;
import org.waveprotocol.wave.federation.proto.ProtocolWaveletOperationProtoImpl;
import org.waveprotocol.wave.federation.ProtocolWaveletDelta;
import org.waveprotocol.wave.federation.ProtocolWaveletDeltaUtil;
import org.waveprotocol.wave.communication.Blob;
import org.waveprotocol.wave.communication.Codec;
import org.waveprotocol.wave.communication.ProtoEnums;
import org.waveprotocol.wave.communication.proto.Int52;
import org.waveprotocol.wave.communication.proto.ProtoWrapper;
import org.waveprotocol.wave.communication.gson.GsonException;
import org.waveprotocol.wave.communication.gson.GsonSerializable;
import org.waveprotocol.wave.communication.gson.GsonUtil;
import org.waveprotocol.wave.communication.json.RawStringData;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Server implementation of ProtocolWaveletDelta.
*
* Generated from org/waveprotocol/wave/federation/federation.protodevel. Do not edit.
*/
// NOTE(kalman): It would be nicer to add a proto serialisation
// utility rather than having this class at all.
public final class ProtocolWaveletDeltaProtoImpl
implements ProtocolWaveletDelta,
// Note: fully-qualified path is required for GsonSerializable and ProtoWrapper.
// An import of it is not resolved correctly from inner classes.
// This appears to be a javac bug. The Eclipse compiler handles it fine.
org.waveprotocol.wave.communication.gson.GsonSerializable,
org.waveprotocol.wave.communication.proto.ProtoWrapper<org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta> {
private org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta proto = null;
private org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta.Builder protoBuilder = org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta.newBuilder();
public ProtocolWaveletDeltaProtoImpl() {
}
public ProtocolWaveletDeltaProtoImpl(org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta proto) {
this.proto = proto;
}
public ProtocolWaveletDeltaProtoImpl(ProtocolWaveletDelta message) {
copyFrom(message);
}
@Override
public org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta getPB() {
switchToProto();
return proto;
}
@Override
public void setPB(org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta proto) {
this.proto = proto;
this.protoBuilder = null;
}
@Override
public void copyFrom(ProtocolWaveletDelta message) {
setHashedVersion(new ProtocolHashedVersionProtoImpl(message.getHashedVersion()));
setAuthor(message.getAuthor());
clearOperation();
for (ProtocolWaveletOperation field : message.getOperation()) {
addOperation(new ProtocolWaveletOperationProtoImpl(field));
}
clearAddressPath();
for (String field : message.getAddressPath()) {
addAddressPath(field);
}
}
@Override
public ProtocolHashedVersionProtoImpl getHashedVersion() {
switchToProto();
return new ProtocolHashedVersionProtoImpl(proto.getHashedVersion());
}
@Override
public void setHashedVersion(ProtocolHashedVersion value) {
switchToProtoBuilder();
protoBuilder.clearHashedVersion();
protoBuilder.setHashedVersion(getOrCreateProtocolHashedVersionProtoImpl(value).getPB());
}
@Override
public String getAuthor() {
switchToProto();
return proto.getAuthor();
}
@Override
public void setAuthor(String value) {
switchToProtoBuilder();
protoBuilder.setAuthor(value);
}
@Override
public List<ProtocolWaveletOperationProtoImpl> getOperation() {
switchToProto();
List<ProtocolWaveletOperationProtoImpl> list = new ArrayList<ProtocolWaveletOperationProtoImpl>();
for (int i = 0; i < getOperationSize(); i++) {
ProtocolWaveletOperationProtoImpl message = new ProtocolWaveletOperationProtoImpl(proto.getOperation(i));
list.add(message);
}
return list;
}
@Override
public void addAllOperation(List<? extends ProtocolWaveletOperation> values) {
for (ProtocolWaveletOperation message : values) {
addOperation(message);
}
}
@Override
public ProtocolWaveletOperationProtoImpl getOperation(int n) {
switchToProto();
return new ProtocolWaveletOperationProtoImpl(proto.getOperation(n));
}
@Override
public void setOperation(int n, ProtocolWaveletOperation value) {
switchToProtoBuilder();
protoBuilder.setOperation(n, getOrCreateProtocolWaveletOperationProtoImpl(value).getPB());
}
@Override
public int getOperationSize() {
switchToProto();
return proto.getOperationCount();
}
@Override
public void addOperation(ProtocolWaveletOperation value) {
switchToProtoBuilder();
protoBuilder.addOperation(getOrCreateProtocolWaveletOperationProtoImpl(value).getPB());
}
@Override
public void clearOperation() {
switchToProtoBuilder();
protoBuilder.clearOperation();
}
@Override
public List<String> getAddressPath() {
switchToProto();
return Collections.unmodifiableList(proto.getAddressPathList());
}
@Override
public void addAllAddressPath(List<String> values) {
switchToProtoBuilder();
protoBuilder.addAllAddressPath(values);
}
@Override
public String getAddressPath(int n) {
switchToProto();
return proto.getAddressPath(n);
}
@Override
public void setAddressPath(int n, String value) {
switchToProtoBuilder();
protoBuilder.setAddressPath(n, value);
}
@Override
public int getAddressPathSize() {
switchToProto();
return proto.getAddressPathCount();
}
@Override
public void addAddressPath(String value) {
switchToProtoBuilder();
protoBuilder.addAddressPath(value);
}
@Override
public void clearAddressPath() {
switchToProtoBuilder();
protoBuilder.clearAddressPath();
}
/** Get or create a ProtocolHashedVersionProtoImpl from a ProtocolHashedVersion. */
private ProtocolHashedVersionProtoImpl getOrCreateProtocolHashedVersionProtoImpl(ProtocolHashedVersion message) {
if (message instanceof ProtocolHashedVersionProtoImpl) {
return (ProtocolHashedVersionProtoImpl) message;
} else {
ProtocolHashedVersionProtoImpl messageImpl = new ProtocolHashedVersionProtoImpl();
messageImpl.copyFrom(message);
return messageImpl;
}
}
/** Get or create a ProtocolWaveletOperationProtoImpl from a ProtocolWaveletOperation. */
private ProtocolWaveletOperationProtoImpl getOrCreateProtocolWaveletOperationProtoImpl(ProtocolWaveletOperation message) {
if (message instanceof ProtocolWaveletOperationProtoImpl) {
return (ProtocolWaveletOperationProtoImpl) message;
} else {
ProtocolWaveletOperationProtoImpl messageImpl = new ProtocolWaveletOperationProtoImpl();
messageImpl.copyFrom(message);
return messageImpl;
}
}
private void switchToProto() {
if (proto == null) {
proto = protoBuilder.build();
protoBuilder = null;
}
}
private void switchToProtoBuilder() {
if (protoBuilder == null) {
protoBuilder = (proto == null)
? org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta.newBuilder()
: org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta.newBuilder(proto);
proto = null;
}
}
private void invalidateAll() {
proto = null;
protoBuilder = org.waveprotocol.wave.federation.Proto.ProtocolWaveletDelta.newBuilder();
}
@Override
public JsonElement toGson(RawStringData raw, Gson gson) {
JsonObject json = new JsonObject();
{
JsonElement elem = ((GsonSerializable) getHashedVersion()).toGson(raw, gson);
json.add("1", elem);
}
json.add("2", new JsonPrimitive(getAuthor()));
{
JsonArray array = new JsonArray();
for (int i = 0; i < getOperationSize(); i++) {
JsonElement elem = ((GsonSerializable) getOperation(i)).toGson(raw, gson);
// NOTE(kalman): if multistage parsing worked, split point would go here.
array.add(elem);
}
json.add("3", array);
}
{
JsonArray array = new JsonArray();
for (int i = 0; i < getAddressPathSize(); i++) {
array.add(new JsonPrimitive(getAddressPath(i)));
}
json.add("4", array);
}
return json;
}
@Override
public void fromGson(JsonElement json, Gson gson, RawStringData raw) throws GsonException {
JsonObject jsonObject = json.getAsJsonObject();
// NOTE: always check with has(...) as the json might not have all required
// fields set; however these (obviously) will need to be set by other means
// before accessing this object.
invalidateAll();
if (jsonObject.has("1")) {
JsonElement elem = jsonObject.get("1");
{
ProtocolHashedVersionProtoImpl payload = new ProtocolHashedVersionProtoImpl();
GsonUtil.extractJsonObject(payload, elem, gson, raw);
setHashedVersion(payload);
}
}
if (jsonObject.has("2")) {
JsonElement elem = jsonObject.get("2");
setAuthor(elem.getAsString());
}
if (jsonObject.has("3")) {
JsonElement elem = jsonObject.get("3");
{
JsonArray array = elem.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
ProtocolWaveletOperationProtoImpl payload = new ProtocolWaveletOperationProtoImpl();
GsonUtil.extractJsonObject(payload, array.get(i), gson, raw);
addOperation(payload);
}
}
}
if (jsonObject.has("4")) {
JsonElement elem = jsonObject.get("4");
{
JsonArray array = elem.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
addAddressPath(array.get(i).getAsString());
}
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof ProtocolWaveletDeltaProtoImpl) {
return getPB().equals(((ProtocolWaveletDeltaProtoImpl) o).getPB());
} else {
return false;
}
}
@Override
public boolean isEqualTo(Object o) {
if (equals(o)) {
return true;
} else if (o instanceof ProtocolWaveletDelta) {
return ProtocolWaveletDeltaUtil.isEqual(this, (ProtocolWaveletDelta) o);
} else {
return false;
}
}
@Override
public int hashCode() {
return getPB().hashCode();
}
@Override
public String toString() {
return getPB().toString();
}
}