<commit_before>package org.jsmpp.session;

import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.jsmpp.PDUStringException;
import org.jsmpp.bean.Bind;
import org.jsmpp.bean.BindType;
import org.jsmpp.bean.NumberingPlanIndicator;
import org.jsmpp.bean.TypeOfNumber;
import org.jsmpp.extra.ProcessRequestException;
import org.jsmpp.util.StringParameter;
import org.jsmpp.util.StringValidator;

/**
 * @author uudashr
 *
 */
public class BindRequest {
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    
    private final BindParameter bindParam;
    private final int originalSequenceNumber;
    private boolean done;
    
    private final ServerResponseHandler responseHandler;
    public BindRequest(int sequenceNumber, BindType bindType, String systemId, String password, 
            String systemType, TypeOfNumber addrTon, NumberingPlanIndicator addrNpi, 
            String addressRange, ServerResponseHandler responseHandler) {
        this.originalSequenceNumber = sequenceNumber;
        bindParam = new BindParameter(bindType, systemId, password, systemType, addrTon, addrNpi, addressRange);
        this.responseHandler = responseHandler;
    }
    
    public BindRequest(Bind bind, ServerResponseHandler responseHandler) {
        this(bind.getSequenceNumber(), BindType.valueOf(bind.getCommandId()), bind.getSystemId(), 
                bind.getPassword(), bind.getSystemType(), 
                TypeOfNumber.valueOf(bind.getAddrTon()), 
                NumberingPlanIndicator.valueOf(bind.getAddrNpi()), 
                bind.getAddressRange(), responseHandler);
    }
    
    public BindParameter getBindParameter() {
        return bindParam;
    }
    
    /**
     * Accept the bind request.
     * 
     * @param systemId is the system identifier that will be send to ESME.
     * @throws PDUStringException if the system id is not valid.
     * @throws IllegalStateException if the acceptance or rejection has been made.
     * @throws IOException is the connection already closed.
     * @see #reject(ProcessRequestException)
     */
    public void accept(String systemId) throws PDUStringException, IllegalStateException, IOException {
        StringValidator.validateString(systemId, StringParameter.SYSTEM_ID);
        lock.lock();
        try {
            if (!done) {
                done = true;
                try {
                    responseHandler.sendBindResp(systemId, bindParam.getBindType(), originalSequenceNumber);
                } finally {
                    condition.signal();
                }
            } else {
                throw new IllegalStateException("Response already initiated");
            }
        } finally {
            lock.unlock();
        }
        done = true;
    }
    
    /**
     * Reject the bind request.
     * 
     * @param errorCode is the reason of rejection.
     * @throws IllegalStateException if the acceptance or rejection has been made.
     * @throws IOException if the connection already closed.
     * @see {@link #accept()}
     */
    public void reject(int errorCode) throws IllegalStateException, IOException {
        lock.lock();
        try {
            if (done) {
                throw new IllegalStateException("Response already initiated");
            } else {
                done = true;
                try {
                    responseHandler.sendNegativeResponse(bindParam.getBindType().commandId(), errorCode, originalSequenceNumber);
                } finally {
                    condition.signal();
                }
            }
        } finally {
            lock.unlock();
        }
    }
}<commit_msg>bind parameter might be removed in future<commit_after>package org.jsmpp.session;

import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.jsmpp.PDUStringException;
import org.jsmpp.bean.Bind;
import org.jsmpp.bean.BindType;
import org.jsmpp.bean.NumberingPlanIndicator;
import org.jsmpp.bean.TypeOfNumber;
import org.jsmpp.extra.ProcessRequestException;
import org.jsmpp.util.StringParameter;
import org.jsmpp.util.StringValidator;

/**
 * @author uudashr
 *
 */
public class BindRequest {
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    private final BindType bindType;
    private final String systemId;
    private final String password;
    private final String systemType;
    private final TypeOfNumber addrTon;
    private final NumberingPlanIndicator addrNpi;
    private final String addressRange;
    
    private final int originalSequenceNumber;
    private boolean done;
    
    private final ServerResponseHandler responseHandler;
    public BindRequest(int sequenceNumber, BindType bindType, String systemId, String password, 
            String systemType, TypeOfNumber addrTon, NumberingPlanIndicator addrNpi, 
            String addressRange, ServerResponseHandler responseHandler) {
        this.originalSequenceNumber = sequenceNumber;
        this.responseHandler = responseHandler;
        
        this.bindType = bindType;
        this.systemId = systemId;
        this.password = password;
        this.systemType = systemType;
        this.addrTon = addrTon;
        this.addrNpi = addrNpi;
        this.addressRange = addressRange;
    }
    
    public BindRequest(Bind bind, ServerResponseHandler responseHandler) {
        this(bind.getSequenceNumber(), BindType.valueOf(bind.getCommandId()), bind.getSystemId(), 
                bind.getPassword(), bind.getSystemType(), 
                TypeOfNumber.valueOf(bind.getAddrTon()), 
                NumberingPlanIndicator.valueOf(bind.getAddrNpi()), 
                bind.getAddressRange(), responseHandler);
    }
    
    @Deprecated
    public BindParameter getBindParameter() {
        return new BindParameter(bindType, systemId, password, systemType, addrTon, addrNpi, addressRange);
    }
    
    public BindType getBindType() {
        return bindType;
    }
    
    public String getSystemId() {
        return systemId;
    }
    
    public String getPassword() {
        return password;
    }
    
    public String getSystemType() {
        return systemType;
    }
    
    public TypeOfNumber getAddrTon() {
        return addrTon;
    }
    
    public NumberingPlanIndicator getAddrNpi() {
        return addrNpi;
    }
    
    public String getAddressRange() {
        return addressRange;
    }
    
    /**
     * Accept the bind request.
     * 
     * @param systemId is the system identifier that will be send to ESME.
     * @throws PDUStringException if the system id is not valid.
     * @throws IllegalStateException if the acceptance or rejection has been made.
     * @throws IOException is the connection already closed.
     * @see #reject(ProcessRequestException)
     */
    public void accept(String systemId) throws PDUStringException, IllegalStateException, IOException {
        StringValidator.validateString(systemId, StringParameter.SYSTEM_ID);
        lock.lock();
        try {
            if (!done) {
                done = true;
                try {
                    responseHandler.sendBindResp(systemId, bindType, originalSequenceNumber);
                } finally {
                    condition.signal();
                }
            } else {
                throw new IllegalStateException("Response already initiated");
            }
        } finally {
            lock.unlock();
        }
        done = true;
    }
    
    /**
     * Reject the bind request.
     * 
     * @param errorCode is the reason of rejection.
     * @throws IllegalStateException if the acceptance or rejection has been made.
     * @throws IOException if the connection already closed.
     * @see {@link #accept()}
     */
    public void reject(int errorCode) throws IllegalStateException, IOException {
        lock.lock();
        try {
            if (done) {
                throw new IllegalStateException("Response already initiated");
            } else {
                done = true;
                try {
                    responseHandler.sendNegativeResponse(bindType.commandId(), errorCode, originalSequenceNumber);
                } finally {
                    condition.signal();
                }
            }
        } finally {
            lock.unlock();
        }
    }
}
{'fraction_non_alphanumeric': 0.07997678467788741, 'fraction_numerical': 0.0, 'mean_word_length': 2.3382409918636187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7242412', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2145, 'n_words': 562}