Author: tchemit Date: 2011-03-24 13:10:07 +0100 (Thu, 24 Mar 2011) New Revision: 2090 Url: http://nuiton.org/repositories/revision/nuiton-utils/2090 Log: Evolution #1419: add toBinder method on binderModelBuilder Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderFactory.java trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderFactory.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderFactory.java 2011-03-17 16:48:18 UTC (rev 2089) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderFactory.java 2011-03-24 12:10:07 UTC (rev 2090) @@ -202,6 +202,37 @@ } /** + * Given a {@code model} and a {@code binderType}, instanciate a new binder + * and returns it. + * + * <strong>Note: </strong> This method will <strong>NOT</strong> register + * the model in the factory. If you want to reuse your model, please use + * one of the {@code registerBinderModel(XXX)} method. + * + * @param model the model used by the binder + * @param binderType the type of the binder + * @param <S> the source type + * @param <T> the target type + * @param <B> the type of the binder + * @return the new instanciated binder + * @since 2.1 + */ + public static <S, T, B extends Binder<S, T>> B newBinder(Binder.BinderModel<S, T> model, + Class<B> binderType) { + + B binder; + try { + binder = binderType.getConstructor().newInstance(); + } catch (Exception e) { + throw new IllegalStateException( + "Could not instanciate binder of type " + binderType, e); + } + + binder.setModel(model); + return binder; + } + + /** * Clear the cache of registred binder models. * <p/> * <b>Note :<b> This is a convienient method for test purposes and should @@ -306,14 +337,7 @@ model = registerBinderModel(builder, contextName); } - B binder; - try { - binder = binderType.newInstance(); - } catch (Exception e) { - throw new IllegalStateException("Could not instanciate binder of type " + binderType, e); - } - - binder.setModel(model); + B binder = newBinder(model, binderType); return binder; } Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java 2011-03-17 16:48:18 UTC (rev 2089) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BinderModelBuilder.java 2011-03-24 12:10:07 UTC (rev 2090) @@ -89,7 +89,7 @@ * builder.addSimpleProperties("name", "surname"); * BinderFactory.registerBinderModel(builder); * Binder<Bean, Bean> binder = BinderFactory.getBinder(BeanA.class); - * <p/> + * * </pre> * <p/> * Once the binder is registred into the {@link BinderFactory}, you can get it @@ -195,8 +195,42 @@ return builder; } + /** + * Convinient method to create directly a {@link Binder} using the + * underlying {@link #model} the builder contains. + * <p/> + * <strong>Note:</strong> Using this method will not make reusable the model + * via the {@link BinderFactory}. + * + * @return a new binder using the model of the builder. + * @see BinderFactory#newBinder(Binder.BinderModel, Class) + * @since 2.1 + */ + public Binder<S, T> toBinder() { + Binder<S, T> binder = toBinder(Binder.class); + return binder; + } /** + * Convinient method to create directly a {@link Binder} using the + * underlying {@link #model} the builder contains. + * <p/> + * <strong>Note:</strong> Using this method will not make reusable the model + * via the {@link BinderFactory}. + * + * @param binderType type of binder to create + * @param <B> type of binder to create + * @return a new binder using the model of the builder. + * @see BinderFactory#newBinder(Binder.BinderModel, Class) + * @since 2.1 + */ + public <B extends Binder<S, T>> B toBinder(Class<B> binderType) { + B binder = BinderFactory.newBinder(model, binderType); + return binder; + } + + + /** * Add to the binder model some simple properties (says source property name * = target property name). * <p/>