<commit_before>package bisq.core.api;

import bisq.core.offer.Offer;
import bisq.core.offer.OpenOffer;

import bisq.proto.grpc.EditOfferRequest;

import java.math.BigDecimal;

import java.util.function.BiPredicate;
import java.util.function.Predicate;

import lombok.extern.slf4j.Slf4j;

import static bisq.proto.grpc.EditOfferRequest.EditType.*;
import static java.lang.String.format;
import static java.math.BigDecimal.ZERO;

@Slf4j
class EditOfferValidator {

    public final BiPredicate<Offer, EditOfferRequest.EditType> isEditingUseMktPriceMarginFlag = (offer, editType) -> {
        if (editType.equals(ACTIVATION_STATE_ONLY)) {
            // If only changing activation state, we are not editing offer.isUseMarketBasedPrice flag.
            return offer.isUseMarketBasedPrice();
        } else {
            return editType.equals(MKT_PRICE_MARGIN_ONLY)
                    || editType.equals(MKT_PRICE_MARGIN_AND_ACTIVATION_STATE)
                    || editType.equals(TRIGGER_PRICE_ONLY)
                    || editType.equals(TRIGGER_PRICE_AND_ACTIVATION_STATE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);
        }
    };

    public final Predicate<EditOfferRequest.EditType> isEditingMktPriceMargin = (editType) ->
            editType.equals(MKT_PRICE_MARGIN_ONLY)
                    || editType.equals(MKT_PRICE_MARGIN_AND_ACTIVATION_STATE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);

    public final Predicate<EditOfferRequest.EditType> isEditingTriggerPrice = (editType) ->
            editType.equals(TRIGGER_PRICE_ONLY)
                    || editType.equals(TRIGGER_PRICE_AND_ACTIVATION_STATE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);

    public final Predicate<EditOfferRequest.EditType> isEditingFixedPrice = (editType) ->
            editType.equals(FIXED_PRICE_ONLY) || editType.equals(FIXED_PRICE_AND_ACTIVATION_STATE);


    private final OpenOffer currentlyOpenOffer;
    private final String newPrice;
    private final boolean newIsUseMarketBasedPrice;
    private final double newMarketPriceMargin;
    private final String newTriggerPrice;
    private final int newEnable;
    private final EditOfferRequest.EditType editType;

    private final boolean isZeroEditedFixedPriceString;
    private final boolean isZeroEditedTriggerPrice;

    EditOfferValidator(OpenOffer currentlyOpenOffer,
                       String newPrice,
                       boolean newIsUseMarketBasedPrice,
                       double newMarketPriceMargin,
                       String newTriggerPrice,
                       int newEnable,
                       EditOfferRequest.EditType editType) {
        this.currentlyOpenOffer = currentlyOpenOffer;
        this.newPrice = newPrice;
        // The client cannot determine what offer.isUseMarketBasedPrice should be
        // when editType = ACTIVATION_STATE_ONLY.  Override newIsUseMarketBasedPrice
        // param for the ACTIVATION_STATE_ONLY case.
        // A cleaner solution might be possible if the client fetched the offer
        // before sending an edit request, but that's an extra round trip to the server.
        this.newIsUseMarketBasedPrice = editType.equals(ACTIVATION_STATE_ONLY)
                ? currentlyOpenOffer.getOffer().isUseMarketBasedPrice()
                : newIsUseMarketBasedPrice;
        this.newMarketPriceMargin = newMarketPriceMargin;
        this.newTriggerPrice = newTriggerPrice;
        this.newEnable = newEnable;
        this.editType = editType;

        this.isZeroEditedFixedPriceString = new BigDecimal(newPrice).doubleValue() == 0;
        this.isZeroEditedTriggerPrice = new BigDecimal(newTriggerPrice).equals(ZERO);
    }

    EditOfferValidator validate() {
        log.info("Verifying 'editoffer' params for editType {}", editType);
        checkNotBsqSwapOffer();
        switch (editType) {
            case ACTIVATION_STATE_ONLY: {
                validateEditedActivationState();
                break;<commit_msg>Allow editoffer clients to pass empty string for price parameter<commit_after>package bisq.core.api;

import bisq.core.offer.Offer;
import bisq.core.offer.OpenOffer;

import bisq.proto.grpc.EditOfferRequest;

import java.math.BigDecimal;

import java.util.function.BiPredicate;
import java.util.function.Predicate;

import lombok.extern.slf4j.Slf4j;

import static bisq.proto.grpc.EditOfferRequest.EditType.*;
import static java.lang.String.format;
import static java.math.BigDecimal.ZERO;

@Slf4j
class EditOfferValidator {

    public final BiPredicate<Offer, EditOfferRequest.EditType> isEditingUseMktPriceMarginFlag = (offer, editType) -> {
        if (editType.equals(ACTIVATION_STATE_ONLY)) {
            // If only changing activation state, we are not editing offer.isUseMarketBasedPrice flag.
            return offer.isUseMarketBasedPrice();
        } else {
            return editType.equals(MKT_PRICE_MARGIN_ONLY)
                    || editType.equals(MKT_PRICE_MARGIN_AND_ACTIVATION_STATE)
                    || editType.equals(TRIGGER_PRICE_ONLY)
                    || editType.equals(TRIGGER_PRICE_AND_ACTIVATION_STATE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);
        }
    };

    public final Predicate<EditOfferRequest.EditType> isEditingMktPriceMargin = (editType) ->
            editType.equals(MKT_PRICE_MARGIN_ONLY)
                    || editType.equals(MKT_PRICE_MARGIN_AND_ACTIVATION_STATE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);

    public final Predicate<EditOfferRequest.EditType> isEditingTriggerPrice = (editType) ->
            editType.equals(TRIGGER_PRICE_ONLY)
                    || editType.equals(TRIGGER_PRICE_AND_ACTIVATION_STATE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE)
                    || editType.equals(MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);

    public final Predicate<EditOfferRequest.EditType> isEditingFixedPrice = (editType) ->
            editType.equals(FIXED_PRICE_ONLY) || editType.equals(FIXED_PRICE_AND_ACTIVATION_STATE);


    private final OpenOffer currentlyOpenOffer;
    private final String newPrice;
    private final boolean newIsUseMarketBasedPrice;
    private final double newMarketPriceMargin;
    private final String newTriggerPrice;
    private final int newEnable;
    private final EditOfferRequest.EditType editType;

    private final boolean isZeroEditedFixedPriceString;
    private final boolean isZeroEditedTriggerPrice;

    EditOfferValidator(OpenOffer currentlyOpenOffer,
                       String newPrice,
                       boolean newIsUseMarketBasedPrice,
                       double newMarketPriceMargin,
                       String newTriggerPrice,
                       int newEnable,
                       EditOfferRequest.EditType editType) {
        this.currentlyOpenOffer = currentlyOpenOffer;
        // The client may have passed an empty string for the price parameter
        // if only enabling or disabling the offer.  If so, validate with new
        // price = old price.
        this.newPrice = editType.equals(ACTIVATION_STATE_ONLY)
                ? currentlyOpenOffer.getOffer().getPrice().toString()
                : newPrice;
        // The client cannot determine what offer.isUseMarketBasedPrice should be
        // when editType = ACTIVATION_STATE_ONLY.  Override newIsUseMarketBasedPrice
        // param for the ACTIVATION_STATE_ONLY case.
        // A cleaner solution might be possible if the client fetched the offer
        // before sending an edit request, but that's an extra round trip to the server.
        this.newIsUseMarketBasedPrice = editType.equals(ACTIVATION_STATE_ONLY)
                ? currentlyOpenOffer.getOffer().isUseMarketBasedPrice()
                : newIsUseMarketBasedPrice;
        this.newMarketPriceMargin = newMarketPriceMargin;
        this.newTriggerPrice = newTriggerPrice;
        this.newEnable = newEnable;
        this.editType = editType;

        this.isZeroEditedFixedPriceString = new BigDecimal(newPrice).doubleValue() == 0;
        this.isZeroEditedTriggerPrice = new BigDecimal(newTriggerPrice).equals(ZERO);
    }

    EditOfferValidator validate() {
        log.info("Verifying 'editoffer' params for editType {}", editType);
        checkNotBsqSwapOffer();
        switch (editType) {
            case ACTIVATION_STATE_ONLY: {
                validateEditedActivationState();
                break;
{'fraction_non_alphanumeric': 0.06790533067905331, 'fraction_numerical': 0.0008847600088476001, 'mean_word_length': 2.867835757057314, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4260869', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2557, 'n_words': 469}