1 /*
2 * #%L
3 * MinKeyElement.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.element;
21
22 import com.allanbank.mongodb.bson.Element;
23 import com.allanbank.mongodb.bson.ElementType;
24 import com.allanbank.mongodb.bson.Visitor;
25 import com.allanbank.mongodb.bson.io.StringEncoder;
26
27 /**
28 * A wrapper for a BSON minimum key element.
29 *
30 * @api.yes This class is part of the driver's API. Public and protected members
31 * will be deprecated for at least 1 non-bugfix release (version
32 * numbers are <major>.<minor>.<bugfix>) before being
33 * removed or modified.
34 * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
35 */
36 public class MinKeyElement extends AbstractElement {
37
38 /** The BSON type for a binary. */
39 public static final ElementType TYPE = ElementType.MIN_KEY;
40
41 /** Serialization version for the class. */
42 private static final long serialVersionUID = 5715544436237499313L;
43
44 /**
45 * Computes and returns the number of bytes that are used to encode the
46 * element.
47 *
48 * @param name
49 * The name for the element.
50 * @return The size of the element when encoded in bytes.
51 */
52 private static long computeSize(final String name) {
53 long result = 2; // type (1) + name null byte (1).
54 result += StringEncoder.utf8Size(name);
55
56 return result;
57 }
58
59 /**
60 * Constructs a new {@link MinKeyElement}.
61 *
62 * @param name
63 * The name for the BSON minimum key.
64 * @throws IllegalArgumentException
65 * If the {@code name} is <code>null</code>.
66 */
67 public MinKeyElement(final String name) {
68 this(name, computeSize(name));
69 }
70
71 /**
72 * Constructs a new {@link MinKeyElement}.
73 *
74 * @param name
75 * The name for the BSON minimum key.
76 * @param size
77 * The size of the element when encoded in bytes. If not known
78 * then use the {@link MinKeyElement#MinKeyElement(String)}
79 * constructor instead.
80 * @throws IllegalArgumentException
81 * If the {@code name} is <code>null</code>.
82 */
83 public MinKeyElement(final String name, final long size) {
84 super(name, size);
85 }
86
87 /**
88 * Accepts the visitor and calls the {@link Visitor#visitMinKey} method.
89 *
90 * @see Element#accept(Visitor)
91 */
92 @Override
93 public void accept(final Visitor visitor) {
94 visitor.visitMinKey(getName());
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public ElementType getType() {
102 return TYPE;
103 }
104
105 /**
106 * {@inheritDoc}
107 * <p>
108 * Returns a {@link Double} with the value {@link Double#NEGATIVE_INFINITY}.
109 * </p>
110 * <p>
111 * <b>Note:</b> This value will not be recreated is a Object-->Element
112 * conversion. Double with the {@link Double#NEGATIVE_INFINITY} value is
113 * created instead.
114 * </p>
115 */
116 @Override
117 public Double getValueAsObject() {
118 return Double.valueOf(Double.NEGATIVE_INFINITY);
119 }
120
121 /**
122 * {@inheritDoc}
123 * <p>
124 * Returns a new {@link MinKeyElement}.
125 * </p>
126 */
127 @Override
128 public MinKeyElement withName(final String name) {
129 if (getName().equals(name)) {
130 return this;
131 }
132 return new MinKeyElement(name);
133 }
134 }