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

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

There is a newer version: 6.0.2
Show newest version
package at.spardat.enterprise.fmt;

import java.util.Locale;
import java.util.ResourceBundle;

import at.spardat.enterprise.util.Types;

/**
 * This class support formatting and parsing of boolean values.
 * It supports two styles:
* LONG the value is represented as "yes" or "no" in the chosen language.
* SHORT the value is represented as the first upper case character of "yes" or "no" in the chosen language.
* The language is determined by the locale passed the constructor or factory method. * * @author s2877 * @since 5.0.5 */ public class ABooleanFmt extends IFmt { /** * Style constant for short style. * the value is represented as the first upper case character of "yes" or "no" in the chosen language. * For english this is "Y" or "N". For german this is "J" or "N". */ public static final int SHORT = ADateFmt.SHORT; /** * Style constant for long style. * The value is represented as "yes" or "no" in the chosen language. * For german this is "ja" or "nein". */ public static final int LONG = ADateFmt.LONG; // language private Locale locale; // language specific translations of yes and no private String yes,no; /** * Creates a ABooleanFmt with the given style an locale. * @param style {@link #SHORT} or {@link #LONG}. {@link IFmt#DEFAULT} defaults to LONG. * @param locale defines the language */ public static ABooleanFmt getInstance(int style, Locale locale) { return new ABooleanFmt(style,locale); } /** * Creates a ABooleanFmt with the given style an locale. * @param style {@link #SHORT} or {@link #LONG}. {@link IFmt#DEFAULT} defaults to LONG. * @param locale defines the language */ public ABooleanFmt(int style, Locale locale) { this.style_=style; this.locale=locale; ResourceBundle messages = ResourceBundle.getBundle("at.spardat.enterprise.fmt.FmtErrors",locale); yes = messages.getString("yes"); no = messages.getString("no"); } /** * Transforms an internal encoding to an external. * It supports two styles:
* LONG the value is represented as "yes" or "no" in the chosen language.
* SHORT the value is represented as the first upper case character of "yes" or "no" in the chosen language.
* The language is determined by the locale passed the constructor or factory method. * @param internal the provided internal encoding. This String must be "J", "N" or "". * @return the external representation. May be the empty string, but is never null. */ public String format(String internal) { if(internal==null||internal.equals("")) return ""; String text; if(internal.equals("J")) { text = yes; } else if(internal.equals("N")) { text = no; } else return ""; if((style_&SHORT) != 0) { return text.substring(0,1).toUpperCase(); } else { return text; } } /** * Examines whether a given character may be part of an external representation. * No character should be suppressed for boolean input. * @param aChar the character to check * @return always true */ public boolean isLegalExternalChar(char char1) { return true; } /** * Determines if the given string is a legal internal representation. * Internal representation is "J" for true, "N" for false, "" for undefined. * @param internal the internal encoding. * @return true for "J","N",""; false otherwise */ public boolean isLegalInternal(String internal) { if(internal==null||internal.equals("")) return true; if(internal.equals("J")||internal.equals("N")) return true; return false; } /** * Tells, that this IFmt can format and parse. * @return false. */ public boolean isOneWay() { return false; } /** * Returns the maximum length an external string representation may have. * @return the maximum length of "yes" and "no" in the chosen language. */ public int maxLenOfExternal() { return Math.max(yes.length(),no.length()); } /** * 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}. * @return true if type is {@link at.spardat.enterprise.util.Types#T_BOOLEAN}, false otherwise. */ public boolean mayBeAppliedTo(byte type) { return type == Types.T_BOOLEAN; } /** * Tries to transform an external encoding to an internal. * If the language representation of "yes" starts with external, true is assumed. * If the language representation of "false" starts with external, false is assumed. * If none or both of the above applies an AParseException is thrown. * @param external the external encoding * @return the internal encoding. "J" for true, "N" for false, "" for undefined. * @exception AParseException if the external encoding cannot be successfully parsed. */ public String parse(String external) throws AParseException { if(external==null||external.length()==0) { if(isMandatory()) { throw new FmtParseException("NotEmpty"); } else { return ""; } } boolean isTrue = yes.startsWith(external.toLowerCase(locale)); boolean isFalse = no.startsWith(external.toLowerCase(locale)); if(isTrue&!isFalse) { return "J"; } else if(isFalse&&!isTrue) { return "N"; } else { if((style_&SHORT) != 0) { throw new FmtParseException("BooleanInput",yes.substring(0,1).toUpperCase(),no.substring(0,1).toUpperCase()); } else { throw new FmtParseException("BooleanInput",yes,no); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy