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 */ 016package org.joda.beans.impl.map; 017 018import java.util.AbstractMap; 019import java.util.AbstractSet; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.Map; 024import java.util.Set; 025 026import org.joda.beans.Property; 027import org.joda.beans.PropertyMap; 028import org.joda.beans.impl.BasicProperty; 029 030/** 031 * A map of properties for a {@code MapBean}. 032 * 033 * @author Stephen Colebourne 034 */ 035public final class MapBeanPropertyMap 036 extends AbstractMap<String, Property<?>> implements PropertyMap { 037 038 /** The bean. */ 039 private final MapBean bean; 040 041 /** 042 * Factory to create a property map avoiding duplicate generics. 043 * 044 * @param bean the bean, not null 045 * @return the property map, not null 046 */ 047 public static MapBeanPropertyMap of(MapBean bean) { 048 return new MapBeanPropertyMap(bean); 049 } 050 051 /** 052 * Creates a property map. 053 * 054 * @param bean the bean that the property is bound to, not null 055 */ 056 private MapBeanPropertyMap(MapBean bean) { 057 if (bean == null) { 058 throw new NullPointerException("Bean must not be null"); 059 } 060 this.bean = bean; 061 } 062 063 //----------------------------------------------------------------------- 064 @Override 065 public int size() { 066 return bean.size(); 067 } 068 069 @Override 070 public boolean containsKey(Object obj) { 071 return bean.containsKey(obj); 072 } 073 074 @Override 075 public Property<?> get(Object obj) { 076 return containsKey(obj) ? bean.property(obj.toString()) : null; 077 } 078 079 @Override 080 public Set<String> keySet() { 081 return bean.keySet(); 082 } 083 084 @Override 085 public Set<Entry<String, Property<?>>> entrySet() { 086 return new AbstractSet<Entry<String, Property<?>>>() { 087 // TODO: possibly override contains() 088 @Override 089 public int size() { 090 return bean.size(); 091 } 092 @Override 093 public Iterator<Entry<String, Property<?>>> iterator() { 094 final Iterator<String> it = bean.keySet().iterator(); 095 return new Iterator<Entry<String, Property<?>>>() { 096 @Override 097 public boolean hasNext() { 098 return it.hasNext(); 099 } 100 @Override 101 public Entry<String, Property<?>> next() { 102 String name = it.next(); 103 Property<?> prop = BasicProperty.of(bean, MapBeanMetaProperty.of(bean, name)); 104 return new SimpleImmutableEntry<String, Property<?>>(name, prop); 105 } 106 @Override 107 public void remove() { 108 throw new UnsupportedOperationException("Unmodifiable"); 109 } 110 }; 111 } 112 }; 113 } 114 115 //----------------------------------------------------------------------- 116 @Override 117 public Map<String, Object> flatten() { 118 Map<String, Object> copy = new HashMap<String, Object>(bean); 119 return Collections.unmodifiableMap(copy); 120 } 121 122}