Coverage Report - com.allanbank.mongodb.builder.TextResult
 
Classes in this File Line Coverage Branch Coverage Complexity
TextResult
100%
22/22
100%
10/10
1.833
 
 1  
 /*
 2  
  * #%L
 3  
  * TextResult.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  
 
 21  
 package com.allanbank.mongodb.builder;
 22  
 
 23  
 import com.allanbank.mongodb.bson.Document;
 24  
 import com.allanbank.mongodb.bson.DocumentAssignable;
 25  
 import com.allanbank.mongodb.bson.NumericElement;
 26  
 import com.allanbank.mongodb.bson.element.DocumentElement;
 27  
 
 28  
 /**
 29  
  * TextResult provides a wrapper for a single result of a {@link Text text}
 30  
  * command.
 31  
  * <p>
 32  
  * The result of a {@code text} command is a document that looks like the
 33  
  * following:<blockquote>
 34  
  * 
 35  
  * <pre>
 36  
  * <code>
 37  
  * > db.collection.runCommand( { "text": "collection" , search: "coffee magic" } )
 38  
  * {
 39  
  *     "queryDebugString" : "coffe|magic||||||",
 40  
  *     "language" : "english",
 41  
  *     "results" : [
 42  
  *         {
 43  
  *             "score" : 2.25,
 44  
  *             "obj" : {
 45  
  *                 "_id" : ObjectId("51376ab8602c316554cfe248"),
 46  
  *                 "content" : "Coffee is full of magical powers."
 47  
  *             }
 48  
  *         },
 49  
  *         {
 50  
  *             "score" : 0.625,
 51  
  *             "obj" : {
 52  
  *                 "_id" : ObjectId("51376a80602c316554cfe246"),
 53  
  *                 "content" : "Now is the time to drink all of the coffee."
 54  
  *             }
 55  
  *         }
 56  
  *     ],
 57  
  *     "stats" : {
 58  
  *         "nscanned" : 3,
 59  
  *         "nscannedObjects" : 0,
 60  
  *         "n" : 2,
 61  
  *         "nfound" : 2,
 62  
  *         "timeMicros" : 97
 63  
  *     },
 64  
  *     "ok" : 1
 65  
  * }
 66  
  * </code>
 67  
  * </pre>
 68  
  * 
 69  
  * </blockquote>
 70  
  * </p>
 71  
  * <p>
 72  
  * This class wraps a single entry from the {@code results} array.
 73  
  * </p>
 74  
  * 
 75  
  * @api.no <b>This class is NOT part of the Public API.</b> This class may be
 76  
  *         mutated in incompatible ways between any two releases of the driver.
 77  
  *         This class <b>WILL</b>, eventually, be part of the driver's API.
 78  
  *         Until MongoDB Inc. finalizes the text query interface we are keeping
 79  
  *         this class out of the Public API so we can track any changes more
 80  
  *         closely.
 81  
  * @see <a
 82  
  *      href="http://docs.mongodb.org/manual/release-notes/2.4/#text-queries">
 83  
  *      MongoDB Text Queries</a>
 84  
  * @since MongoDB 2.4
 85  
  * @deprecated Support for the {@code text} command was deprecated in the 2.6
 86  
  *             version of MongoDB. Use the {@link ConditionBuilder#text(String)
 87  
  *             $text} query operator instead. This class will not be removed
 88  
  *             until two releases after the MongoDB 2.6 release (e.g. 2.10 if
 89  
  *             the releases are 2.8 and 2.10).
 90  
  * @copyright 2013-2014, Allanbank Consulting, Inc., All Rights Reserved
 91  
  */
 92  
 @Deprecated
 93  
 public class TextResult {
 94  
     /** The document. */
 95  
     private final Document myDocument;
 96  
 
 97  
     /** The score for the document. */
 98  
     private final Document myRawDocument;
 99  
 
 100  
     /** The score for the document. */
 101  
     private final double myScore;
 102  
 
 103  
     /**
 104  
      * Creates a new Text.
 105  
      * 
 106  
      * @param document
 107  
      *            The document containing the 'score' and 'obj' fields.
 108  
      * @throws AssertionError
 109  
      *             On the search term not being set.
 110  
      */
 111  12
     public TextResult(final DocumentAssignable document) {
 112  12
         myRawDocument = document.asDocument();
 113  
 
 114  12
         final NumericElement score = myRawDocument.get(NumericElement.class,
 115  
                 "score");
 116  12
         if (score != null) {
 117  9
             myScore = score.getDoubleValue();
 118  
         }
 119  
         else {
 120  3
             myScore = -1;
 121  
         }
 122  
 
 123  12
         final DocumentElement obj = myRawDocument.get(DocumentElement.class,
 124  
                 "obj");
 125  12
         if (obj != null) {
 126  5
             myDocument = obj.getDocument();
 127  
         }
 128  
         else {
 129  7
             myDocument = null;
 130  
         }
 131  12
     }
 132  
 
 133  
     /**
 134  
      * {@inheritDoc}
 135  
      * <p>
 136  
      * Overridden to compare equal to an equivalent text result.
 137  
      * </p>
 138  
      */
 139  
     @Override
 140  
     public boolean equals(final Object object) {
 141  12
         boolean result = false;
 142  12
         if (this == object) {
 143  2
             result = true;
 144  
         }
 145  10
         else if ((object != null) && (getClass() == object.getClass())) {
 146  6
             final TextResult other = (TextResult) object;
 147  
 
 148  6
             result = myRawDocument.equals(other.myRawDocument);
 149  
         }
 150  12
         return result;
 151  
     }
 152  
 
 153  
     /**
 154  
      * Returns the document.
 155  
      * 
 156  
      * @return The document.
 157  
      */
 158  
     public Document getDocument() {
 159  2
         return myDocument;
 160  
     }
 161  
 
 162  
     /**
 163  
      * Returns the un-processed result document. It is expected to be a document
 164  
      * containing two fields: 'score' and 'obj'.
 165  
      * 
 166  
      * @return The un-processed result document.
 167  
      */
 168  
     public Document getRawDocument() {
 169  2
         return myRawDocument;
 170  
     }
 171  
 
 172  
     /**
 173  
      * Returns the score for the document.
 174  
      * 
 175  
      * @return The score for the document.
 176  
      */
 177  
     public double getScore() {
 178  2
         return myScore;
 179  
     }
 180  
 
 181  
     /**
 182  
      * {@inheritDoc}
 183  
      * <p>
 184  
      * Overridden to return the hashCode of the raw document.
 185  
      * </p>
 186  
      */
 187  
     @Override
 188  
     public int hashCode() {
 189  6
         return myRawDocument.hashCode();
 190  
     }
 191  
 }