/**
   * Deserializes the snapshot contained in the {@link WaveletSnapshot} into an {@link
   * ObservableWaveletData}.
   *
   * @param snapshot the {@link WaveletSnapshot} to deserialize.
   * @throws OperationException if the ops in the snapshot can not be applied.
   * @throws InvalidParticipantAddress
   * @throws InvalidIdException
   */
  public static ObservableWaveletData deserializeWavelet(WaveletSnapshot snapshot, WaveId waveId)
      throws OperationException, InvalidParticipantAddress, InvalidIdException {
    ObservableWaveletData.Factory<? extends ObservableWaveletData> factory =
        WaveletDataImpl.Factory.create(
            ObservablePluggableMutableDocument.createFactory(SchemaCollection.empty()));

    ParticipantId author = ParticipantId.of(snapshot.getCreator());
    WaveletId waveletId = ModernIdSerialiser.INSTANCE.deserialiseWaveletId(snapshot.getWaveletId());
    long creationTime = snapshot.getCreationTime();

    ObservableWaveletData wavelet =
        factory.create(
            new EmptyWaveletSnapshot(
                waveId,
                waveletId,
                author,
                CoreWaveletOperationSerializer.deserialize(snapshot.getVersion()),
                creationTime));

    for (String participant : snapshot.getParticipantIdList()) {
      wavelet.addParticipant(getParticipantId(participant));
    }

    for (DocumentSnapshot document : snapshot.getDocumentList()) {
      addDocumentSnapshotToWavelet(document, wavelet);
    }

    wavelet.setVersion(snapshot.getVersion().getVersion());
    wavelet.setLastModifiedTime(snapshot.getLastModifiedTime());
    // The creator and creation time are set when the empty wavelet template is
    // created above.

    return wavelet;
  }
  /**
   * Serializes a document to a document snapshot.
   *
   * @param document The document to serialize
   * @return A snapshot of the given document
   */
  public static DocumentSnapshot serializeDocument(ReadableBlipData document) {
    DocumentSnapshot.Builder builder = DocumentSnapshot.newBuilder();

    builder.setDocumentId(document.getId());
    builder.setDocumentOperation(
        CoreWaveletOperationSerializer.serialize(document.getContent().asOperation()));

    builder.setAuthor(document.getAuthor().getAddress());
    for (ParticipantId participant : document.getContributors()) {
      builder.addContributor(participant.getAddress());
    }
    builder.setLastModifiedVersion(document.getLastModifiedVersion());
    builder.setLastModifiedTime(document.getLastModifiedTime());

    return builder.build();
  }
  private static void addDocumentSnapshotToWavelet(DocumentSnapshot snapshot, WaveletData container)
      throws InvalidParticipantAddress {
    DocOp op = CoreWaveletOperationSerializer.deserialize(snapshot.getDocumentOperation());
    DocInitialization docInit = DocOpUtil.asInitialization(op);

    Collection<ParticipantId> contributors = CollectionUtils.newArrayList();
    for (String p : snapshot.getContributorList()) {
      contributors.add(getParticipantId(p));
    }
    container.createDocument(
        snapshot.getDocumentId(),
        getParticipantId(snapshot.getAuthor()),
        contributors,
        docInit,
        snapshot.getLastModifiedTime(),
        snapshot.getLastModifiedVersion());
  }
  /**
   * Serializes a snapshot for a wavelet.
   *
   * @param wavelet wavelet to snapshot
   * @param hashedVersion hashed version of the wavelet
   * @return a wavelet snapshot that contains all the information in the original wavelet.
   */
  public static WaveletSnapshot serializeWavelet(
      ReadableWaveletData wavelet, HashedVersion hashedVersion) {
    WaveletSnapshot.Builder builder = WaveletSnapshot.newBuilder();

    builder.setWaveletId(ModernIdSerialiser.INSTANCE.serialiseWaveletId(wavelet.getWaveletId()));
    for (ParticipantId participant : wavelet.getParticipants()) {
      builder.addParticipantId(participant.toString());
    }
    for (String id : wavelet.getDocumentIds()) {
      ReadableBlipData data = wavelet.getDocument(id);
      builder.addDocument(serializeDocument(data));
    }

    builder.setVersion(CoreWaveletOperationSerializer.serialize(hashedVersion));
    builder.setLastModifiedTime(wavelet.getLastModifiedTime());
    builder.setCreator(wavelet.getCreator().getAddress());
    builder.setCreationTime(wavelet.getCreationTime());

    return builder.build();
  }