1 /* 2 * #%L 3 * EmptyDocument.java - mongodb-async-driver - Allanbank Consulting, Inc. 4 * %% 5 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. 6 * %% 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * #L% 19 */ 20 package com.allanbank.mongodb.bson.impl; 21 22 import java.util.Collections; 23 import java.util.List; 24 import java.util.Map; 25 26 import com.allanbank.mongodb.bson.Element; 27 28 /** 29 * An immutable empty document. 30 * 31 * @api.yes This interface is part of the driver's API. Public and protected 32 * members will be deprecated for at least 1 non-bugfix release 33 * (version numbers are <major>.<minor>.<bugfix>) 34 * before being removed or modified. 35 * @copyright 2011, Allanbank Consulting, Inc., All Rights Reserved 36 */ 37 public class EmptyDocument extends AbstractDocument { 38 39 /** An instance of the Empty Document. */ 40 public static final EmptyDocument INSTANCE = new EmptyDocument(); 41 42 /** 43 * The bytes to encode an empty document. Length (4) + terminal null (1) = 44 * 5. 45 */ 46 public static final int SIZE = 5; 47 48 /** Serialization version for the class. */ 49 private static final long serialVersionUID = -2775918328146027036L; 50 51 /** 52 * Constructs a new {@link EmptyDocument}. 53 */ 54 public EmptyDocument() { 55 super(); 56 } 57 58 /** 59 * {@inheritDoc} 60 * <p> 61 * Overridden to return an empty list of elements. 62 * </p> 63 * 64 * @return The elements in the document. 65 */ 66 @Override 67 public List<Element> getElements() { 68 return Collections.emptyList(); 69 } 70 71 /** 72 * Returns the size of the empty document when encoded as bytes. This is 73 * always 5 bytes. 74 * 75 * @return The size of the document when encoded as bytes. 76 */ 77 @Override 78 public long size() { 79 return SIZE; // length (4) + terminal null (1). 80 } 81 82 /** 83 * {@inheritDoc} 84 * <p> 85 * Overridden to return an empty map as this document is always empty. 86 * </p> 87 */ 88 @Override 89 protected Map<String, Element> getElementMap() { 90 return Collections.emptyMap(); 91 } 92 }