All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.noenv.wiremongo.mapping.find.FindOne Maven / Gradle / Ivy

There is a newer version: 4.5.10
Show newest version
package com.noenv.wiremongo.mapping.find;

import com.noenv.wiremongo.command.Command;
import com.noenv.wiremongo.command.find.FindOneCommand;
import com.noenv.wiremongo.mapping.WithQuery;
import com.noenv.wiremongo.matching.Matcher;
import io.vertx.core.json.JsonObject;

import static com.noenv.wiremongo.matching.EqualsMatcher.equalTo;

public class FindOne extends WithQuery {

  private Matcher fields;

  public FindOne() {
    super("findOne");
  }

  public FindOne(JsonObject json) {
    super(json);
    fields = Matcher.create(json.getJsonObject("fields"));
  }

  @Override
  public FindOne returns(final JsonObject response) {
    return super.stub(c -> null == response ? null : response.copy());
  }

  @Override
  public boolean matches(Command cmd) {
    if (!super.matches(cmd)) {
      return false;
    }
    if (!(cmd instanceof FindOneCommand)) {
      return false;
    }
    FindOneCommand c = (FindOneCommand) cmd;
    return fields == null || fields.matches(c.getFields());
  }

  @Override
  protected JsonObject parseResponse(Object jsonValue) {
    return (JsonObject) jsonValue;
  }

  public FindOne withFields(JsonObject fields) {
    return withFields(equalTo(fields));
  }

  public FindOne withFields(Matcher fields) {
    this.fields = fields;
    return self();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy