Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * Licensed 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.foxlabs.validation;
 
 
Defines a factory that allows to create Validator instances.

The main purpose of this factory is maintaining validators configuration. You can configure factory once and then create validators with different metadata.

Author(s):
Fox Mulder
See also:
Validator
 
 public class ValidatorFactory {
    
    
Default pattern for integer numbers.
 
     protected String integerPattern = null;
    
    
Default pattern for decimal numbers.
 
     protected String decimalPattern = null;
    
    
Default pattern for dates.
 
     protected String datePattern = null;
    
    
Default date style.
 
     protected int dateStyle = .;
    
    
Default time style.
 
     protected int timeStyle = .;
    
    
Message resolver to be used for resolving localized messages.
 
     protected MessageResolver messageResolver = .;
    
    
Message builder to be used for building error messages.
 
     protected MessageBuilder messageBuilder = .;
    
    
Constructs default ValidatorFactory.
 
     public ValidatorFactory() {}
    
    
Constructs a new ValidatorFactory with the specified message bundle name.

Parameters:
bundleName Message bundle name.
 
     public ValidatorFactory(String bundleName) {
         this. = new ResourceMessageResolver(bundleName);
     }
    
    
Sets default format of integer numbers according to the specified pattern.

Parameters:
pattern Default pattern for integer numbers.
Returns:
Reference to this factory instance.
 
     public ValidatorFactory setIntegerFormat(String pattern) {
         this. = pattern == null ? null : new DecimalFormat(pattern).toPattern();
        return this;
    }
    
    
Sets default format of decimal numbers according to the specified pattern.

Parameters:
pattern Default pattern for decimal numbers.
Returns:
Reference to this factory instance.
    public ValidatorFactory setDecimalFormat(String pattern) {
        this. = pattern == null ? null : new DecimalFormat(pattern).toPattern();
        return this;
    }
    
    
Sets default format of dates according to the specified pattern.

Parameters:
pattern Default pattern for dates.
Returns:
Reference to this factory instance.
    public ValidatorFactory setDateFormat(String pattern) {
        this. = pattern == null ? null : new SimpleDateFormat(pattern).toPattern();
        return this;
    }
    
    
Sets default format of dates according to the specified date and time styles. If negative value will be specified as date or time style (but not both) then corresponding part would not be used.

Parameters:
dateStyle Default date style.
timeStyle Default time style.
Returns:
Reference to this factory instance.
    public ValidatorFactory setDateFormat(int dateStyleint timeStyle) {
        if (timeStyle < 0) {
            DateFormat.getDateInstance(dateStyle);
        } else if (dateStyle < 0) {
            DateFormat.getTimeInstance(timeStyle);
        } else {
            DateFormat.getDateTimeInstance(dateStyletimeStyle);
        }
        this. = dateStyle;
        this. = timeStyle;
        this. = null;
        return this;
    }
    
    
Sets message resolver to be used for resolving localized messages.

Parameters:
resolver Message resolver to be used for resolving localized messages.
Returns:
Reference to this factory instance.
        this. = resolver == null ? . : resolver;
        return this;
    }
    
    
Sets message builder to be used for building error messages.

Parameters:
builder Message builder to be used for building error messages.
Returns:
Reference to this factory instance.
        this. = builder == null ? . : builder;
        return this;
    }
    
    
Creates a new validator for the specified bean type using factory configuration.

Parameters:
type Bean type for which validator should be created.
Returns:
Validator for the specified bean type.
    public <T> Validator<T> newValidator(Class<T> type) {
        return newValidator(BeanMetaData.<T>getMetaData(type));
    }
    
    
Creates a new validator for the specified bean type using configuration from another validator.

Parameters:
type Bean type for which validator should be created.
config Validator whose configuration should be taken.
Returns:
Validator for the specified bean type.
    public <T> Validator<T> newValidator(Class<T> typeValidator<?> config) {
        return newValidator(BeanMetaData.<T>getMetaData(type), config);
    }
    
    
Creates a new validator for the specified entity metadata using factory configuration.

Parameters:
metadata Entity metadata for which validator should be created.
Returns:
Validator for the specified entity metadata.
    public <T> Validator<T> newValidator(EntityMetaData<T> metadata) {
        return new Validator<T>(thismetadata,
                );
    }
    
    
Creates a new validator for the specified entity metadata using configuration from another validator.

Parameters:
metadata Entity metadata.
config Validator whose configuration should be taken.
Returns:
Validator for the specified entity metadata.
    public <T> Validator<T> newValidator(EntityMetaData<T> metadataValidator<?> config) {
        return new Validator<T>(thismetadata,
                config.messageResolverconfig.messageBuilder,
                config.defaultIntegerPatternconfig.defaultDecimalPattern,
                config.defaultDatePatternconfig.defaultDateStyleconfig.defaultTimeStyle);
    }
    
    
Returns immutable validator factory configured by default.

Returns:
Immutable validator factory configured by default.
    public static ValidatorFactory getDefault() {
        return ;
    }
    
    
Validator factory configured by default.
    private static final ValidatorFactory DEFAULT_FACTORY = new ValidatorFactory() {
        @Override public ValidatorFactory setIntegerFormat(String pattern) {
            throw new UnsupportedOperationException();
        }
        @Override public ValidatorFactory setDecimalFormat(String pattern) {
            throw new UnsupportedOperationException();
        }
        @Override public ValidatorFactory setDateFormat(String pattern) {
            throw new UnsupportedOperationException();
        }
        @Override public ValidatorFactory setDateFormat(int dateStyleint timeStyle) {
            throw new UnsupportedOperationException();
        }
        @Override public ValidatorFactory setMessageResolver(MessageResolver resolver) {
            throw new UnsupportedOperationException();
        }
        @Override public ValidatorFactory setMessageBuilder(MessageBuilder builder) {
            throw new UnsupportedOperationException();
        }
    };
    
New to GrepCode? Check out our FAQ X