All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.fmt.IFmt Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

// @(#) $Id: IFmt.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.fmt;


/**
 * IFmt objects are responsible for converting an internal string encoding of a particular
 * AtomicAttrVal type to an UI encoding and vice versa.
 */
public abstract class IFmt implements Cloneable {
    
    /**
     * May be used in subclasses where ever a style is needed.
     */
    public static final int             DEFAULT = 1;
    
    /**
     * Used to express that any provided external input must not be empty. 
     * Method parse throws an exception if empty input is provided.
     */
    public static final int             MANDATORY = DEFAULT*2;
    
    /**
     * For subclasses which should start their style numbering at LAST_STYLE*2;
     */
    public static final int             LAST_STYLE = MANDATORY;
    
    /**
     * Style-field that may also be used by subclasses. Within this class, only the
     * style MANDATORY is known.
     */
    protected int                       style_;
    

    /**
     * Tries to transform an external encoding to an internal. Must not be called if
     * isOneWay() yields true.
     * 
     * @param external the external encoding
     * @return the internal encoding. May be the empty String, but never is null.
     * @exception AParseException if the external encoding cannot be successfully parsed.
     */
    abstract public String parse (String external) throws AParseException;

    /**
     * Transforms an internal encoding to an external.
     * 
     * @param internal the provided internal encoding. This String must satisfy the
     *         condition isLegalInternal(), otherwise the behaviour of this
     *         method is undefined.
     * @return the external representation. May be the empty string, but is never null.
     */     
    abstract public String format (String internal);

    /**
     * Returns the maximum length an external string representation may have.
     * 
     * @return the maximum length or -1 if there is no known maximum.
     */     
    abstract public int maxLenOfExternal ();

    /**
     * Examines whether a given character may be part of an external representation.
     * 
     * @param aChar the character to check
     * @return boolean if the character may be part of an external rep.
     */ 
    abstract public boolean isLegalExternalChar (char aChar);

    /**
     * Determines if the given string is a legal internal representation.
     * 
     * @param internal the internal encoding.
     * @return whether the provided string follows a legal internal encoding.
     */ 
    abstract public boolean isLegalInternal (String internal);
    
    /**
     * An IFmt is one way, if the external encoding may be generated out of
     * the internal, but not vice versa.
     * 
     * @return true, if format may be called, but parse not.
     */ 
    abstract public boolean isOneWay();
    
    /**
     * Defines if this formatter is able to successfully format a specified type.
     * 
     * @param type a type constant defined in {@link at.spardat.enterprise.util.Types Types}.
     */
    abstract public boolean mayBeAppliedTo (byte type);
    
    /**
     * Returns true, if this formatter only accepts non-empty input.
     */
    public boolean isMandatory () {
        return (style_ & MANDATORY) != 0;
    }
    
    /**
     * Sets the mandatory-property to the provided value. Mandatory indicates that
     * empty values are not accepted by this formatter. 
     */
    public void setMandatory (boolean what) {
        if (what) style_ |= MANDATORY;
        else      style_ &= ~MANDATORY;
    }
    
    /**
     * Returns the style-property. May be used to check if a particular style-bit is set, 
     * e.g., if((getStyle() & SOMESTYLE) != 0) ....
     */
    public int getStyle () {
        return style_;
    }
    
    /**
     * Utility method to check a provided String for emptyness if the MANDATORY style
     * is present. 
     * 
     * @exception AParseException if isMandatory() and the provided String
     *             is either null or empty.
     */
    public void checkMandatory (String internal) throws AParseException {
        if (isMandatory()) {
            if (internal == null || internal.length() == 0) {
                throw new FmtParseException ("NotEmpty");
            }
        }
    }
    
    /**
     * Returns a clone of this;
     */
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException ex) {
            ex.printStackTrace();  // does not happen
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy