/src/main/org/bson/BasicBSONObject.java

https://github.com/tqthe/mongo-java-driver · Java · 228 lines · 103 code · 32 blank · 93 comment · 26 complexity · 37aad1a858ba178d67da1b8dbfb48305 MD5 · raw file

  1. // BasicBSONObject.java
  2. /**
  3. * Copyright (C) 2008 10gen Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.bson;
  18. import java.util.*;
  19. import org.bson.*;
  20. /**
  21. * A simple implementation of <code>DBObject</code>.
  22. * A <code>DBObject</code> can be created as follows, using this class:
  23. * <blockquote><pre>
  24. * DBObject obj = new BasicBSONObject();
  25. * obj.put( "foo", "bar" );
  26. * </pre></blockquote>
  27. */
  28. public class BasicBSONObject extends LinkedHashMap<String,Object> implements BSONObject {
  29. /**
  30. * Creates an empty object.
  31. */
  32. public BasicBSONObject(){
  33. }
  34. /**
  35. * Convenience CTOR
  36. * @param key key under which to store
  37. * @param value value to stor
  38. */
  39. public BasicBSONObject(String key, Object value){
  40. put(key, value);
  41. }
  42. /**
  43. * Creates a DBObject from a map.
  44. * @param m map to convert
  45. */
  46. public BasicBSONObject(Map m) {
  47. super(m);
  48. }
  49. /**
  50. * Converts a DBObject to a map.
  51. * @return the DBObject
  52. */
  53. public Map toMap() {
  54. return new LinkedHashMap<String,Object>(this);
  55. }
  56. /** Deletes a field from this object.
  57. * @param key the field name to remove
  58. * @return the object removed
  59. */
  60. public Object removeField( String key ){
  61. return remove( key );
  62. }
  63. /** Checks if this object contains a given field
  64. * @param field field name
  65. * @return if the field exists
  66. */
  67. public boolean containsField( String field ){
  68. return super.containsKey(field);
  69. }
  70. /**
  71. * @deprecated
  72. */
  73. public boolean containsKey( String key ){
  74. return containsField(key);
  75. }
  76. /** Gets a value from this object
  77. * @param key field name
  78. * @return the value
  79. */
  80. public Object get( String key ){
  81. return super.get(key);
  82. }
  83. /** Returns the value of a field as an <code>int</code>.
  84. * @param key the field to look for
  85. * @return the field value (or default)
  86. */
  87. public int getInt( String key ){
  88. Object o = get(key);
  89. if ( o == null )
  90. throw new NullPointerException( "no value for: " + key );
  91. return BSON.toInt( o );
  92. }
  93. /** Returns the value of a field as an <code>int</code>.
  94. * @param key the field to look for
  95. * @param def the default to return
  96. * @return the field value (or default)
  97. */
  98. public int getInt( String key , int def ){
  99. Object foo = get( key );
  100. if ( foo == null )
  101. return def;
  102. return BSON.toInt( foo );
  103. }
  104. /**
  105. * Returns the value of a field as a <code>long</code>.
  106. *
  107. * @param key the field to return
  108. * @return the field value
  109. */
  110. public long getLong( String key){
  111. Object foo = get( key );
  112. return ((Number)foo).longValue();
  113. }
  114. /**
  115. * Returns the value of a field as a <code>double</code>.
  116. *
  117. * @param key the field to return
  118. * @return the field value
  119. */
  120. public double getDouble( String key){
  121. Object foo = get( key );
  122. return ((Number)foo).doubleValue();
  123. }
  124. /** Returns the value of a field as a string
  125. * @param key the field to look up
  126. * @return the value of the field, converted to a string
  127. */
  128. public String getString( String key ){
  129. Object foo = get( key );
  130. if ( foo == null )
  131. return null;
  132. return foo.toString();
  133. }
  134. /** Add a key/value pair to this object
  135. * @param key the field name
  136. * @param val the field value
  137. * @return the <code>val</code> parameter
  138. */
  139. public Object put( String key , Object val ){
  140. return super.put( key , val );
  141. }
  142. public void putAll( Map m ){
  143. for ( Map.Entry entry : (Set<Map.Entry>)m.entrySet() ){
  144. put( entry.getKey().toString() , entry.getValue() );
  145. }
  146. }
  147. public void putAll( BSONObject o ){
  148. for ( String k : o.keySet() ){
  149. put( k , o.get( k ) );
  150. }
  151. }
  152. /** Add a key/value pair to this object
  153. * @param key the field name
  154. * @param val the field value
  155. * @return the <code>val</code> parameter
  156. */
  157. public BasicBSONObject append( String key , Object val ){
  158. put( key , val );
  159. return this;
  160. }
  161. /** Returns a JSON serialization of this object
  162. * @return JSON serialization
  163. */
  164. public String toString(){
  165. return com.mongodb.util.JSON.serialize( this );
  166. }
  167. public boolean equals( Object o ){
  168. if ( ! ( o instanceof BSONObject ) )
  169. return false;
  170. BSONObject other = (BSONObject)o;
  171. if ( ! keySet().equals( other.keySet() ) )
  172. return false;
  173. for ( String key : keySet() ){
  174. Object a = get( key );
  175. Object b = other.get( key );
  176. if ( a == null ){
  177. if ( b != null )
  178. return false;
  179. }
  180. if ( b == null ){
  181. if ( a != null )
  182. return false;
  183. }
  184. else if ( a instanceof Number && b instanceof Number ){
  185. if ( ((Number)a).doubleValue() !=
  186. ((Number)b).doubleValue() )
  187. return false;
  188. }
  189. else {
  190. if ( ! a.equals( b ) )
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. }