001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021import org.jpos.iso.validator.ISOVException;
022
023/**
024 * Validator for ASCII numeric and no-zero filled fields.
025 * <p>Title: jPOS</p>
026 * <p>Description: Java Framework for Financial Systems</p>
027 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
028 * <p>Company: www.jPOS.org</p>
029 * @author Jose Eduardo Leon
030 * @version 1.0
031 */
032public class IVA_NUMNOZERO extends IVA_NUM {
033
034    /** Default constructor. */
035    public IVA_NUMNOZERO(  ) {
036        super();
037    }
038
039    /**
040     * Constructs a validator with explicit length bounds and numeric radix.
041     *
042     * @param minLen minimum acceptable length
043     * @param maxLen maximum acceptable length
044     * @param Description human-readable field description
045     * @param radix numeric radix (e.g. 10, 16)
046     */
047    public IVA_NUMNOZERO( int minLen, int maxLen, String Description, int radix ) {
048        super( minLen, maxLen, Description, radix );
049    }
050
051    /**
052     * Constructs a validator with explicit length bounds.
053     *
054     * @param minLen minimum acceptable length
055     * @param maxLen maximum acceptable length
056     * @param Description human-readable field description
057     */
058    public IVA_NUMNOZERO( int minLen, int maxLen, String Description ) {
059        super( minLen, maxLen, Description );
060    }
061
062    /**
063     * Constructs a validator with a minimum length and numeric radix.
064     *
065     * @param minLen minimum acceptable length
066     * @param Description human-readable field description
067     * @param radix numeric radix
068     */
069    public IVA_NUMNOZERO( int minLen, String Description, int radix ) {
070        super( minLen, Description, radix );
071    }
072
073    /**
074     * Constructs a validator with a maximum length.
075     *
076     * @param maxLen maximum acceptable length
077     * @param Description human-readable field description
078     */
079    public IVA_NUMNOZERO( int maxLen, String Description ) {
080        super( maxLen, Description );
081    }
082
083    /**
084     * Constructs a validator using only a description and numeric radix.
085     *
086     * @param Description human-readable field description
087     * @param radix numeric radix
088     */
089    public IVA_NUMNOZERO( String Description, int radix ) {
090        super( Description, radix );
091    }
092
093    /**
094     * Constructs a validator using only a description.
095     *
096     * @param Description human-readable field description
097     */
098    public IVA_NUMNOZERO( String Description ) {
099        super( Description );
100    }
101
102    /**
103     * Constructs a validator with a custom break-on-error flag.
104     *
105     * @param breakOnError if {@code true}, validation throws on the first error
106     * @param Description human-readable field description
107     */
108    public IVA_NUMNOZERO( boolean breakOnError, String Description ) {
109        this( Description );
110        this.breakOnError = breakOnError;
111    }
112
113    /**
114     * Constructs a validator with a custom break-on-error flag and numeric radix.
115     *
116     * @param breakOnError if {@code true}, validation throws on the first error
117     * @param Description human-readable field description
118     * @param radix numeric radix
119     */
120    public IVA_NUMNOZERO( boolean breakOnError, String Description, int radix ) {
121        this( Description, radix );
122        this.breakOnError = breakOnError;
123    }
124
125    /**
126     * Constructs a validator with a custom break-on-error flag and a maximum length.
127     *
128     * @param breakOnError if {@code true}, validation throws on the first error
129     * @param maxLen maximum acceptable length
130     * @param Description human-readable field description
131     */
132    public IVA_NUMNOZERO( boolean breakOnError, int maxLen, String Description ) {
133        this( maxLen, Description );
134        this.breakOnError = breakOnError;
135    }
136
137    /**
138     * Constructs a validator with a custom break-on-error flag, max length, and radix.
139     *
140     * @param breakOnError if {@code true}, validation throws on the first error
141     * @param maxLen maximum acceptable length
142     * @param Description human-readable field description
143     * @param radix numeric radix
144     */
145    public IVA_NUMNOZERO( boolean breakOnError, int maxLen, String Description, int radix ) {
146        this( maxLen, Description, radix );
147        this.breakOnError = breakOnError;
148    }
149
150    /**
151     * Constructs a validator with a custom break-on-error flag and explicit length bounds.
152     *
153     * @param breakOnError if {@code true}, validation throws on the first error
154     * @param minLen minimum acceptable length
155     * @param maxLen maximum acceptable length
156     * @param Description human-readable field description
157     */
158    public IVA_NUMNOZERO( boolean breakOnError, int minLen, int maxLen, String Description ) {
159        this( minLen, maxLen, Description );
160        this.breakOnError = breakOnError;
161    }
162
163    /**
164     * Constructs a validator with a custom break-on-error flag, length bounds, and radix.
165     *
166     * @param breakOnError if {@code true}, validation throws on the first error
167     * @param minLen minimum acceptable length
168     * @param maxLen maximum acceptable length
169     * @param Description human-readable field description
170     * @param radix numeric radix
171     */
172    public IVA_NUMNOZERO( boolean breakOnError, int minLen, int maxLen, String Description, int radix ) {
173        this( minLen, maxLen, Description, radix );
174        this.breakOnError = breakOnError;
175    }
176
177
178    /**
179     * Validate that component is not zero-filled.
180     *
181     * @param f component to validate
182     * @return the validated (possibly wrapped) component
183     * @throws ISOException if {@link #breakOnError} is set and the field is zero-filled or fails numeric validation
184     */
185    public ISOComponent validate ( ISOComponent f ) throws ISOException {
186        ISOField c = (ISOField)f;
187        try {
188            /** numeric **/
189            c = (ISOField)super.validate( c );
190            /** positive **/
191            if (ISOUtil.isZero( (String)c.getValue() ) ){
192                ISOVError e = new ISOVError(
193                        "Invalid Value Error. It can not be zero-filled. (Current value: " +
194                                c.getValue() + ") ",
195                        getRejCode( ISOVError.ERR_INVALID_VALUE ) );
196                if ( c instanceof ISOVField )
197                    ((ISOVField)c).addISOVError( e );
198                else
199                    c = new ISOVField( c, e );
200                if ( breakOnError )
201                    throw new ISOVException ( "Error on field " + c.getKey(), c );
202            }
203            return c;
204        }
205        catch (Exception ex) {
206            /** This catch is useful in case of error-dependencies. If
207             an error take place in super, and it imply second in child. **/
208            if ( ex instanceof ISOVException ) throw (ISOVException)ex;
209            return c;
210        }
211    }
212}