commons.validator.routines.AbstractFormatValidator Maven / Gradle / Ivy
Show all versions of android-saripaar Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package commons.validator.routines;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Locale;
import java.io.Serializable;
/**
* Abstract class for Format based Validation.
*
* This is a base class for building Date and Number
* Validators using format parsing.
*
* @version $Revision$
* @since Validator 1.3.0
*/
public abstract class AbstractFormatValidator implements Serializable {
private static final long serialVersionUID = -4690687565200568258L;
private final boolean strict;
/**
* Construct an instance with the specified strict setting.
*
* @param strict true
if strict
* Format
parsing should be used.
*/
public AbstractFormatValidator(boolean strict) {
this.strict = strict;
}
/**
* Indicates whether validated values should adhere
* strictly to the Format
used.
*
* Typically implementations of Format
* ignore invalid characters at the end of the value
* and just stop parsing. For example parsing a date
* value of 01/01/20x0
using a pattern
* of dd/MM/yyyy
will result in a year
* of 20
if strict
is set
* to false
, whereas setting strict
* to true
will cause this value to fail
* validation.
*
* @return true
if strict Format
* parsing should be used.
*/
public boolean isStrict() {
return strict;
}
/**
* Validate using the default Locale
.
*
* @param value The value validation is being performed on.
* @return true
if the value is valid.
*/
public boolean isValid(String value) {
return isValid(value, (String)null, (Locale)null);
}
/**
*
Validate using the specified pattern.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to validate the value against.
* @return true
if the value is valid.
*/
public boolean isValid(String value, String pattern) {
return isValid(value, pattern, (Locale)null);
}
/**
*
Validate using the specified Locale
.
*
* @param value The value validation is being performed on.
* @param locale The locale to use for the Format, defaults to the default
* @return true
if the value is valid.
*/
public boolean isValid(String value, Locale locale) {
return isValid(value, (String)null, locale);
}
/**
*
Validate using the specified pattern and/or Locale
.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @param locale The locale to use for the Format, defaults to the default
* @return true
if the value is valid.
*/
public abstract boolean isValid(String value, String pattern, Locale locale);
/**
*
Format an object into a String
using
* the default Locale.
*
* @param value The value validation is being performed on.
* @return The value formatted as a String
.
*/
public String format(Object value) {
return format(value, (String)null, (Locale)null);
}
/**
* Format an object into a String
using
* the specified pattern.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @return The value formatted as a String
.
*/
public String format(Object value, String pattern) {
return format(value, pattern, (Locale)null);
}
/**
* Format an object into a String
using
* the specified Locale.
*
* @param value The value validation is being performed on.
* @param locale The locale to use for the Format.
* @return The value formatted as a String
.
*/
public String format(Object value, Locale locale) {
return format(value, (String)null, locale);
}
/**
* Format an object using the specified pattern and/or
* Locale
.
*
* @param value The value validation is being performed on.
* @param pattern The pattern used to format the value.
* @param locale The locale to use for the Format.
* @return The value formatted as a String
.
*/
public String format(Object value, String pattern, Locale locale) {
Format formatter = getFormat(pattern, locale);
return format(value, formatter);
}
/**
*
Format a value with the specified Format
.
*
* @param value The value to be formatted.
* @param formatter The Format to use.
* @return The formatted value.
*/
protected String format(Object value, Format formatter) {
return formatter.format(value);
}
/**
* Parse the value with the specified Format
.
*
* @param value The value to be parsed.
* @param formatter The Format to parse the value with.
* @return The parsed value if valid or null
if invalid.
*/
protected Object parse(String value, Format formatter) {
ParsePosition pos = new ParsePosition(0);
Object parsedValue = formatter.parseObject(value, pos);
if (pos.getErrorIndex() > -1) {
return null;
}
if (isStrict() && pos.getIndex() < value.length()) {
return null;
}
if (parsedValue != null) {
parsedValue = processParsedValue(parsedValue, formatter);
}
return parsedValue;
}
/**
* Process the parsed value, performing any further validation
* and type conversion required.
*
* @param value The parsed object created.
* @param formatter The Format used to parse the value with.
* @return The parsed value converted to the appropriate type
* if valid or null
if invalid.
*/
protected abstract Object processParsedValue(Object value, Format formatter);
/**
* Returns a Format
for the specified pattern
* and/or Locale
.
*
* @param pattern The pattern used to validate the value against or
* null
to use the default for the Locale
.
* @param locale The locale to use for the currency format, system default if null.
* @return The NumberFormat
to created.
*/
protected abstract Format getFormat(String pattern, Locale locale);
}