1 /*
2 * Copyright 2001-2013 Stephen Colebourne
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.joda.beans.impl;
17
18 import java.util.Map;
19 import java.util.Map.Entry;
20
21 import org.joda.beans.Bean;
22 import org.joda.beans.BeanBuilder;
23 import org.joda.beans.MetaProperty;
24
25 /**
26 * Basic implementation of {@code BeanBuilder} that wraps a real bean.
27 * <p>
28 * This approach saves creating a temporary map, but is only suitable if the
29 * bean has a no-arg constructor and allows properties to be set.
30 *
31 * @author Stephen Colebourne
32 * @param <T> the bean type
33 */
34 public class BasicBeanBuilder<T extends Bean> implements BeanBuilder<T> {
35
36 /**
37 * The actual target bean.
38 */
39 private final T bean;
40
41 /**
42 * Constructs the builder wrapping the target bean.
43 *
44 * @param bean the target bean, not null
45 */
46 public BasicBeanBuilder(T bean) {
47 if (bean == null) {
48 throw new NullPointerException("Bean must not be null");
49 }
50 this.bean = bean;
51 }
52
53 //-----------------------------------------------------------------------
54 /**
55 * Gets the target bean.
56 *
57 * @return the target bean, not null
58 */
59 protected T getTargetBean() {
60 return bean;
61 }
62
63 /**
64 * Gets the current value of the property.
65 *
66 * @param propertyName the property name, not null
67 * @return the current value in the builder, null if not found or value is null
68 */
69 protected BeanBuilder<T> get(String propertyName) {
70 bean.property(propertyName).get();
71 return this;
72 }
73
74 //-----------------------------------------------------------------------
75 @Override
76 public BeanBuilder<T> set(String propertyName, Object value) {
77 return set(bean.metaBean().metaProperty(propertyName), value);
78 }
79
80 @Override
81 public BeanBuilder<T> set(MetaProperty<?> property, Object value) {
82 property.set(bean, value);
83 return this;
84 }
85
86 @Override
87 public BeanBuilder<T> setString(String propertyName, String value) {
88 return setString(bean.metaBean().metaProperty(propertyName), value);
89 }
90
91 @Override
92 public BeanBuilder<T> setString(MetaProperty<?> property, String value) {
93 property.setString(bean, value);
94 return this;
95 }
96
97 @Override
98 public BeanBuilder<T> setAll(Map<String, ? extends Object> propertyValueMap) {
99 for (Entry<String, ? extends Object> entry : propertyValueMap.entrySet()) {
100 set(entry.getKey(), entry.getValue());
101 }
102 return this;
103 }
104
105 @Override
106 public T build() {
107 validate(bean);
108 return bean;
109 }
110
111 /**
112 * Hook to allow a subclass to validate the bean.
113 *
114 * @param bean the bean to validate, not null
115 */
116 protected void validate(T bean) {
117 // override to validate the bean
118 }
119
120 //-----------------------------------------------------------------------
121 /**
122 * Returns a string that summarises the builder.
123 *
124 * @return a summary string, not null
125 */
126 @Override
127 public String toString() {
128 return "BeanBuilder for " + bean.metaBean().beanName();
129 }
130
131 }