1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.conversion
;
5 import com
.google
.appengine
.api
.conversion
.ConversionServicePb
.AssetInfo
;
6 import com
.google
.appengine
.api
.conversion
.ConversionServicePb
.DocumentInfo
;
7 import com
.google
.common
.base
.Converter
;
8 import com
.google
.common
.collect
.Lists
;
10 import java
.util
.List
;
13 * A reversible converter between {@link Document} and its corresponding
17 class DocumentProtoConverter
extends Converter
<Document
, DocumentInfo
> {
19 private final AssetProtoConverter assetProtoConverter
= new AssetProtoConverter();
22 * Converts a {@link Document} to its corresponding protocol buffer.
24 * @throws IllegalArgumentException if the size of document is over limit
27 public DocumentInfo
doForward(Document document
) {
28 ConversionHelper
.validateDocumentSize(document
);
30 DocumentInfo
.Builder documentInfo
= DocumentInfo
.newBuilder();
31 for (Asset asset
: document
.getAssets()) {
32 documentInfo
.addAsset(assetProtoConverter
.convert(asset
));
34 return documentInfo
.build();
38 public Document
doBackward(DocumentInfo documentPb
) {
39 List
<Asset
> assets
= Lists
.newArrayList();
40 for (AssetInfo asset
: documentPb
.getAssetList()) {
41 assets
.add(assetProtoConverter
.reverse(asset
));
43 return new Document(assets
);