<commit_before>/*
**  Copyright (C) 2015 Aldebaran Robotics
**  See COPYING for the license
*/
package com.aldebaran.qi;

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

/**
 * @author proullon
 *
 * @param <T>
 */
public class Future<T> implements java.util.concurrent.Future<T>
{

  // Loading QiMessaging JNI layer
  static
  {
    if (!EmbeddedTools.LOADED_EMBEDDED_LIBRARY)
    {
      EmbeddedTools loader = new EmbeddedTools();
      loader.loadEmbeddedLibraries();
    }
  }

  public interface Callback<T>
  {
    void onFinished(Future<T> future);
  }

  private static final int TIMEOUT_INFINITE = -1;

  // C++ Future
  private long  _fut;

  // Native C API object functions
  private native boolean qiFutureCallCancel(long pFuture);
  private native Object qiFutureCallGet(long pFuture, int msecs) throws ExecutionException, TimeoutException;
  private native boolean qiFutureCallIsCancelled(long pFuture);
  private native boolean qiFutureCallIsDone(long pFuture);
  private native boolean qiFutureCallConnect(long pFuture, Object callback, String className, Object[] args);
  private native void qiFutureCallWaitWithTimeout(long pFuture, int timeout);
  private native void qiFutureDestroy(long pFuture);
  private native void qiFutureCallConnectCallback(long pFuture, Callback<?> callback);
  private native long qiFutureCallThen(long pFuture, QiFunction<?, ?> function);
  private native long qiFutureCallAndThen(long pFuture, QiFunction<?, ?> function);
  private static native long qiFutureCreate(Object value);

  private boolean cancelled;

  Future(long pFuture)
  {
    _fut = pFuture;
  }

  public static <T> Future<T> of(T value)
  {
    return new Future<T>(qiFutureCreate(value));
  }

  public void sync(long timeout, TimeUnit unit)
  {
    qiFutureCallWaitWithTimeout(_fut, (int) unit.toMillis(timeout));
  }

  public void sync()
  {
    this.sync(0, TimeUnit.SECONDS);
  }

  /**
   * Callbacks to future can be set.
   * @param callback com.aldebaran.qi.Callback implementation
   * @param args Argument to be forwarded to callback functions.
   * @return true on success.
   * @since 1.20
   */
  @Deprecated
  public boolean addCallback(com.aldebaran.qi.Callback<?> callback, Object ... args)
  {
    String className = callback.getClass().toString();
    className = className.substring(6); // Remove "class "
    className = className.replace('.', '/');

    return qiFutureCallConnect(_fut, callback, className, args);
  }

  public void connect(Callback<T> callback)
  {
    qiFutureCallConnectCallback(_fut, callback);
  }

  @Override
  public boolean cancel(boolean mayInterruptIfRunning)
  {
    // ignore mayInterruptIfRunning, can't map it to native libqi
    return cancel();
  }

  public synchronized boolean cancel()
  {
    if (qiFutureCallCancel(_fut))
      cancelled = true;
    return cancelled;
  }

  @SuppressWarnings("unchecked")
  private T get(int msecs) throws ExecutionException, TimeoutException
  {

    // inherited from java.util.concurrent.Future, it must match its semantics
    // i.e. it must throw after any successful call to cancel(…)
    if (isCancelled())
      throw new CancellationException();

    // there is a harmless race condition here: if cancel() is called after the
    // check but before the call to qiFutureCallGet(), then the cancellation
    // will be handled only after get() has returned

    Object result = qiFutureCallGet(_fut, msecs);

    // we don't want to keep the "cancelled" mutex during qiFutureCallGet()
    // so we have to check again
    if (isCancelled())
      throw new CancellationException();

    return (T) result;
  }

  @Override
  public T get() throws ExecutionException
  {<commit_msg>blind fix to make qiFutureCallCancel work<commit_after>/*
**  Copyright (C) 2015 Aldebaran Robotics
**  See COPYING for the license
*/
package com.aldebaran.qi;

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

/**
 * @author proullon
 *
 * @param <T>
 */
public class Future<T> implements java.util.concurrent.Future<T>
{

  // Loading QiMessaging JNI layer
  static
  {
    if (!EmbeddedTools.LOADED_EMBEDDED_LIBRARY)
    {
      EmbeddedTools loader = new EmbeddedTools();
      loader.loadEmbeddedLibraries();
    }
  }

  public interface Callback<T>
  {
    void onFinished(Future<T> future);
  }

  private static final int TIMEOUT_INFINITE = -1;

  // C++ Future
  private long  _fut;

  // Native C API object functions
  private native boolean qiFutureCallCancel(long pFuture, boolean mayInterrupt);
  private native Object qiFutureCallGet(long pFuture, int msecs) throws ExecutionException, TimeoutException;
  private native boolean qiFutureCallIsCancelled(long pFuture);
  private native boolean qiFutureCallIsDone(long pFuture);
  private native boolean qiFutureCallConnect(long pFuture, Object callback, String className, Object[] args);
  private native void qiFutureCallWaitWithTimeout(long pFuture, int timeout);
  private native void qiFutureDestroy(long pFuture);
  private native void qiFutureCallConnectCallback(long pFuture, Callback<?> callback);
  private native long qiFutureCallThen(long pFuture, QiFunction<?, ?> function);
  private native long qiFutureCallAndThen(long pFuture, QiFunction<?, ?> function);
  private static native long qiFutureCreate(Object value);

  private boolean cancelled;

  Future(long pFuture)
  {
    _fut = pFuture;
  }

  public static <T> Future<T> of(T value)
  {
    return new Future<T>(qiFutureCreate(value));
  }

  public void sync(long timeout, TimeUnit unit)
  {
    qiFutureCallWaitWithTimeout(_fut, (int) unit.toMillis(timeout));
  }

  public void sync()
  {
    this.sync(0, TimeUnit.SECONDS);
  }

  /**
   * Callbacks to future can be set.
   * @param callback com.aldebaran.qi.Callback implementation
   * @param args Argument to be forwarded to callback functions.
   * @return true on success.
   * @since 1.20
   */
  @Deprecated
  public boolean addCallback(com.aldebaran.qi.Callback<?> callback, Object ... args)
  {
    String className = callback.getClass().toString();
    className = className.substring(6); // Remove "class "
    className = className.replace('.', '/');

    return qiFutureCallConnect(_fut, callback, className, args);
  }

  public void connect(Callback<T> callback)
  {
    qiFutureCallConnectCallback(_fut, callback);
  }

  @Override
  public boolean cancel(boolean mayInterruptIfRunning)
  {
    // ignore mayInterruptIfRunning, can't map it to native libqi
    return cancel();
  }

  public synchronized boolean cancel()
  {
    if (qiFutureCallCancel(_fut, true))
      cancelled = true;
    return cancelled;
  }

  @SuppressWarnings("unchecked")
  private T get(int msecs) throws ExecutionException, TimeoutException
  {

    // inherited from java.util.concurrent.Future, it must match its semantics
    // i.e. it must throw after any successful call to cancel(…)
    if (isCancelled())
      throw new CancellationException();

    // there is a harmless race condition here: if cancel() is called after the
    // check but before the call to qiFutureCallGet(), then the cancellation
    // will be handled only after get() has returned

    Object result = qiFutureCallGet(_fut, msecs);

    // we don't want to keep the "cancelled" mutex during qiFutureCallGet()
    // so we have to check again
    if (isCancelled())
      throw new CancellationException();

    return (T) result;
  }

  @Override
  public T get() throws ExecutionException
  {
{'fraction_non_alphanumeric': 0.0952995867768595, 'fraction_numerical': 0.0025826446280991736, 'mean_word_length': 3.8802772526780087, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '4736090', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2266, 'n_words': 692}