At last update to JUnit 4.x
[zxing.git] / core / test / src / com / google / zxing / oned / rss / expanded / BinaryUtil.java
1 /*
2  * Copyright (C) 2010 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 /*
18  * These authors would like to acknowledge the Spanish Ministry of Industry,
19  * Tourism and Trade, for the support in the project TSI020301-2008-2
20  * "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
21  * Mobile Dynamic Environments", led by Treelogic
22  * ( http://www.treelogic.com/ ):
23  *
24  *   http://www.piramidepse.com/
25  */
26
27 package com.google.zxing.oned.rss.expanded;
28
29 import com.google.zxing.common.BitArray;
30
31 /**
32  * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
33  */
34 public final class BinaryUtil {
35
36   private BinaryUtil() {
37   }
38
39   /*
40   * Constructs a BitArray from a String like the one returned from BitArray.toString()
41   */
42   public static BitArray buildBitArrayFromString(String data){
43     String dotsAndXs = data.replaceAll("1", "X").replaceAll("0",".");
44     BitArray binary = new BitArray(dotsAndXs.replaceAll(" ", "").length());
45     int counter = 0;
46
47     for(int i = 0; i < dotsAndXs.length(); ++i){
48       if(i % 9 == 0){ // spaces
49         if(dotsAndXs.charAt(i) != ' ') {
50           throw new IllegalStateException("space expected");
51         }
52         continue;
53       }
54
55       char currentChar = dotsAndXs.charAt(i);
56       if(currentChar == 'X' || currentChar == 'x') {
57         binary.set(counter);
58       }
59       counter++;
60     }
61     return binary;
62   }
63
64   public static BitArray buildBitArrayFromStringWithoutSpaces(String data){
65     StringBuilder sb = new StringBuilder();
66
67     String dotsAndXs = data.replaceAll("1", "X").replaceAll("0",".");
68
69     int current = 0;
70     while(current < dotsAndXs.length()){
71       sb.append(' ');
72       for(int i = 0; i < 8 && current < dotsAndXs.length(); ++i){
73         sb.append(dotsAndXs.charAt(current));
74         current++;
75       }
76     }
77
78     return buildBitArrayFromString(sb.toString());
79   }
80
81   /*
82   public static int extractNumericValueFromBitArray(BitArray information, int pos, int bits) {
83     if(bits > 32) {
84       throw new IllegalArgumentException("extractNumberValueFromBitArray can't handle more than 32 bits");
85     }
86
87     BitArray numeric = new BitArray(bits);
88     for(int i = 0; i < bits; ++i) {
89       if (information.get(pos + i)) {
90         numeric.set(i);
91       }
92     }
93     numeric.reverse();
94     return numeric.bits[0];
95   }
96    */
97 }