<commit_before>package org.jgroups.blocks;


import org.jgroups.Address;
import org.jgroups.Message;
import org.jgroups.View;
import org.jgroups.annotations.GuardedBy;
import org.jgroups.util.Rsp;

import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


/**
 * Sends a request to a single target destination
 *
 * @author <NAME>
 */
public class UnicastRequest<T> extends Request {
    protected final Rsp<T>     result;
    protected final Address    target;



    public UnicastRequest(Message msg, RequestCorrelator corr, Address target, RequestOptions options) {
        super(msg, corr, options);
        this.target=target;
        result=new Rsp<T>(target);
    }

    public UnicastRequest(Message msg, Address target, RequestOptions options) {
        super(msg, null, options);
        this.target=target;
        result=new Rsp<T>(target);
    }


    protected void sendRequest() throws Exception {
        try {
            if(log.isTraceEnabled()) log.trace(new StringBuilder("sending request (id=").append(req_id).append(')'));
            corr.sendUnicastRequest(req_id, target, request_msg, options.getMode() == ResponseMode.GET_NONE? null : this);
        }
        catch(Exception ex) {
            if(corr != null)
                corr.done(req_id);
            throw ex;
        }
    }
    

    /* ---------------------- Interface RspCollector -------------------------- */
    /**
     * <b>Callback</b> (called by RequestCorrelator or Transport).
     * Adds a response to the response table. When all responses have been received,
     * <code>execute()</code> returns.
     */
    public void receiveResponse(Object response_value, Address sender, boolean is_exception) {
        RspFilter rsp_filter=options.getRspFilter();

        lock.lock();
        try {
            if(done)
                return;
            if(!result.wasReceived()) {
                boolean responseReceived=(rsp_filter == null) || rsp_filter.isAcceptable(response_value, sender);
                if(is_exception && response_value instanceof Throwable)
                    result.setException((Throwable)response_value);
                else
                    result.setValue((T)response_value);
                result.setReceived(responseReceived);
                if(log.isTraceEnabled()) {
                    StringBuilder sb=new StringBuilder("received response for request ");
                    sb.append(req_id).append(", sender=").append(sender);
                    if(is_exception && response_value instanceof Throwable)
                        sb.append(", exception=");
                    else
                        sb.append(", val=");
                    sb.append(response_value);
                    log.trace(sb.toString());
                }
            }
            done=rsp_filter == null? responsesComplete() : !rsp_filter.needMoreResponses();
            if(done && corr != null)
                corr.done(req_id);
        }
        finally {
            completed.signalAll(); // wakes up execute()
            lock.unlock();
        }
        checkCompletion(this);
    }


    /**
     * <b>Callback</b> (called by RequestCorrelator or Transport).
     * Report to <code>GroupRequest</code> that a member is reported as faulty (suspected).
     * This method would probably be called when getting a suspect message from a failure detector
     * (where available). It is used to exclude faulty members from the response list.
     */
    public void suspect(Address suspected_member) {
        if(suspected_member == null || !suspected_member.equals(target))
            return;

        lock.lock();
        try {
            if(done)
                return;
            if(result != null && !result.wasReceived())
                result.setSuspected(true);
            done=true;
            if(corr != null)
                corr.done(req_id);
            completed.signalAll();
        }
        finally {
            lock.unlock();
        }
        checkCompletion(this);
    }


    /**
     * If the target address is not a member of the new view, we'll mark the response as not received and unblock
     * the caller of execute()
     */
    public void viewChange(View new_view) {
        Collection<Address> mbrs=new_view != null? new_view.getMembers() : null;
        if(mbrs == null)
            return;

        lock.lock();
        try {
            if(!mbrs.contains(target)) {
                result.setReceived(false);
                done=true;
                if(corr != null)
                    corr.done(req_id);
                completed.signalAll();
            }
        }
        finally {
            lock.unlock();
        }
        
        checkCompletion(this);
    }


    /* -------------------- End of Interface RspCollector ----------------------------------- */

    public Rsp<T> getResult() {
        return result;
    }


    public T getValue() throws ExecutionException {
        if(!result.hasException())
            return result.getValue();

        Throwable exception=result.getException();
        if(exception instanceof Error) throw (Error)exception;
        else if(exception instanceof RuntimeException) throw (RuntimeException)exception;
        else if(exception instanceof Exception) throw new ExecutionException(exception);
        else throw new RuntimeException(exception);
    }


    public T get() throws InterruptedException, ExecutionException {
        lock.lock();
        try {
            waitForResults(0);
            return getValue();
        }
        finally {
            lock.unlock();
        }
    }

    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        boolean ok;
        lock.lock();
        try {
            ok=waitForResults(unit.toMillis(timeout));
        }
        finally {
            lock.unlock();<commit_msg>UnicastRequest.getValue(): wrapping in ExecutionException<commit_after>package org.jgroups.blocks;


import org.jgroups.Address;
import org.jgroups.Message;
import org.jgroups.View;
import org.jgroups.annotations.GuardedBy;
import org.jgroups.util.Rsp;

import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


/**
 * Sends a request to a single target destination
 *
 * @author <NAME>
 */
public class UnicastRequest<T> extends Request {
    protected final Rsp<T>     result;
    protected final Address    target;



    public UnicastRequest(Message msg, RequestCorrelator corr, Address target, RequestOptions options) {
        super(msg, corr, options);
        this.target=target;
        result=new Rsp<T>(target);
    }

    public UnicastRequest(Message msg, Address target, RequestOptions options) {
        super(msg, null, options);
        this.target=target;
        result=new Rsp<T>(target);
    }


    protected void sendRequest() throws Exception {
        try {
            if(log.isTraceEnabled()) log.trace(new StringBuilder("sending request (id=").append(req_id).append(')'));
            corr.sendUnicastRequest(req_id, target, request_msg, options.getMode() == ResponseMode.GET_NONE? null : this);
        }
        catch(Exception ex) {
            if(corr != null)
                corr.done(req_id);
            throw ex;
        }
    }
    

    /* ---------------------- Interface RspCollector -------------------------- */
    /**
     * <b>Callback</b> (called by RequestCorrelator or Transport).
     * Adds a response to the response table. When all responses have been received,
     * <code>execute()</code> returns.
     */
    public void receiveResponse(Object response_value, Address sender, boolean is_exception) {
        RspFilter rsp_filter=options.getRspFilter();

        lock.lock();
        try {
            if(done)
                return;
            if(!result.wasReceived()) {
                boolean responseReceived=(rsp_filter == null) || rsp_filter.isAcceptable(response_value, sender);
                if(is_exception && response_value instanceof Throwable)
                    result.setException((Throwable)response_value);
                else
                    result.setValue((T)response_value);
                result.setReceived(responseReceived);
                if(log.isTraceEnabled()) {
                    StringBuilder sb=new StringBuilder("received response for request ");
                    sb.append(req_id).append(", sender=").append(sender);
                    if(is_exception && response_value instanceof Throwable)
                        sb.append(", exception=");
                    else
                        sb.append(", val=");
                    sb.append(response_value);
                    log.trace(sb.toString());
                }
            }
            done=rsp_filter == null? responsesComplete() : !rsp_filter.needMoreResponses();
            if(done && corr != null)
                corr.done(req_id);
        }
        finally {
            completed.signalAll(); // wakes up execute()
            lock.unlock();
        }
        checkCompletion(this);
    }


    /**
     * <b>Callback</b> (called by RequestCorrelator or Transport).
     * Report to <code>GroupRequest</code> that a member is reported as faulty (suspected).
     * This method would probably be called when getting a suspect message from a failure detector
     * (where available). It is used to exclude faulty members from the response list.
     */
    public void suspect(Address suspected_member) {
        if(suspected_member == null || !suspected_member.equals(target))
            return;

        lock.lock();
        try {
            if(done)
                return;
            if(result != null && !result.wasReceived())
                result.setSuspected(true);
            done=true;
            if(corr != null)
                corr.done(req_id);
            completed.signalAll();
        }
        finally {
            lock.unlock();
        }
        checkCompletion(this);
    }


    /**
     * If the target address is not a member of the new view, we'll mark the response as not received and unblock
     * the caller of execute()
     */
    public void viewChange(View new_view) {
        Collection<Address> mbrs=new_view != null? new_view.getMembers() : null;
        if(mbrs == null)
            return;

        lock.lock();
        try {
            if(!mbrs.contains(target)) {
                result.setReceived(false);
                done=true;
                if(corr != null)
                    corr.done(req_id);
                completed.signalAll();
            }
        }
        finally {
            lock.unlock();
        }
        
        checkCompletion(this);
    }


    /* -------------------- End of Interface RspCollector ----------------------------------- */

    public Rsp<T> getResult() {
        return result;
    }


    public T getValue() throws ExecutionException {
        if(!result.hasException())
            return result.getValue();
        throw new ExecutionException(result.getException());
    }


    public T get() throws InterruptedException, ExecutionException {
        lock.lock();
        try {
            waitForResults(0);
            return getValue();
        }
        finally {
            lock.unlock();
        }
    }

    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        boolean ok;
        lock.lock();
        try {
            ok=waitForResults(unit.toMillis(timeout));
        }
        finally {
            lock.unlock();
{'fraction_non_alphanumeric': 0.11814095496565177, 'fraction_numerical': 0.00016962089729454668, 'mean_word_length': 1.8366610536444552, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '4902602', 'n_tokens_mistral': 3076, 'n_tokens_neox': 2961, 'n_words': 723}