View Javadoc

1   /***********************************************************************************************************************
2    * Copyright (c) 2004, International Barcode Consortium
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this list of
9    * conditions and the following disclaimer.
10   * Redistributions in binary form must reproduce the above copyright notice, this list of
11   * conditions and the following disclaimer in the documentation and/or other materials
12   * provided with the distribution.
13   * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14   * or promote products derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   ***********************************************************************************************************************/
26  
27  package net.sourceforge.barbecue.linear.twoOfFive;
28  
29  import net.sourceforge.barbecue.Module;
30  
31  import java.util.Map;
32  import java.util.HashMap;
33  
34  /**
35   * Standard 2 of 5 barcode module definitions.
36   *
37   * @author <a href="mailto:james@metalskin.com">James Jenner</a>
38   */
39  class Std2of5ModuleFactory {
40      protected static final Map SET = new HashMap();
41      
42      public static final Module START_CHAR = new Module(new int[] {3, 1, 3, 1, 1, 1});
43      public static final Module END_CHAR = new Module(new int[] {3, 1, 1, 1, 3, 1});
44      
45      private final static int CHECKSUM_WEIGHT_EVEN = 1;
46      private final static int CHECKSUM_WEIGHT_ODD = 3;
47      
48      static {
49          init();
50      }
51      
52      /**
53       * No public access.
54       */
55      protected Std2of5ModuleFactory() {
56      }
57      
58      /**
59       * Initialise the module definitions.
60       */
61      private static void init() {
62          SET.put("0", new int[] {1, 1, 3, 3, 1});
63          SET.put("1", new int[] {3, 1, 1, 1, 3});
64          SET.put("2", new int[] {1, 3, 1, 1, 3});
65          SET.put("3", new int[] {3, 3, 1, 1, 1});
66          SET.put("4", new int[] {1, 1, 3, 1, 3});
67          SET.put("5", new int[] {3, 1, 3, 1, 1});
68          SET.put("6", new int[] {1, 3, 3, 1, 1});
69          SET.put("7", new int[] {1, 1, 1, 3, 3});
70          SET.put("8", new int[] {3, 1, 1, 3, 1});
71          SET.put("9", new int[] {1, 3, 1, 3, 1});
72      }
73      
74      /**
75       * Returns the module that represents the specified character.
76       * @param key The data character to get the encoding module for
77       * @return The module that encodes the given char
78       */
79      public static Module getModule(String key) {
80          /*
81          Module module = null;
82          module = (Module) SET.get(key);
83          module.setSymbol(key);
84          return module;
85           */
86          Module module = null;
87          int[] bar = null;
88          bar = (int[])SET.get(key);
89          
90          // this should always be true, but best to check just in case...
91          if(bar != null && bar.length == 5) {
92              
93              // create the new module based on the selected bar val with the space as 1
94              module = new Module(new int[] {
95                  bar[0], 1,
96                  bar[1], 1,
97                  bar[2], 1,
98                  bar[3], 1,
99                  bar[4], 1
100             });
101 
102             module.setSymbol(key);
103         }
104         
105         return module;
106         
107     }
108     
109     /**
110      * Indicates whether the given character is valid for this barcode or not.
111      * This basically just checks to see whether the key is in the list of
112      * encoded characters.
113      * @param key The key to check for validity
114      * @return True if the key is valid, false otherwise
115      */
116     public static boolean isValid(String key) {
117         return SET.containsKey(key);
118     }
119 }