Package org.waveprotocol.box.common.comms.proto

Source Code of org.waveprotocol.box.common.comms.proto.WaveViewSnapshotProtoImpl

/**
* 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.box.common.comms.proto;

import org.waveprotocol.box.common.comms.WaveletSnapshot;
import org.waveprotocol.box.common.comms.proto.WaveletSnapshotProtoImpl;
import org.waveprotocol.box.common.comms.WaveViewSnapshot;
import org.waveprotocol.box.common.comms.WaveViewSnapshotUtil;
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 WaveViewSnapshot.
*
* Generated from org/waveprotocol/box/common/comms/waveclient-rpc.proto. 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 WaveViewSnapshotProtoImpl
    implements WaveViewSnapshot,
// 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.box.common.comms.WaveClientRpc.WaveViewSnapshot> {
  private org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot proto = null;
  private org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot.Builder protoBuilder = org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot.newBuilder();
  public WaveViewSnapshotProtoImpl() {
  }

  public WaveViewSnapshotProtoImpl(org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot proto) {
    this.proto = proto;
  }

  public WaveViewSnapshotProtoImpl(WaveViewSnapshot message) {
    copyFrom(message);
  }

  @Override
  public org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot getPB() {
    switchToProto();
    return proto;
  }

  @Override
  public void setPB(org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot proto) {
    this.proto = proto;
    this.protoBuilder = null;
  }

  @Override
  public void copyFrom(WaveViewSnapshot message) {
    setWaveId(message.getWaveId());
    clearWavelet();
    for (WaveletSnapshot field : message.getWavelet()) {
      addWavelet(new WaveletSnapshotProtoImpl(field));
    }
  }

  @Override
  public String getWaveId() {
    switchToProto();
    return proto.getWaveId();
  }

  @Override
  public void setWaveId(String value) {
    switchToProtoBuilder();
    protoBuilder.setWaveId(value);
  }

  @Override
  public List<WaveletSnapshotProtoImpl> getWavelet() {
    switchToProto();
    List<WaveletSnapshotProtoImpl> list = new ArrayList<WaveletSnapshotProtoImpl>();
    for (int i = 0; i < getWaveletSize(); i++) {
      WaveletSnapshotProtoImpl message = new WaveletSnapshotProtoImpl(proto.getWavelet(i));
      list.add(message);
    }
    return list;
  }

  @Override
  public void addAllWavelet(List<? extends WaveletSnapshot> values) {
    for (WaveletSnapshot message : values) {
      addWavelet(message);
    }
  }

  @Override
  public WaveletSnapshotProtoImpl getWavelet(int n) {
    switchToProto();
    return new WaveletSnapshotProtoImpl(proto.getWavelet(n));
  }

  @Override
  public void setWavelet(int n, WaveletSnapshot value) {
    switchToProtoBuilder();
    protoBuilder.setWavelet(n, getOrCreateWaveletSnapshotProtoImpl(value).getPB());
  }

  @Override
  public int getWaveletSize() {
    switchToProto();
    return proto.getWaveletCount();
  }

  @Override
  public void addWavelet(WaveletSnapshot value) {
    switchToProtoBuilder();
    protoBuilder.addWavelet(getOrCreateWaveletSnapshotProtoImpl(value).getPB());
  }

  @Override
  public void clearWavelet() {
    switchToProtoBuilder();
    protoBuilder.clearWavelet();
  }

  /** Get or create a WaveletSnapshotProtoImpl from a WaveletSnapshot. */
  private WaveletSnapshotProtoImpl getOrCreateWaveletSnapshotProtoImpl(WaveletSnapshot message) {
    if (message instanceof WaveletSnapshotProtoImpl) {
      return (WaveletSnapshotProtoImpl) message;
    } else {
      WaveletSnapshotProtoImpl messageImpl = new WaveletSnapshotProtoImpl();
      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.box.common.comms.WaveClientRpc.WaveViewSnapshot.newBuilder()
          : org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot.newBuilder(proto);
      proto = null;
    }
  }

  private void invalidateAll() {
    proto = null;
    protoBuilder = org.waveprotocol.box.common.comms.WaveClientRpc.WaveViewSnapshot.newBuilder();
  }

  @Override
  public JsonElement toGson(RawStringData raw, Gson gson) {
    JsonObject json = new JsonObject();
    json.add("1", new JsonPrimitive(getWaveId()));
    {
      JsonArray array = new JsonArray();
      for (int i = 0; i < getWaveletSize(); i++) {
        JsonElement elem = ((GsonSerializable) getWavelet(i)).toGson(raw, gson);
        // NOTE(kalman): if multistage parsing worked, split point would go here.
        array.add(elem);
      }
      json.add("2", 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");
      setWaveId(elem.getAsString());
    }
    if (jsonObject.has("2")) {
      JsonElement elem = jsonObject.get("2");
      {
        JsonArray array = elem.getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
          WaveletSnapshotProtoImpl payload = new WaveletSnapshotProtoImpl();
          GsonUtil.extractJsonObject(payload, array.get(i), gson, raw);
          addWavelet(payload);
        }
      }
    }
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    } else if (o instanceof WaveViewSnapshotProtoImpl) {
      return getPB().equals(((WaveViewSnapshotProtoImpl) o).getPB());
    } else {
      return false;
    }
  }

  @Override
  public boolean isEqualTo(Object o) {
    if (equals(o)) {
      return true;
    } else if (o instanceof WaveViewSnapshot) {
      return WaveViewSnapshotUtil.isEqual(this, (WaveViewSnapshot) o);
    } else {
      return false;
    }
  }

  @Override
  public int hashCode() {
    return getPB().hashCode();
  }

  @Override
  public String toString() {
    return getPB().toString();
  }

}
TOP

Related Classes of org.waveprotocol.box.common.comms.proto.WaveViewSnapshotProtoImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.