Search in sources :

Example 1 with BsonNumber

use of org.bson.BsonNumber in project mongo-java-driver by mongodb.

the class IndexHelper method generateIndexName.

/**
     * Convenience method to generate an index name from the set of fields it is over.
     *
     * @return a string representation of this index's fields
     */
static String generateIndexName(final BsonDocument index) {
    StringBuilder indexName = new StringBuilder();
    for (final String keyNames : index.keySet()) {
        if (indexName.length() != 0) {
            indexName.append('_');
        }
        indexName.append(keyNames).append('_');
        BsonValue ascOrDescValue = index.get(keyNames);
        if (ascOrDescValue instanceof BsonNumber) {
            indexName.append(((BsonNumber) ascOrDescValue).intValue());
        } else if (ascOrDescValue instanceof BsonString) {
            indexName.append(((BsonString) ascOrDescValue).getValue().replace(' ', '_'));
        }
    }
    return indexName.toString();
}
Also used : BsonNumber(org.bson.BsonNumber) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 2 with BsonNumber

use of org.bson.BsonNumber in project mongo-java-driver by mongodb.

the class IndexHelper method generateIndexName.

/**
 * Convenience method to generate an index name from the set of fields it is over.
 *
 * @return a string representation of this index's fields
 */
public static String generateIndexName(final BsonDocument index) {
    StringBuilder indexName = new StringBuilder();
    for (final String keyNames : index.keySet()) {
        if (indexName.length() != 0) {
            indexName.append('_');
        }
        indexName.append(keyNames).append('_');
        BsonValue ascOrDescValue = index.get(keyNames);
        if (ascOrDescValue instanceof BsonNumber) {
            indexName.append(((BsonNumber) ascOrDescValue).intValue());
        } else if (ascOrDescValue instanceof BsonString) {
            indexName.append(((BsonString) ascOrDescValue).getValue().replace(' ', '_'));
        }
    }
    return indexName.toString();
}
Also used : BsonNumber(org.bson.BsonNumber) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 3 with BsonNumber

use of org.bson.BsonNumber in project mongo-java-driver by mongodb.

the class WriteConcernDocumentTest method getWriteConcern.

private WriteConcern getWriteConcern(final BsonDocument writeConcernDocument) {
    BsonValue wValue = writeConcernDocument.get("w");
    WriteConcern retVal;
    if (wValue == null) {
        retVal = WriteConcern.ACKNOWLEDGED;
    } else if (wValue instanceof BsonNumber) {
        retVal = new WriteConcern(wValue.asNumber().intValue());
    } else if (wValue instanceof BsonString) {
        retVal = new WriteConcern(wValue.asString().getValue());
    } else {
        throw new IllegalArgumentException("Unexpected w value: " + wValue);
    }
    if (writeConcernDocument.containsKey("wtimeoutMS")) {
        retVal = retVal.withWTimeout(writeConcernDocument.getNumber("wtimeoutMS", new BsonInt32(0)).intValue(), TimeUnit.MILLISECONDS);
    }
    if (writeConcernDocument.containsKey("journal")) {
        retVal = retVal.withJournal(writeConcernDocument.getBoolean("journal", BsonBoolean.FALSE).getValue());
    }
    return retVal;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonNumber(org.bson.BsonNumber) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 4 with BsonNumber

use of org.bson.BsonNumber in project mongo-java-driver by mongodb.

the class WriteConcernConnectionStringTest method getExpectedWriteConcern.

private WriteConcern getExpectedWriteConcern() {
    BsonDocument writeConcernDocument = definition.getDocument("writeConcern");
    BsonValue wValue = writeConcernDocument.get("w");
    WriteConcern retVal;
    if (wValue == null) {
        retVal = WriteConcern.ACKNOWLEDGED;
    } else if (wValue instanceof BsonNumber) {
        retVal = new WriteConcern(wValue.asNumber().intValue());
    } else if (wValue instanceof BsonString) {
        retVal = new WriteConcern(wValue.asString().getValue());
    } else {
        throw new IllegalArgumentException("Unexpected w value: " + wValue);
    }
    if (writeConcernDocument.containsKey("wtimeoutMS")) {
        retVal = retVal.withWTimeout(writeConcernDocument.getNumber("wtimeoutMS", new BsonInt32(0)).intValue(), TimeUnit.MILLISECONDS);
    }
    if (writeConcernDocument.containsKey("journal")) {
        retVal = retVal.withJournal(writeConcernDocument.getBoolean("journal", BsonBoolean.FALSE).getValue());
    }
    return retVal;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonNumber(org.bson.BsonNumber) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Aggregations

BsonNumber (org.bson.BsonNumber)4 BsonString (org.bson.BsonString)4 BsonValue (org.bson.BsonValue)4 BsonInt32 (org.bson.BsonInt32)2 BsonDocument (org.bson.BsonDocument)1