Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.srplib.binding;
  
  import java.util.List;
  
An implementation of Binder which bind collection of Binding.

Author(s):
Anton Pechinsky
 
 public class ValueModelBinder<T> implements Binder<T> {
 
     private List<Bindingbindings = new ArrayList<Binding>();
 
 
     public ValueModelBinder(List<Bindingbindings) {
         Argument.checkNotNull(bindings"binding");
         this. = bindings;
     }
 
         ConverterRegistry converterRegistry = new ConverterRegistry();
         return converterRegistry;
     }
 
     public ValueModelBinder() {
         this(new ArrayList<Binding>());
     }
 
         return ;
     }
 
     public void setConverterRegistry(ConverterRegistry converterRegistry) {
         this. = converterRegistry;
     }

    
Adds binding to this binder.

Parameters:
binding Binding a binding definition.
 
     public void addBinding(Binding binding) {
         this..add(binding);
     }

    
Adds binding to this binder.

Call to this method is equivalent to

 addBinding(new DefaultBinding(source, target))
 

Parameters:
source ValueModel source model
target ValueModel target model
 
     public void addBinding(ValueModel sourceValueModel target) {
         addBinding(new DefaultBinding(sourcetarget));
     }
 
     @Override
     public void bind(T object) {
         for (Binding binding : ) {
             bind(objectbinding);
         }
     }
 
     private void bind(T objectBinding binding) {
         ValueModel<?> source = binding.getSource();
         setContextIfApplicable(objectsource);
 
         ValueModel target = binding.getTarget();
 
         Object value = source.getValue();
 
         // Use binding specific converter if exists otherwise use global converter
         Converter converter = binding.getConverter() != null
             ? binding.getConverter()
             : getConverterNullSafe(source.getType(), target.getType());
 
         Object convertedValue = converter.convert(value);
 
         // Set converted value to target model
        target.setValue(convertedValue);
    }
    @Override
    public T unbind(T object) {
        ValidationErrors validationErrors = unbind(objectnew ValidationErrors());
        if (validationErrors.hasErrors()) {
            throw new ValidationException(validationErrors);
        }
        return object;
    }
    @Override
    public ValidationErrors unbind(T objectValidationErrors errors) {
        for (Binding binding : ) {
            unbind(objectbindingerrors);
        }
        return errors;
    }
    private void unbind(T objectBinding bindingValidationErrors errors) {
        ValueModel<Objectsource = binding.getSource();
        setContextIfApplicable(objectsource);
        ValueModel target = binding.getTarget();
        Object value = target.getValue();
        // Use binding specific converter if exists and its type is TwoWayConverter otherwise use global converter
        Converter converter = binding.getConverter() instanceof TwoWayConverter
            ? new ConvertBackConverter((TwoWayConverter)binding.getConverter())
            : getConverterNullSafe(target.getType(), source.getType());
        Object convertedValue = converter.convert(value);
        // Set converted value to target model
        source.setValue(convertedValue);
    }
    private void setContextIfApplicable(T objectValueModel<?> valueModel) {
        if (valueModel instanceof ContextDependentValueModel) {
            ((ContextDependentValueModel<T, ?>)valueModel).setContext(object);
        }
    }
    private Converter getConverterNullSafe(Class<?> sourceClass<?> target) {
        Converter converter = null;
        if (target.isAssignableFrom(source)) {
            converter = EmptyConverter.instance();
        }
        else {
            converter = .getConverter(sourcetarget);
        }
        Assert.checkNotNull(converter"No converter from type '" + source + "' to type '" + target + "'.");
        return converter;
    }
New to GrepCode? Check out our FAQ X