001 /*
002 * Copyright 2001-2013 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.beans.impl;
017
018 import java.util.NoSuchElementException;
019
020 import org.joda.beans.Bean;
021 import org.joda.beans.MetaBean;
022 import org.joda.beans.MetaProperty;
023 import org.joda.beans.PropertyMap;
024
025 /**
026 * Basic implementation of {@code MetaBean}.
027 *
028 * @author Stephen Colebourne
029 */
030 public abstract class BasicMetaBean implements MetaBean {
031
032 @Override
033 public PropertyMap createPropertyMap(Bean bean) {
034 return BasicPropertyMap.of(bean);
035 }
036
037 @Override
038 public String beanName() {
039 return beanType().getName();
040 }
041
042 @Override
043 public int metaPropertyCount() {
044 return metaPropertyMap().size();
045 }
046
047 @Override
048 public boolean metaPropertyExists(String propertyName) {
049 return metaPropertyMap().containsKey(propertyName);
050 }
051
052 @SuppressWarnings("unchecked")
053 @Override
054 public <R> MetaProperty<R> metaProperty(String propertyName) {
055 MetaProperty<?> mp = metaPropertyMap().get(propertyName);
056 if (mp == null) {
057 throw new NoSuchElementException("Unknown property: " + propertyName);
058 }
059 return (MetaProperty<R>) mp;
060 }
061
062 @Override
063 public Iterable<MetaProperty<?>> metaPropertyIterable() {
064 return metaPropertyMap().values();
065 }
066
067 //-----------------------------------------------------------------------
068 /**
069 * Returns a string that summarises the meta-bean.
070 *
071 * @return a summary string, not null
072 */
073 @Override
074 public String toString() {
075 return "MetaBean:" + beanType().getSimpleName();
076 }
077
078 }