org.mongojack.WriteResult Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.mongojack;
import com.mongodb.MongoException;
import org.bson.BsonValue;
import org.bson.types.ObjectId;
import static org.graylog2.shared.utilities.StringUtils.f;
/**
* Compatibility layer to support existing code interacting with the Mongojack 2.x API.
*
* @deprecated use {@link org.graylog2.database.MongoCollections} as an entrypoint for interacting with MongoDB.
*/
@Deprecated
public interface WriteResult {
default T getSavedObject() {
throw new MongoException("No objects to return");
}
int getN();
boolean wasAcknowledged();
default K getSavedId() {
throw new MongoException("No Id to return");
}
default Object getUpsertedId() {
return null;
}
boolean isUpdateOfExisting();
@SuppressWarnings("unchecked")
static L toIdType(BsonValue id, Class idType) {
if (id == null) {
return null;
}
if (String.class.isAssignableFrom(idType)) {
if (id.isObjectId()) {
return (L) id.asObjectId().getValue().toHexString();
}
if (id.isString()) {
return (L) id.asString().getValue();
}
}
if (ObjectId.class.isAssignableFrom(idType)) {
return (L) id.asObjectId().getValue();
}
throw new IllegalArgumentException(f("Only String and ObjectID types supported for ID. Got %s.",
id.getBsonType()));
}
static Object extractValue(BsonValue bsonValue) {
if (bsonValue == null) {
return null;
}
return switch (bsonValue.getBsonType()) {
case OBJECT_ID -> bsonValue.asObjectId().getValue();
case STRING -> bsonValue.asString().getValue();
default -> throw new IllegalArgumentException(f("Only String and ObjectID types supported for ID. Got %s.",
bsonValue.getBsonType()));
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy