Big refactoring of ParsedResult: now split into ResultParser and ParsedResult classes...
[zxing.git] / core / src / com / google / zxing / client / result / ParsedResult.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result;
18
19 import com.google.zxing.Result;
20
21 /**
22  * <p>Abstract class representing the result of decoding a barcode, as more than
23  * a String -- as some type of structured data. This might be a subclass which represents
24  * a URL, or an e-mail address. {@link #parseReaderResult(Result)} will turn a raw
25  * decoded string into the most appropriate type of structured representation.</p>
26  *
27  * <p>Thanks to Jeff Griffin for proposing rewrite of these classes that relies less
28  * on exception-based mechanisms during parsing.</p>
29  *
30  * @author srowen@google.com (Sean Owen)
31  */
32 public abstract class ParsedResult {
33
34   private final ParsedResultType type;
35
36   protected ParsedResult(ParsedResultType type) {
37     this.type = type;
38   }
39
40   public ParsedResultType getType() {
41     return type;
42   }
43
44   public abstract String getDisplayResult();
45
46
47   public String toString() {
48     return getDisplayResult();
49   }
50
51   protected static void maybeAppend(String value, StringBuffer result) {
52     if (value != null) {
53       result.append('\n');
54       result.append(value);
55     }
56   }
57
58   protected static void maybeAppend(String[] value, StringBuffer result) {
59     if (value != null) {
60       for (int i = 0; i < value.length; i++) {
61         result.append('\n');
62         result.append(value[i]);
63       }
64     }
65   }
66
67 }