making stuff final, weakening types, etc. per IntelliJ analysis
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / BenchmarkItem.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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.androidtest;
18
19 import com.google.zxing.BarcodeFormat;
20
21 public final class BenchmarkItem {
22
23   private final String mPath;
24   private int[] mTimes;
25   private int mPosition;
26   private boolean mDecoded;
27   private BarcodeFormat mFormat;
28
29   public BenchmarkItem(String path, int runs) {
30     mPath = path;
31     assert(runs > 0);
32     mTimes = new int[runs];
33     mPosition = 0;
34     mDecoded = false;
35     mFormat = null;
36   }
37
38   public final void addResult(int microseconds) {
39     mTimes[mPosition] = microseconds;
40     mPosition++;
41   }
42
43   public final void setDecoded(boolean decoded) {
44     mDecoded = decoded;
45   }
46
47   public final void setFormat(BarcodeFormat format) {
48     mFormat = format;
49   }
50
51   @Override
52   public final String toString() {
53     StringBuffer result = new StringBuffer();
54     result.append(mDecoded ? ("DECODED " + mFormat.toString() + ": ") : "FAILED: ");
55     result.append(mPath);
56     result.append(" (");
57     result.append(getAverageTime());
58     result.append(" us average)");
59 //    int size = mTimes.length;
60 //    for (int x = 0; x < size; x++) {
61 //      result.append(mTimes[x]);
62 //      result.append(" ");
63 //    }
64     return result.toString();
65   }
66
67   /**
68    * Calculates the average time but throws out the maximum as an outlier first.
69    *
70    * @return The average decoding time in microseconds.
71    */
72   private int getAverageTime() {
73     int size = mTimes.length;
74     int total = 0;
75     int max = mTimes[0];
76     for (int x = 0; x < size; x++) {
77       int time = mTimes[x];
78       total += time;
79       if (time > max) {
80         max = time;
81       }
82     }
83     total -= max;
84     size--;
85     if (size > 0) {
86       return total / size;
87     } else {
88       return 0;
89     }
90   }
91
92 }