Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / conversion / ConversionHelper.java
blobf6f929a39ad6c6eacc0305134006f55f23d3214d
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.conversion;
5 import com.google.common.base.Preconditions;
6 import com.google.common.base.StringUtil;
8 import java.util.List;
10 /**
11 * Utility methods to validate data structures.
14 class ConversionHelper {
16 private ConversionHelper() {
19 /**
20 * Validates the size of document is not over limit, returns itself if the
21 * validation passes.
23 * @throws IllegalArgumentException if the size of document is over limit
25 static Document validateDocumentSize(Document document) {
26 Preconditions.checkNotNull(document);
27 int size = 0;
28 for (Asset asset : document.getAssets()) {
29 size += asset.getData().length;
31 Preconditions.checkArgument(size <= ConversionService.CONVERSION_MAX_SIZE_BYTES,
32 String.format("Each conversion should not be over %d bytes.",
33 ConversionService.CONVERSION_MAX_SIZE_BYTES));
34 return document;
37 /**
38 * Validates the document can be used as input for a conversion, returns
39 * itself if the validation passes.
41 * @throws IllegalArgumentException if additional asset name is null, empty
42 * or comprises only whitespace characters
44 static Document validateInputDoc(Document document) {
45 Preconditions.checkNotNull(document);
46 List<Asset> assets = document.getAssets();
47 for (Asset asset : assets.subList(1, assets.size())) {
48 Preconditions.checkArgument(!StringUtil.isEmptyOrWhitespace(asset.getName()),
49 "Additional asset name should not be null, empty or comprises only whitespaces");
51 return document;