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

org.apache.commons.validator.routines.ByteValidator Maven / Gradle / Ivy

Go to download

Apache Commons Validator provides the building blocks for both client side validation and server side data validation. It may be used standalone or with a framework like Struts.

There is a newer version: 1.8.0
Show newest version
/*
 * 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 org.apache.commons.validator.routines;

import java.text.Format;
import java.util.Locale;

/**
 * 

Byte Validation and Conversion routines (java.lang.Byte).

* *

This validator provides a number of methods for * validating/converting a String value to * a Byte using java.text.NumberFormat * to parse either:

*
    *
  • using the default format for the default Locale
  • *
  • using a specified pattern with the default Locale
  • *
  • using the default format for a specified Locale
  • *
  • using a specified pattern with a specified Locale
  • *
* *

Use one of the isValid() methods to just validate or * one of the validate() methods to validate and receive a * converted Byte value.

* *

Once a value has been successfully converted the following * methods can be used to perform minimum, maximum and range checks:

*
    *
  • minValue() checks whether the value is greater * than or equal to a specified minimum.
  • *
  • maxValue() checks whether the value is less * than or equal to a specified maximum.
  • *
  • isInRange() checks whether the value is within * a specified range of values.
  • *
* *

So that the same mechanism used for parsing an input value * for validation can be used to format output, corresponding * format() methods are also provided. That is you can * format either:

*
    *
  • using the default format for the default Locale
  • *
  • using a specified pattern with the default Locale
  • *
  • using the default format for a specified Locale
  • *
  • using a specified pattern with a specified Locale
  • *
* * @version $Revision: 1739356 $ * @since Validator 1.3.0 */ public class ByteValidator extends AbstractNumberValidator { private static final long serialVersionUID = 7001640945881854649L; private static final ByteValidator VALIDATOR = new ByteValidator(); /** * Return a singleton instance of this validator. * @return A singleton instance of the ByteValidator. */ public static ByteValidator getInstance() { return VALIDATOR; } /** * Construct a strict instance. */ public ByteValidator() { this(true, STANDARD_FORMAT); } /** *

Construct an instance with the specified strict setting * and format type.

* *

The formatType specified what type of * NumberFormat is created - valid types * are:

*
    *
  • AbstractNumberValidator.STANDARD_FORMAT -to create * standard number formats (the default).
  • *
  • AbstractNumberValidator.CURRENCY_FORMAT -to create * currency number formats.
  • *
  • AbstractNumberValidator.PERCENT_FORMAT -to create * percent number formats (the default).
  • *
* * @param strict true if strict * Format parsing should be used. * @param formatType The NumberFormat type to * create for validation, default is STANDARD_FORMAT. */ public ByteValidator(boolean strict, int formatType) { super(strict, formatType, false); } /** *

Validate/convert a Byte using the default * Locale. * * @param value The value validation is being performed on. * @return The parsed Byte if valid or null * if invalid. */ public Byte validate(String value) { return (Byte)parse(value, (String)null, (Locale)null); } /** *

Validate/convert a Byte using the * specified pattern. * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against. * @return The parsed Byte if valid or null if invalid. */ public Byte validate(String value, String pattern) { return (Byte)parse(value, pattern, (Locale)null); } /** *

Validate/convert a Byte using the * specified Locale. * * @param value The value validation is being performed on. * @param locale The locale to use for the number format, system default if null. * @return The parsed Byte if valid or null if invalid. */ public Byte validate(String value, Locale locale) { return (Byte)parse(value, (String)null, locale); } /** *

Validate/convert a Byte using the * specified pattern and/ or Locale. * * @param value The value validation is being performed on. * @param pattern The pattern used to validate the value against, or the * default for the Locale if null. * @param locale The locale to use for the date format, system default if null. * @return The parsed Byte if valid or null if invalid. */ public Byte validate(String value, String pattern, Locale locale) { return (Byte)parse(value, pattern, locale); } /** * Check if the value is within a specified range. * * @param value The Number value to check. * @param min The minimum value of the range. * @param max The maximum value of the range. * @return true if the value is within the * specified range. */ public boolean isInRange(byte value, byte min, byte max) { return (value >= min && value <= max); } /** * Check if the value is within a specified range. * * @param value The Number value to check. * @param min The minimum value of the range. * @param max The maximum value of the range. * @return true if the value is within the * specified range. */ public boolean isInRange(Byte value, byte min, byte max) { return isInRange(value.byteValue(), min, max); } /** * Check if the value is greater than or equal to a minimum. * * @param value The value validation is being performed on. * @param min The minimum value. * @return true if the value is greater than * or equal to the minimum. */ public boolean minValue(byte value, byte min) { return (value >= min); } /** * Check if the value is greater than or equal to a minimum. * * @param value The value validation is being performed on. * @param min The minimum value. * @return true if the value is greater than * or equal to the minimum. */ public boolean minValue(Byte value, byte min) { return minValue(value.byteValue(), min); } /** * Check if the value is less than or equal to a maximum. * * @param value The value validation is being performed on. * @param max The maximum value. * @return true if the value is less than * or equal to the maximum. */ public boolean maxValue(byte value, byte max) { return (value <= max); } /** * Check if the value is less than or equal to a maximum. * * @param value The value validation is being performed on. * @param max The maximum value. * @return true if the value is less than * or equal to the maximum. */ public boolean maxValue(Byte value, byte max) { return maxValue(value.byteValue(), max); } /** *

Perform further validation and convert the Number to * a Byte.

* * @param value The parsed Number object created. * @param formatter The Format used to parse the value with. * @return The parsed Number converted to a * Byte if valid or null if invalid. */ @Override protected Object processParsedValue(Object value, Format formatter) { long longValue = ((Number)value).longValue(); if (longValue < Byte.MIN_VALUE || longValue > Byte.MAX_VALUE) { return null; } return Byte.valueOf((byte)longValue); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy