1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.conversion
;
5 import com
.google
.common
.base
.Objects
;
6 import com
.google
.common
.base
.Preconditions
;
7 import com
.google
.common
.base
.StringUtil
;
10 * The options for a conversion following builder pattern. This class also
11 * defines default values for most options.
14 public final class ConversionOptions
{
16 private Integer imageWidth
= 800;
18 private Integer firstPage
= 1;
20 private Integer lastPage
= -1;
21 private String ocrInputLanguage
;
23 private ConversionOptions() {}
26 * Sets the output image width in pixels. Only applies to
27 * conversions that generate image files.
29 * @param imageWidth the output image width in pixels
30 * @return this ConversionOptions instance for chaining
31 * @throws IllegalArgumentException if imageWidth is not positive
33 public ConversionOptions
imageWidth(int imageWidth
) {
34 Preconditions
.checkArgument(imageWidth
> 0, "image width must be > 0, got " + imageWidth
);
35 this.imageWidth
= imageWidth
;
40 * Sets the number of the first page to generate. Only applies to
41 * conversions that generate image files.
43 * @param firstPage the number of the first page to generate
44 * @return this ConversionOptions instance for chaining
45 * @throws IllegalArgumentException if firstPage is not positive
47 public ConversionOptions
firstPage(int firstPage
) {
48 Preconditions
.checkArgument(firstPage
> 0, "first page must be > 0, got " + firstPage
);
49 this.firstPage
= firstPage
;
54 * Sets the number of the last page to generate, defaults to the last page
55 * of the document. Only applies to conversions that generate image files.
57 * @param lastPage the number of the last page to generate
58 * @return this ConversionOptions instance for chaining
59 * @throws IllegalArgumentException if lastPage is not positive
61 public ConversionOptions
lastPage(int lastPage
) {
62 Preconditions
.checkArgument(lastPage
> 0, "last page must be > 0, got " + lastPage
);
63 this.lastPage
= lastPage
;
68 * Sets the language code in BCP 47 format, used by OCR engine to search for
69 * language-specific character set.
71 * @param ocrInputLanguage the language code used by OCR engine
72 * @return this ConversionOptions instance for chaining
73 * @throws IllegalArgumentException if ocrInputLanguage is null, empty or
74 * comprises only whitespace characters
76 public ConversionOptions
ocrInputLanguage(String ocrInputLanguage
) {
77 Preconditions
.checkArgument(!StringUtil
.isEmptyOrWhitespace(ocrInputLanguage
),
78 "OCR input language should not be null, empty or comprises only whitespace characters");
79 this.ocrInputLanguage
= ocrInputLanguage
.toLowerCase();
84 * Returns the output image width in pixels.
86 Integer
getImageWidth() {
91 * Returns the number of the first page to generate.
93 Integer
getFirstPage() {
98 * Returns the number of the last page to generate.
100 Integer
getLastPage() {
105 * Returns the language code used by OCR engine, or null if not present.
107 String
getOcrInputLanguage() {
108 return ocrInputLanguage
;
112 public boolean equals(Object o
) {
116 if (o
== null || getClass() != o
.getClass()) {
120 ConversionOptions other
= (ConversionOptions
) o
;
121 return Objects
.equal(imageWidth
, other
.getImageWidth())
122 && Objects
.equal(firstPage
, other
.getFirstPage())
123 && Objects
.equal(lastPage
, other
.getLastPage())
124 && Objects
.equal(ocrInputLanguage
, other
.getOcrInputLanguage());
128 public int hashCode() {
129 return Objects
.hashCode(imageWidth
, firstPage
, lastPage
, ocrInputLanguage
);
133 * Provides static creation methods for {@link ConversionOptions}.
135 public static final class Builder
{
140 * Returns a {@link ConversionOptions} with default values.
142 public static ConversionOptions
withDefaults() {
143 return new ConversionOptions();
147 * Returns a {@link ConversionOptions} with specified output image width.
149 public static ConversionOptions
withImageWidth(int imageWidth
) {
150 return withDefaults().imageWidth(imageWidth
);
154 * Returns a {@link ConversionOptions} with specified number of first page.
156 public static ConversionOptions
withFirstPage(int firstPage
) {
157 return withDefaults().firstPage(firstPage
);
161 * Returns a {@link ConversionOptions} with specified number of last page.
163 public static ConversionOptions
withLastPage(int lastPage
) {
164 return withDefaults().lastPage(lastPage
);
168 * Returns a {@link ConversionOptions} with specified language code for OCR.
170 public static ConversionOptions
withOcrInputLanguage(String ocrInputLanguage
) {
171 return withDefaults().ocrInputLanguage(ocrInputLanguage
);