View Javadoc

1   /***********************************************************************************************************************
2    * Copyright (c) 2003, 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;
28  
29  import net.sourceforge.barbecue.output.Output;
30  import net.sourceforge.barbecue.output.OutputException;
31  
32  import java.util.Arrays;
33  
34  /**
35   * Internal class that is used to organise barcode data into groups of bars.
36   * <p/>Note: You should not instantiate this class directly.
37   *
38   * @author <a href="mailto:opensource@ianbourke.com">Ian Bourke</a>
39   */
40  public class Module {
41      /** The specification of bars that makes up this module, in a list of bar widths in on, off order) */
42      protected final int[] bars;
43      private String symbol;
44      
45      /**
46       * Constructs a new Module with the given bar specification.
47       * @param bars The bar specification
48       */
49      public Module(int[] bars) {
50          this.bars = bars;
51      }
52      
53      /**
54       * Returns the symbol being encoded by this module.
55       * @return The symbol encoded by this module
56       */
57      public String getSymbol() {
58          return symbol;
59      }
60      
61      /**
62       * Sets the symbol that this module encodes.
63       * @param symbol The symbol being encoded by this module
64       */
65      public void setSymbol(String symbol) {
66          this.symbol = symbol;
67      }
68      
69      /**
70       * Returns the underlying total width of the bars from the bar
71       * specification (that is, the sum of original bar widths in base
72       * bar units).
73       * @return The total width of bars in base (unscaled) units
74       */
75      public int widthInBars() {
76          int sum = 0;
77          for (int i = 0; i < bars.length; i++) {
78              sum += bars[i];
79          }
80          return sum;
81      }
82      
83      /**
84       * Draws the module to the given outputter at the specified origin.
85       * @param output The outputter to draw to
86       * @param x The X component of the origin
87       * @param y The Y component of the origin
88       * @param barWidth
89       * @param barHeight
90       * @return The total width drawn
91       */
92      protected int draw(Output output, int x, int y, int barWidth, int barHeight) throws OutputException {
93          int sum = 0;
94          for (int i = 0; i < bars.length; i++) {
95              int bar = bars[i];
96              int w = bar * barWidth;
97              // sum += w;
98              // output.drawBar(x, y, w, barHeight, (i % 2 == 0));
99              sum += output.drawBar(x, y, w, barHeight, (i % 2 == 0));
100             x += w;
101         }
102 
103         return sum;
104     }
105     
106     /**
107      * See Object.
108      */
109     public boolean equals(Object o) {
110         if (this == o) {
111             return true;
112         }
113         
114         if (!(o instanceof Module)) {
115             return false;
116         }
117         
118         final Module module = (Module) o;
119         
120         if (!Arrays.equals(bars, module.bars)) {
121             return false;
122         }
123         
124         return true;
125     }
126     
127     
128     
129     /**
130      * Returns a hash code value for the object. This method is
131      * supported for the benefit of hashtables such as those provided by
132      * <code>java.util.Hashtable</code>.
133      * <p>
134      * The general contract of <code>hashCode</code> is:
135      * <ul>
136      * <li>Whenever it is invoked on the same object more than once during
137      *     an execution of a Java application, the <tt>hashCode</tt> method
138      *     must consistently return the same integer, provided no information
139      *     used in <tt>equals</tt> comparisons on the object is modified.
140      *     This integer need not remain consistent from one execution of an
141      *     application to another execution of the same application.
142      * <li>If two objects are equal according to the <tt>equals(Object)</tt>
143      *     method, then calling the <code>hashCode</code> method on each of
144      *     the two objects must produce the same integer result.
145      * <li>It is <em>not</em> required that if two objects are unequal
146      *     according to the {@link Object#equals(Object)}
147      *     method, then calling the <tt>hashCode</tt> method on each of the
148      *     two objects must produce distinct integer results.  However, the
149      *     programmer should be aware that producing distinct integer results
150      *     for unequal objects may improve the performance of hashtables.
151      * </ul>
152      * <p>
153      * As much as is reasonably practical, the hashCode method defined by
154      * class <tt>Object</tt> does return distinct integers for distinct
155      * objects. (This is typically implemented by converting the internal
156      * address of the object into an integer, but this implementation
157      * technique is not required by the
158      * Java<font size="-2"><sup>TM</sup></font> programming language.)
159      *
160      * @return  a hash code value for this object.
161      * @see     Object#equals(Object)
162      */
163     public int hashCode() {
164         int sum = 0;
165         for (int i = 0; i < bars.length; i++) {
166             sum += (i + 1) * bars[i];
167         }
168         return sum;
169     }
170     
171     public String toString() {
172         StringBuffer buf = new StringBuffer();
173         for (int i = 0; i < bars.length; i++) {
174             if (i > 0) {
175                 buf.append(", ");
176             }
177             buf.append(bars[i]);
178         }
179         return buf.toString();
180     }
181 }