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.integrate.mongo;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.joda.beans.Bean;
23  
24  import com.mongodb.DBObject;
25  
26  /**
27   * Allows a Joda-Bean to be passed directly to MongoDB.
28   *
29   * @author Stephen Colebourne
30   */
31  public class BeanMongoDBObject implements DBObject {
32  
33      /**
34       * The underlying bean.
35       */
36      private final Bean bean;
37      /**
38       * The Mongo partial flag.
39       */
40      private boolean partial;
41  
42      /**
43       * Creates an instance wrapping a bean.
44       * @param bean  the bean to wrap, not null
45       */
46      public BeanMongoDBObject(Bean bean) {
47          this.bean = bean;
48      }
49  
50      //-----------------------------------------------------------------------
51      @Override
52      public boolean containsField(String name) {
53          return bean.propertyNames().contains(name);
54      }
55  
56      /**
57       * {@inheritDoc}
58       * @deprecated Use containsField()
59       */
60      @Override
61      @Deprecated
62      public boolean containsKey(String name) {
63          return containsField(name);
64      }
65  
66      @Override
67      public Object get(String name) {
68          return bean.property(name).get();
69      }
70  
71      @Override
72      public Object put(String name, Object value) {
73          return bean.property(name).put(value);
74      }
75  
76      @Override
77      public void putAll(DBObject object) {
78          for (String name : object.keySet()) {
79              put(name, object.get(name));
80          }
81      }
82  
83      @Override
84      @SuppressWarnings("rawtypes")
85      public void putAll(Map map) {
86          for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
87              Object key = (Object) it.next();
88              put(key.toString(), map.get(key));
89          }
90      }
91  
92      @Override
93      public Object removeField(String name) {
94          throw new UnsupportedOperationException("Remove unsupported");
95      }
96  
97      @Override
98      public Set<String> keySet() {
99          return bean.propertyNames();
100     }
101 
102     @Override
103     @SuppressWarnings("rawtypes")
104     public Map toMap() {
105         return bean.metaBean().createPropertyMap(bean).flatten();
106     }
107 
108     //-----------------------------------------------------------------------
109     @Override
110     public boolean isPartialObject() {
111         return partial;
112     }
113 
114     @Override
115     public void markAsPartialObject() {
116         partial = true;
117     }
118 
119 }