View Javadoc

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.NoSuchElementException;
19  
20  import org.joda.beans.Bean;
21  import org.joda.beans.MetaBean;
22  import org.joda.beans.MetaProperty;
23  import org.joda.beans.PropertyMap;
24  
25  /**
26   * Basic implementation of {@code MetaBean}.
27   * 
28   * @author Stephen Colebourne
29   */
30  public abstract class BasicMetaBean implements MetaBean {
31  
32      @Override
33      public PropertyMap createPropertyMap(Bean bean) {
34          return BasicPropertyMap.of(bean);
35      }
36  
37      @Override
38      public String beanName() {
39          return beanType().getName();
40      }
41  
42      @Override
43      public int metaPropertyCount() {
44          return metaPropertyMap().size();
45      }
46  
47      @Override
48      public boolean metaPropertyExists(String propertyName) {
49          return metaPropertyMap().containsKey(propertyName);
50      }
51  
52      @SuppressWarnings("unchecked")
53      @Override
54      public <R> MetaProperty<R> metaProperty(String propertyName) {
55          MetaProperty<?> mp = metaPropertyMap().get(propertyName);
56          if (mp == null) {
57              throw new NoSuchElementException("Unknown property: " + propertyName);
58          }
59          return (MetaProperty<R>) mp;
60      }
61  
62      @Override
63      public Iterable<MetaProperty<?>> metaPropertyIterable() {
64          return metaPropertyMap().values();
65      }
66  
67      //-----------------------------------------------------------------------
68      /**
69       * Returns a string that summarises the meta-bean.
70       * 
71       * @return a summary string, not null
72       */
73      @Override
74      public String toString() {
75          return "MetaBean:" + beanType().getSimpleName();
76      }
77  
78  }