Standardize array initializer syntax to use the form without "new type[]", to be...
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / ErrorCorrectionLevel.java
1 /*
2  * Copyright 2007 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.qrcode.decoder;
18
19 import com.google.zxing.ReaderException;
20
21 /**
22  * <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
23  * defined by the QR code standard.</p>
24  *
25  * @author srowen@google.com (Sean Owen)
26  */
27 final class ErrorCorrectionLevel {
28
29   // No, we can't use an enum here. J2ME doesn't support it.
30
31   /**
32    * L = ~7% correction
33    */
34   static final ErrorCorrectionLevel L = new ErrorCorrectionLevel(0);
35   /**
36    * M = ~15% correction
37    */
38   static final ErrorCorrectionLevel M = new ErrorCorrectionLevel(1);
39   /**
40    * Q = ~25% correction
41    */
42   static final ErrorCorrectionLevel Q = new ErrorCorrectionLevel(2);
43   /**
44    * H = ~30% correction
45    */
46   static final ErrorCorrectionLevel H = new ErrorCorrectionLevel(3);
47
48   private static final ErrorCorrectionLevel[] FOR_BITS = {M, L, H, Q};
49
50   private final int ordinal;
51
52   private ErrorCorrectionLevel(int ordinal) {
53     this.ordinal = ordinal;
54   }
55
56   int ordinal() {
57     return ordinal;
58   }
59
60   /**
61    * @param bits int containing the two bits encoding a QR Code's error correction level
62    * @return {@link ErrorCorrectionLevel} representing the encoded error correction level
63    */
64   static ErrorCorrectionLevel forBits(int bits) throws ReaderException {
65     if (bits < 0 || bits >= FOR_BITS.length) {
66       throw new ReaderException("Illegal error correction level bits" + bits);
67     }
68     return FOR_BITS[bits];
69   }
70
71
72 }