Added the ability to track who is throwing exceptions and how often in the core libra...
[zxing.git] / core / src / com / google / zxing / ReaderException.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;
18
19 /**
20  * The general exception class throw when something goes wrong during decoding of a barcode.
21  * This includes, but is not limited to, failing checksums / error correction algorithms, being
22  * unable to locate finder timing patterns, and so on.
23  *
24  * @author Sean Owen
25  */
26 public final class ReaderException extends Exception {
27
28   // TODO: Currently we throw up to 400 ReaderExceptions while scanning a single 240x240 image before
29   // rejecting it. This involves a lot of overhead and memory allocation, and affects both performance
30   // and latency on continuous scan clients. In the future, we should change all the decoders not to
31   // throw exceptions for routine events, like not finding a barcode on a given row. Instead, we
32   // should return error codes back to the callers, and simply delete this class. In the mean time, I
33   // have altered this class to be as lightweight as possible, by ignoring the exception string, and
34   // by disabling the generation of stack traces, which is especially time consuming. These are just
35   // temporary measures, pending the big cleanup.
36
37   private static final ReaderException instance = new ReaderException();
38
39   // EXCEPTION TRACKING SUPPORT
40   // Identifies who is throwing exceptions and how often. To use:
41   //
42   // 1. Uncomment these lines and the code below which uses them.
43   // 2. Uncomment the two corresponding lines in j2se/CommandLineRunner.decode()
44   // 3. Change core to build as Java 1.5 temporarily
45 //  private static int exceptionCount = 0;
46 //  private static Map<String,Integer> throwers = new HashMap<String,Integer>(32);
47
48   private ReaderException() {
49     // do nothing
50   }
51
52   public static ReaderException getInstance() {
53 //    Exception e = new Exception();
54 //    // Take the stack frame before this one.
55 //    StackTraceElement stack = e.getStackTrace()[1];
56 //    String key = stack.getClassName() + "." + stack.getMethodName() + "(), line " +
57 //        stack.getLineNumber();
58 //    if (throwers.containsKey(key)) {
59 //      Integer value = throwers.get(key);
60 //      value++;
61 //      throwers.put(key, value);
62 //    } else {
63 //      throwers.put(key, 1);
64 //    }
65 //    exceptionCount++;
66
67     return instance;
68   }
69
70 //  public static int getExceptionCountAndReset() {
71 //    int temp = exceptionCount;
72 //    exceptionCount = 0;
73 //    return temp;
74 //  }
75 //
76 //  public static String getThrowersAndReset() {
77 //    StringBuilder builder = new StringBuilder(1024);
78 //    Object[] keys = throwers.keySet().toArray();
79 //    for (int x = 0; x < keys.length; x++) {
80 //      String key = (String) keys[x];
81 //      Integer value = throwers.get(key);
82 //      builder.append(key);
83 //      builder.append(": ");
84 //      builder.append(value);
85 //      builder.append("\n");
86 //    }
87 //    throwers.clear();
88 //    return builder.toString();
89 //  }
90
91   // Prevent stack traces from being taken
92   // srowen says: huh, my IDE is saying this is not an override. native methods can't be overridden?
93   // This, at least, does not hurt. Because we use a singleton pattern here, it doesn't matter anyhow.
94   public Throwable fillInStackTrace() {
95     return null;
96   }
97
98 }