<commit_before>// $Id: Promise.java,v 1.5 2004/03/30 06:47:28 belaban Exp $

package org.jgroups.util;


/**
 * Allows a thread to submit an asynchronous request and to wait for the result. The caller may choose to check
 * for the result at a later time, or immediately and it may block or not. Both the caller and responder have to
 * know the promise.
 */
public class Promise {
    Object result=null;
    boolean hasResult=false;


    /**
     * Gets result. If result was already submitted,
     * returns it immediately (resetting it), else blocks
     * until
     * results get available.
     * 
     * @param timeout Max time to wait for result. If it
     *                is 0, we wait indefinitely
     */
    public Object getResult(long timeout) {
        Object ret=null;
        // long   time_to_wait=timeout;

//        synchronized(this) {
//            while(hasResult == false) {
//                try {
//                    if(timeout <= 0) {
//                        wait();
//                        break;
//                    }
//                    else {
//                        wait(time_to_wait);
//
//                    }
//                }
//                catch(InterruptedException ex) {
//                    ;
//                }
//            }
//            if(hasResult) {
//                ret=result;
//                result=null;
//                hasResult=false;
//                return ret;
//            }
//        }


        synchronized(this) {
            if(hasResult) {
                ret=result;
                result=null;
                hasResult=false;
                return ret;
            }
            try {
                if(timeout <= 0) {
                    wait();
                }
                else {
                    wait(timeout);
                }
            }
            catch(Exception ex) {

            }
            if(hasResult) {
                ret=result;
                result=null;
                hasResult=false;
                return ret;
            }
            return null;
        }
    }


    /**
     * Checks whether result is available. Does not block.
     */
    public boolean hasResult() {
        synchronized(this) {
            return hasResult;
        }
    }

    /**
     * Sets the result and notifies any threads
     * waiting for it
     */
    public void setResult(Object obj) {
        synchronized(this) {
            result=obj;<commit_msg>modified getResult()<commit_after>// $Id: Promise.java,v 1.6 2004/09/16 15:04:25 belaban Exp $

package org.jgroups.util;

import org.jgroups.TimeoutException;


/**
 * Allows a thread to submit an asynchronous request and to wait for the result. The caller may choose to check
 * for the result at a later time, or immediately and it may block or not. Both the caller and responder have to
 * know the promise.
 */
public class Promise {
    Object result=null;
    boolean hasResult=false;





    /**
     * Blocks until a result is available, or timeout milliseconds have elapsed
     * @param timeout
     * @return An object
     * @throws TimeoutException. If a timeout occurred (implies that timeout > 0)
     */
    public Object getResultWithTimeout(long timeout) throws TimeoutException {
        Object  ret=null;
        long    time_to_wait=timeout, start;
        boolean timeout_occurred=false;

        synchronized(this) {
            start=System.currentTimeMillis();
            while(hasResult == false) {
                if(timeout <= 0) {
                    doWait();
                }
                else {
                    if(time_to_wait <= 0) {
                        timeout_occurred=true;
                        break; // terminate the while loop
                    }
                    else {
                        doWait(time_to_wait);
                        time_to_wait-=System.currentTimeMillis()-start;
                    }
                }
            }

            ret=result;
            result=null;
            hasResult=false;
            if(timeout_occurred)
                throw new TimeoutException();
            else
                return ret;
        }
    }

    public Object getResult() {
        try {
            return getResultWithTimeout(0);
        }
        catch(TimeoutException e) {
            return null;
        }
    }

    /**
     * Returns the result, but never throws a TimeoutException; returns null instead
     * @param timeout
     * @return
     */
    public Object getResult(long timeout) {
        try {
            return getResultWithTimeout(timeout);
        }
        catch(TimeoutException e) {
            return null;
        }
    }


    void doWait() {
        try {wait();} catch(InterruptedException e) {}
    }

    void doWait(long timeout) {
        try {wait(timeout);} catch(InterruptedException e) {}
    }




    /**
     * Checks whether result is available. Does not block.
     */
    public boolean hasResult() {
        synchronized(this) {
            return hasResult;
        }
    }

    /**
     * Sets the result and notifies any threads
     * waiting for it
     */
    public void setResult(Object obj) {
        synchronized(this) {
            result=obj;
{'fraction_non_alphanumeric': 0.08749288289998101, 'fraction_numerical': 0.00740178401973809, 'mean_word_length': 1.2686181661644425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, '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': '6698979', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1314, 'n_words': 384}