/**
* 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.search.proto;
import org.waveprotocol.box.search.SearchResponse.Digest;
import org.waveprotocol.box.search.proto.SearchResponseProtoImpl.DigestProtoImpl;
import org.waveprotocol.box.search.SearchResponse;
import org.waveprotocol.box.search.SearchResponseUtil;
import org.waveprotocol.box.search.SearchResponse.Digest;
import org.waveprotocol.box.search.SearchResponseUtil.DigestUtil;
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 SearchResponse.
*
* Generated from org/waveprotocol/box/search/search.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 SearchResponseProtoImpl
implements SearchResponse,
// 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.search.SearchProto.SearchResponse> {
// NOTE(kalman): It would be nicer to add a proto serialisation
// utility rather than having this class at all.
public static final class DigestProtoImpl
implements Digest,
// 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.search.SearchProto.SearchResponse.Digest> {
private org.waveprotocol.box.search.SearchProto.SearchResponse.Digest proto = null;
private org.waveprotocol.box.search.SearchProto.SearchResponse.Digest.Builder protoBuilder = org.waveprotocol.box.search.SearchProto.SearchResponse.Digest.newBuilder();
public DigestProtoImpl() {
}
public DigestProtoImpl(org.waveprotocol.box.search.SearchProto.SearchResponse.Digest proto) {
this.proto = proto;
}
public DigestProtoImpl(Digest message) {
copyFrom(message);
}
@Override
public org.waveprotocol.box.search.SearchProto.SearchResponse.Digest getPB() {
switchToProto();
return proto;
}
@Override
public void setPB(org.waveprotocol.box.search.SearchProto.SearchResponse.Digest proto) {
this.proto = proto;
this.protoBuilder = null;
}
@Override
public void copyFrom(Digest message) {
setTitle(message.getTitle());
setSnippet(message.getSnippet());
setWaveId(message.getWaveId());
setLastModified(message.getLastModified());
setUnreadCount(message.getUnreadCount());
setBlipCount(message.getBlipCount());
clearParticipants();
for (String field : message.getParticipants()) {
addParticipants(field);
}
setAuthor(message.getAuthor());
}
@Override
public String getTitle() {
switchToProto();
return proto.getTitle();
}
@Override
public void setTitle(String value) {
switchToProtoBuilder();
protoBuilder.setTitle(value);
}
@Override
public String getSnippet() {
switchToProto();
return proto.getSnippet();
}
@Override
public void setSnippet(String value) {
switchToProtoBuilder();
protoBuilder.setSnippet(value);
}
@Override
public String getWaveId() {
switchToProto();
return proto.getWaveId();
}
@Override
public void setWaveId(String value) {
switchToProtoBuilder();
protoBuilder.setWaveId(value);
}
@Override
public long getLastModified() {
switchToProto();
return proto.getLastModified();
}
@Override
public void setLastModified(long value) {
switchToProtoBuilder();
protoBuilder.setLastModified(value);
}
@Override
public int getUnreadCount() {
switchToProto();
return proto.getUnreadCount();
}
@Override
public void setUnreadCount(int value) {
switchToProtoBuilder();
protoBuilder.setUnreadCount(value);
}
@Override
public int getBlipCount() {
switchToProto();
return proto.getBlipCount();
}
@Override
public void setBlipCount(int value) {
switchToProtoBuilder();
protoBuilder.setBlipCount(value);
}
@Override
public List<String> getParticipants() {
switchToProto();
return Collections.unmodifiableList(proto.getParticipantsList());
}
@Override
public void addAllParticipants(List<String> values) {
switchToProtoBuilder();
protoBuilder.addAllParticipants(values);
}
@Override
public String getParticipants(int n) {
switchToProto();
return proto.getParticipants(n);
}
@Override
public void setParticipants(int n, String value) {
switchToProtoBuilder();
protoBuilder.setParticipants(n, value);
}
@Override
public int getParticipantsSize() {
switchToProto();
return proto.getParticipantsCount();
}
@Override
public void addParticipants(String value) {
switchToProtoBuilder();
protoBuilder.addParticipants(value);
}
@Override
public void clearParticipants() {
switchToProtoBuilder();
protoBuilder.clearParticipants();
}
@Override
public String getAuthor() {
switchToProto();
return proto.getAuthor();
}
@Override
public void setAuthor(String value) {
switchToProtoBuilder();
protoBuilder.setAuthor(value);
}
private void switchToProto() {
if (proto == null) {
proto = protoBuilder.build();
protoBuilder = null;
}
}
private void switchToProtoBuilder() {
if (protoBuilder == null) {
protoBuilder = (proto == null)
? org.waveprotocol.box.search.SearchProto.SearchResponse.Digest.newBuilder()
: org.waveprotocol.box.search.SearchProto.SearchResponse.Digest.newBuilder(proto);
proto = null;
}
}
private void invalidateAll() {
proto = null;
protoBuilder = org.waveprotocol.box.search.SearchProto.SearchResponse.Digest.newBuilder();
}
@Override
public JsonElement toGson(RawStringData raw, Gson gson) {
JsonObject json = new JsonObject();
json.add("1", new JsonPrimitive(getTitle()));
json.add("2", new JsonPrimitive(getSnippet()));
json.add("3", new JsonPrimitive(getWaveId()));
json.add("4", GsonUtil.toJson(getLastModified()));
json.add("5", new JsonPrimitive(getUnreadCount()));
json.add("6", new JsonPrimitive(getBlipCount()));
{
JsonArray array = new JsonArray();
for (int i = 0; i < getParticipantsSize(); i++) {
array.add(new JsonPrimitive(getParticipants(i)));
}
json.add("7", array);
}
json.add("8", new JsonPrimitive(getAuthor()));
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");
setTitle(elem.getAsString());
}
if (jsonObject.has("2")) {
JsonElement elem = jsonObject.get("2");
setSnippet(elem.getAsString());
}
if (jsonObject.has("3")) {
JsonElement elem = jsonObject.get("3");
setWaveId(elem.getAsString());
}
if (jsonObject.has("4")) {
JsonElement elem = jsonObject.get("4");
setLastModified(GsonUtil.fromJson(elem));
}
if (jsonObject.has("5")) {
JsonElement elem = jsonObject.get("5");
setUnreadCount(elem.getAsInt());
}
if (jsonObject.has("6")) {
JsonElement elem = jsonObject.get("6");
setBlipCount(elem.getAsInt());
}
if (jsonObject.has("7")) {
JsonElement elem = jsonObject.get("7");
{
JsonArray array = elem.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
addParticipants(array.get(i).getAsString());
}
}
}
if (jsonObject.has("8")) {
JsonElement elem = jsonObject.get("8");
setAuthor(elem.getAsString());
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof DigestProtoImpl) {
return getPB().equals(((DigestProtoImpl) o).getPB());
} else {
return false;
}
}
@Override
public boolean isEqualTo(Object o) {
if (equals(o)) {
return true;
} else if (o instanceof Digest) {
return DigestUtil.isEqual(this, (Digest) o);
} else {
return false;
}
}
@Override
public int hashCode() {
return getPB().hashCode();
}
@Override
public String toString() {
return getPB().toString();
}
}
private org.waveprotocol.box.search.SearchProto.SearchResponse proto = null;
private org.waveprotocol.box.search.SearchProto.SearchResponse.Builder protoBuilder = org.waveprotocol.box.search.SearchProto.SearchResponse.newBuilder();
public SearchResponseProtoImpl() {
}
public SearchResponseProtoImpl(org.waveprotocol.box.search.SearchProto.SearchResponse proto) {
this.proto = proto;
}
public SearchResponseProtoImpl(SearchResponse message) {
copyFrom(message);
}
@Override
public org.waveprotocol.box.search.SearchProto.SearchResponse getPB() {
switchToProto();
return proto;
}
@Override
public void setPB(org.waveprotocol.box.search.SearchProto.SearchResponse proto) {
this.proto = proto;
this.protoBuilder = null;
}
@Override
public void copyFrom(SearchResponse message) {
setQuery(message.getQuery());
setTotalResults(message.getTotalResults());
clearDigests();
for (Digest field : message.getDigests()) {
addDigests(new DigestProtoImpl(field));
}
}
@Override
public String getQuery() {
switchToProto();
return proto.getQuery();
}
@Override
public void setQuery(String value) {
switchToProtoBuilder();
protoBuilder.setQuery(value);
}
@Override
public int getTotalResults() {
switchToProto();
return proto.getTotalResults();
}
@Override
public void setTotalResults(int value) {
switchToProtoBuilder();
protoBuilder.setTotalResults(value);
}
@Override
public List<DigestProtoImpl> getDigests() {
switchToProto();
List<DigestProtoImpl> list = new ArrayList<DigestProtoImpl>();
for (int i = 0; i < getDigestsSize(); i++) {
DigestProtoImpl message = new DigestProtoImpl(proto.getDigests(i));
list.add(message);
}
return list;
}
@Override
public void addAllDigests(List<? extends Digest> values) {
for (Digest message : values) {
addDigests(message);
}
}
@Override
public DigestProtoImpl getDigests(int n) {
switchToProto();
return new DigestProtoImpl(proto.getDigests(n));
}
@Override
public void setDigests(int n, Digest value) {
switchToProtoBuilder();
protoBuilder.setDigests(n, getOrCreateDigestProtoImpl(value).getPB());
}
@Override
public int getDigestsSize() {
switchToProto();
return proto.getDigestsCount();
}
@Override
public void addDigests(Digest value) {
switchToProtoBuilder();
protoBuilder.addDigests(getOrCreateDigestProtoImpl(value).getPB());
}
@Override
public void clearDigests() {
switchToProtoBuilder();
protoBuilder.clearDigests();
}
/** Get or create a DigestProtoImpl from a Digest. */
private DigestProtoImpl getOrCreateDigestProtoImpl(Digest message) {
if (message instanceof DigestProtoImpl) {
return (DigestProtoImpl) message;
} else {
DigestProtoImpl messageImpl = new DigestProtoImpl();
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.search.SearchProto.SearchResponse.newBuilder()
: org.waveprotocol.box.search.SearchProto.SearchResponse.newBuilder(proto);
proto = null;
}
}
private void invalidateAll() {
proto = null;
protoBuilder = org.waveprotocol.box.search.SearchProto.SearchResponse.newBuilder();
}
@Override
public JsonElement toGson(RawStringData raw, Gson gson) {
JsonObject json = new JsonObject();
json.add("1", new JsonPrimitive(getQuery()));
json.add("2", new JsonPrimitive(getTotalResults()));
{
JsonArray array = new JsonArray();
for (int i = 0; i < getDigestsSize(); i++) {
JsonElement elem = ((GsonSerializable) getDigests(i)).toGson(raw, gson);
// NOTE(kalman): if multistage parsing worked, split point would go here.
array.add(elem);
}
json.add("3", 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");
setQuery(elem.getAsString());
}
if (jsonObject.has("2")) {
JsonElement elem = jsonObject.get("2");
setTotalResults(elem.getAsInt());
}
if (jsonObject.has("3")) {
JsonElement elem = jsonObject.get("3");
{
JsonArray array = elem.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
DigestProtoImpl payload = new DigestProtoImpl();
GsonUtil.extractJsonObject(payload, array.get(i), gson, raw);
addDigests(payload);
}
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof SearchResponseProtoImpl) {
return getPB().equals(((SearchResponseProtoImpl) o).getPB());
} else {
return false;
}
}
@Override
public boolean isEqualTo(Object o) {
if (equals(o)) {
return true;
} else if (o instanceof SearchResponse) {
return SearchResponseUtil.isEqual(this, (SearchResponse) o);
} else {
return false;
}
}
@Override
public int hashCode() {
return getPB().hashCode();
}
@Override
public String toString() {
return getPB().toString();
}
}