|
| 1 | +/* |
| 2 | + * Copyright 2015 MongoDB, Inc. |
| 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 | + |
| 17 | +package org.mongodb.scala.bson.collection.immutable |
| 18 | + |
| 19 | +import scala.jdk.CollectionConverters._ |
| 20 | +import scala.collection.mutable.ListBuffer |
| 21 | +import scala.collection.{ Iterable, IterableOps, SpecificIterableFactory, StrictOptimizedIterableOps, mutable } |
| 22 | +import org.mongodb.scala.bson._ |
| 23 | +import org.mongodb.scala.bson.collection.BaseDocument |
| 24 | + |
| 25 | +/** |
| 26 | + * The immutable [[Document]] companion object for easy creation. |
| 27 | + */ |
| 28 | +object Document extends SpecificIterableFactory[(String, BsonValue), Document] { |
| 29 | + |
| 30 | + import BsonMagnets._ |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a new empty Document |
| 34 | + * @return a new Document |
| 35 | + */ |
| 36 | + def empty: Document = apply() |
| 37 | + |
| 38 | + /** |
| 39 | + * Create a new Document |
| 40 | + * @return a new Document |
| 41 | + */ |
| 42 | + def apply(): Document = new Document(new BsonDocument()) |
| 43 | + |
| 44 | + /** |
| 45 | + * Parses a string in MongoDB Extended JSON format to a `Document` |
| 46 | + * |
| 47 | + * @param json the JSON string |
| 48 | + * @throws org.bson.json.JsonParseException if passed an invalid json string |
| 49 | + * @return a corresponding `Document` object |
| 50 | + * @see org.bson.json.JsonReader |
| 51 | + * @see [[http://docs.mongodb.com/manual/reference/mongodb-extended-json/ MongoDB Extended JSON]] |
| 52 | + */ |
| 53 | + def apply(json: String): Document = new Document(BsonDocument(json)) |
| 54 | + |
| 55 | + /** |
| 56 | + * Create a new document from the elems |
| 57 | + * @param elems the key/value pairs that make up the Document. This can be any valid `(String, BsonValue)` pair that can be |
| 58 | + * transformed into a [[BsonElement]] via [[BsonMagnets.CanBeBsonElement]] implicits and any [[BsonTransformer]]s that |
| 59 | + * are in scope. |
| 60 | + * @return a new Document consisting key/value pairs given by `elems`. |
| 61 | + */ |
| 62 | + def apply(elems: CanBeBsonElement*): Document = { |
| 63 | + val underlying = new BsonDocument() |
| 64 | + elems.foreach(elem => underlying.put(elem.key, elem.value)) |
| 65 | + new Document(underlying) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a new document from the elems |
| 70 | + * @param elems a sequence of key/values that make up the Document. This can be any valid sequence of `(String, BsonValue)` pairs that |
| 71 | + * can be transformed into a sequence of [[BsonElement]]s via [[BsonMagnets.CanBeBsonElements]] implicits and any |
| 72 | + * [[BsonTransformer]]s |
| 73 | + * that are in scope. |
| 74 | + * @return a new Document consisting key/value pairs given by `elems`. |
| 75 | + */ |
| 76 | + def apply(elems: CanBeBsonElements): Document = { |
| 77 | + val underlying = new BsonDocument() |
| 78 | + elems.values.foreach(el => underlying.put(el.key, el.value)) |
| 79 | + new Document(underlying) |
| 80 | + } |
| 81 | + |
| 82 | + def builder: mutable.Builder[(String, BsonValue), Document] = ListBuffer[(String, BsonValue)]() mapResult fromSeq |
| 83 | + |
| 84 | + def fromSeq(ts: Seq[(String, BsonValue)]): Document = { |
| 85 | + val underlying = new BsonDocument() |
| 86 | + ts.foreach(kv => underlying.put(kv._1, kv._2)) |
| 87 | + apply(underlying) |
| 88 | + } |
| 89 | + |
| 90 | + override def newBuilder: mutable.Builder[(String, BsonValue), Document] = builder |
| 91 | + override def fromSpecific(it: IterableOnce[(String, BsonValue)]): Document = fromSeq(it.iterator.toSeq) |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * An immutable Document implementation. |
| 96 | + * |
| 97 | + * A strictly typed `Map[String, BsonValue]` like structure that traverses the elements in insertion order. Unlike native scala maps there |
| 98 | + * is no variance in the value type and it always has to be a `BsonValue`. |
| 99 | + * |
| 100 | + * @param underlying the underlying BsonDocument which stores the data. |
| 101 | + * |
| 102 | + */ |
| 103 | +case class Document(protected[scala] val underlying: BsonDocument) |
| 104 | + extends BaseDocument[Document] |
| 105 | + with IterableOps[(String, BsonValue), Iterable, Document] |
| 106 | + with StrictOptimizedIterableOps[(String, BsonValue), Iterable, Document] { |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a new immutable document |
| 110 | + * @param underlying the underlying BsonDocument |
| 111 | + * @return a new document |
| 112 | + */ |
| 113 | + protected[scala] def apply(underlying: BsonDocument) = new Document(underlying) |
| 114 | + |
| 115 | + /** |
| 116 | + * Applies a function `f` to all elements of this document. |
| 117 | + * |
| 118 | + * @param f the function that is applied for its side-effect to every element. |
| 119 | + * The result of function `f` is discarded. |
| 120 | + * |
| 121 | + * @tparam U the type parameter describing the result of function `f`. |
| 122 | + * This result will always be ignored. Typically `U` is `Unit`, |
| 123 | + * but this is not necessary. |
| 124 | + * |
| 125 | + */ |
| 126 | + override def foreach[U](f: ((String, BsonValue)) => U): Unit = underlying.asScala foreach f |
| 127 | + |
| 128 | + // Mandatory overrides of `fromSpecific`, `newSpecificBuilder`, |
| 129 | + // and `empty`, from `IterableOps` |
| 130 | + override protected def fromSpecific(coll: IterableOnce[(String, BsonValue)]): Document = Document.fromSpecific(coll) |
| 131 | + override protected def newSpecificBuilder: mutable.Builder[(String, BsonValue), Document] = Document.newBuilder |
| 132 | + override def empty: Document = Document.empty |
| 133 | + |
| 134 | + // Overloading of `appended`, `prepended`, `appendedAll`, `prependedAll`, |
| 135 | + // `map`, `flatMap` and `concat` to return an `RNA` when possible |
| 136 | + def concat(suffix: IterableOnce[(String, BsonValue)]): Document = strictOptimizedConcat(suffix, newSpecificBuilder) |
| 137 | + // scalastyle:off method.name |
| 138 | + @inline final def ++(suffix: IterableOnce[(String, BsonValue)]): Document = concat(suffix) |
| 139 | + // scalastyle:on method.name |
| 140 | + def map[B](f: ((String, BsonValue)) => (String, BsonValue)): Document = strictOptimizedMap(newSpecificBuilder, f) |
| 141 | + |
| 142 | +} |
0 commit comments