{%- import "macros.java" as java %}
// This template implements a class for working with a Rust struct via a handle
// to the live Rust struct on the other side of the FFI.
//
// Each instance implements core operations for working with the Rust `Arc<T>` and the
// Java handle to work with the live Rust struct on the other side of the FFI.
//
// There's some subtlety here, because we have to be careful not to operate on a Rust
// struct after it has been dropped, and because we must expose a public API for freeing
// the Java wrapper object in lieu of reliable finalizers. The core requirements are:
//
//   * Each instance holds an opaque handle to the underlying Rust struct.
//     Method calls need to read this handle from the object's state and pass it in to
//     the Rust FFI.
//
//   * When an instance is no longer needed, its handle should be passed to a
//     special destructor function provided by the Rust FFI, which will drop the
//     underlying Rust struct.
//
//   * Given an instance, calling code is expected to call the special
//     `close` method in order to free it after use, either by calling it explicitly
//     or by using a higher-level helper like `try-with-resources`. Failing to do so risks
//     leaking the underlying Rust struct.
//
//   * We can't assume that calling code will do the right thing, and must be prepared
//     to handle Java method calls executing concurrently with or even after a call to
//     `close`, and to handle multiple (possibly concurrent!) calls to `close`.
//
//   * We must never allow Rust code to operate on the underlying Rust struct after
//     the destructor has been called, and must never call the destructor more than once.
//     Doing so may trigger memory unsafety.
//
//   * To mitigate many of the risks of leaking memory and use-after-free unsafety, a `Cleaner`
//     is implemented to call the destructor when the Java object becomes unreachable.
//     This is done in a background thread. This is not a panacea, and client code should be aware that
//      1. the thread may starve if some there are objects that have poorly performing
//     `drop` methods or do significant work in their `drop` methods.
//      2. the thread is shared across the whole library. This can be tuned by using `android_cleaner = true`,
//         or `android = true` in the [`java` section of the `uniffi.toml` file, like the Kotlin one](https://mozilla.github.io/uniffi-rs/kotlin/configuration.html).
//
// If we try to implement this with mutual exclusion on access to the handle, there is the
// possibility of a race between a method call and a concurrent call to `close`:
//
//    * Thread A starts a method call, reads the value of the handle, but is interrupted
//      before it can pass the handle over the FFI to Rust.
//    * Thread B calls `close` and frees the underlying Rust struct.
//    * Thread A resumes, passing the already-read handle value to Rust and triggering
//      a use-after-free.
//
// One possible solution would be to use a `ReadWriteLock`, with each method call taking
// a read lock (and thus allowed to run concurrently) and the special `close` method
// taking a write lock (and thus blocking on live method calls). However, we aim not to
// generate methods with any hidden blocking semantics, and a `close` method that might
// block if called incorrectly seems to meet that bar.
//
// So, we achieve our goals by giving each instance an associated `AtomicLong` counter to track
// the number of in-flight method calls, and an `AtomicBoolean` flag to indicate whether `close`
// has been called. These are updated according to the following rules:
//
//    * The initial value of the counter is 1, indicating a live object with no in-flight calls.
//      The initial value for the flag is false.
//
//    * At the start of each method call, we atomically check the counter.
//      If it is 0 then the underlying Rust struct has already been destroyed and the call is aborted.
//      If it is nonzero them we atomically increment it by 1 and proceed with the method call.
//
//    * At the end of each method call, we atomically decrement and check the counter.
//      If it has reached zero then we destroy the underlying Rust struct.
//
//    * When `close` is called, we atomically flip the flag from false to true.
//      If the flag was already true we silently fail.
//      Otherwise we atomically decrement and check the counter.
//      If it has reached zero then we destroy the underlying Rust struct.
//
// Astute readers may observe that this all sounds very similar to the way that Rust's `Arc<T>` works,
// and indeed it is, with the addition of a flag to guard against multiple calls to `close`.
//
// The overall effect is that the underlying Rust struct is destroyed only when `close` has been
// called *and* all in-flight method calls have completed, avoiding violating any of the expectations
// of the underlying Rust code.
//
// This makes a cleaner a better alternative to _not_ calling `close()` as
// and when the object is finished with, but the abstraction is not perfect: if the Rust object's `drop`
// method is slow, and/or there are many objects to cleanup, and it's on a low end Android device, then the cleaner
// thread may be starved, and the app will leak memory.
//
// In this case, `close`ing manually may be a better solution.
//
// The cleaner can live side by side with the manual calling of `close`. In the order of responsiveness, uniffi objects
// with Rust peers are reclaimed:
//
// 1. By calling the `close` method of the object, which calls `rustObject.free()`. If that doesn't happen:
// 2. When the object becomes unreachable, AND the Cleaner thread gets to call `rustObject.free()`. If the thread is starved then:
// 3. The memory is reclaimed when the process terminates.
//
// [1] https://stackoverflow.com/questions/24376768/can-java-finalize-an-object-when-it-is-still-in-scope/24380219
//

{%- let (interface_name, impl_class_name) = obj|object_names(ci) %}
{%- let methods = obj.methods() %}
{%- let uniffi_trait_methods = obj.uniffi_trait_methods() %}
{%- let interface_docstring = obj.docstring() %}
{%- let ffi_converter_name = obj|ffi_converter_name %}

{%- include "Interface.java" %}

package {{ config.package_name() }};

{%- call java::docstring(obj, 0) %}{% endcall %}
{% if (is_error) %}
public class {{ impl_class_name }} extends Exception implements AutoCloseable, {{ interface_name }}{% for t in obj.trait_impls() %}, {{ t.trait_ty|trait_interface_name(ci) }}{% endfor %}{% if uniffi_trait_methods.ord_cmp.is_some() %}, Comparable<{{ impl_class_name }}>{% endif %} {
{% else -%}
public class {{ impl_class_name }} implements AutoCloseable, {{ interface_name }}{% for t in obj.trait_impls() %}, {{ t.trait_ty|trait_interface_name(ci) }}{% endfor %}{% if uniffi_trait_methods.ord_cmp.is_some() %}, Comparable<{{ impl_class_name }}>{% endif %} {
{%- endif %}
  protected long handle;
  protected UniffiCleaner.{% if config.nullness_annotations() %}@org.jspecify.annotations.Nullable {% endif %}Cleanable cleanable;

  private java.util.concurrent.atomic.AtomicBoolean wasDestroyed = new java.util.concurrent.atomic.AtomicBoolean(false);
  private java.util.concurrent.atomic.AtomicLong callCounter = new java.util.concurrent.atomic.AtomicLong(1);

  /**
   * Internal constructor to wrap a raw handle from FFI.
   * The UniffiWithHandle marker disambiguates this from other constructors.
   */
  public {{ impl_class_name }}(UniffiWithHandle phantom, long handle) {
    this.handle = handle;
    this.cleanable = UniffiLib.CLEANER.register(this, new UniffiCleanAction(handle));
  }

  /**
   * This constructor can be used to instantiate a fake object. Only used for tests. Any
   * attempt to actually use an object constructed this way will fail as there is no
   * connected Rust object.
   */
  public {{ impl_class_name }}(NoHandle noHandle) {
    this.handle = 0L;
    this.cleanable = null;
  }

  {% match obj.primary_constructor() %}
  {%- when Some(cons) %}
  {%-     if cons.is_async() %}
  // Note no constructor generated for this object as it is async.
  {%-     else %}
  {%- call java::docstring(cons, 4) %}{% endcall %}
  public {{ impl_class_name }}({% call java::arg_list(cons, true) %}{% endcall -%}) {% match cons.throws_type() %}{% when Some(throwable) %}throws {{ throwable|type_name(ci, config) }}{% else %}{% endmatch %}{
    this(UniffiWithHandle.INSTANCE, (long){%- call java::to_ffi_call(cons) %}{% endcall -%});
  }
  {%-     endif %}
  {%- when None %}
  {%- endmatch %}

  @Override
  public synchronized void close() {
    // Only allow a single call to this method.
    // TODO(uniffi): maybe we should log a warning if called more than once?
    if (this.wasDestroyed.compareAndSet(false, true)) {
      // This decrement always matches the initial count of 1 given at creation time.
      if (this.callCounter.decrementAndGet() == 0L) {
        if (cleanable != null) {
          cleanable.clean();
        }
      }
    }
  }

  public <R> R callWithHandle(java.util.function.Function<java.lang.Long, R> block) {
    // Check and increment the call counter, to keep the object alive.
    // This needs a compare-and-set retry loop in case of concurrent updates.
    long c;
    do {
      c = this.callCounter.get();
      if (c == 0L) {
        throw new java.lang.IllegalStateException("{{ impl_class_name }} object has already been destroyed");
      }
      if (c == java.lang.Long.MAX_VALUE) {
        throw new java.lang.IllegalStateException("{{ impl_class_name }} call counter would overflow");
      }
    } while (! this.callCounter.compareAndSet(c, c + 1L));
    // Now we can safely do the method call without the handle being freed concurrently.
    try {
      return block.apply(this.uniffiCloneHandle());
    } finally {
      // This decrement always matches the increment we performed above.
      if (this.callCounter.decrementAndGet() == 0L) {
        if (cleanable != null) {
          cleanable.clean();
        }
      }
    }
  }

  public void callWithHandle(java.util.function.Consumer<java.lang.Long> block) {
    callWithHandle((java.lang.Long uniffiHandle) -> {
      block.accept(uniffiHandle);
      return (java.lang.Void)null;
    });
  }

  private class UniffiCleanAction implements Runnable {
    private final long handle;

    public UniffiCleanAction(long handle) {
      this.handle = handle;
    }

    @Override
    public void run() {
      // If the handle is 0 this is a fake object created with `NoHandle`, don't try to free.
      if (handle != 0L) {
        UniffiHelpers.uniffiRustCall((_allocator, status) -> {
          UniffiLib.{{ obj.ffi_object_free().name() }}(handle, status);
          return null;
        });
      }
    }
  }

  long uniffiCloneHandle() {
    return UniffiHelpers.uniffiRustCall((_allocator, status) -> {
      if (handle == 0L) {
        throw new java.lang.NullPointerException();
      }
      return UniffiLib.{{ obj.ffi_object_clone().name() }}(handle, status);
    });
  }

  {% for meth in obj.methods() -%}
  {%- call java::func_decl("public", "Override", meth, 4) %}{% endcall %}
  {% endfor %}

  {% call java::uniffi_trait_impls(uniffi_trait_methods) %}{% endcall %}

  {% if !obj.alternate_constructors().is_empty() -%}
  {% for cons in obj.alternate_constructors() -%}
  {% call java::func_decl("public static", "", cons, 4) %}{% endcall %}
  {% endfor %}
  {% endif %}
}

{% if is_error %}
package {{ config.package_name() }};

public class {{ impl_class_name }}ErrorHandler implements UniffiRustCallStatusErrorHandler<{{ impl_class_name }}> {
    @Override
    public {{ impl_class_name }} lift(java.lang.foreign.MemorySegment error_buf) {
        // Due to some mismatches in the ffi converter mechanisms, errors are a RustBuffer.
        var bb = RustBuffer.asByteBuffer(error_buf);
        if (bb == null) {
            throw new InternalException("?");
        }
        return {{ ffi_converter_instance }}.read(bb);
    }
}
{% endif %}

{%- if !obj.has_callback_interface() %}
{#- Simple case: the interface can only be implemented in Rust -#}

package {{ config.package_name() }};

public enum {{ ffi_converter_name }} implements FfiConverter<{{ type_name }}, java.lang.Long> {
    INSTANCE;

    @Override
    public java.lang.Long lower({{ type_name }} value) {
        return value.uniffiCloneHandle();
    }

    @Override
    public {{ type_name }} lift(java.lang.Long value) {
        return new {{ impl_class_name }}(UniffiWithHandle.INSTANCE, value);
    }

    @Override
    public {{ type_name }} read(java.nio.ByteBuffer buf) {
        // The Rust code always writes handles as 8 bytes, and will
        // fail to compile if they don't fit.
        return lift(buf.getLong());
    }

    @Override
    public long allocationSize({{ type_name }} value) {
        return 8L;
    }

    @Override
    public void write({{ type_name }} value, java.nio.ByteBuffer buf) {
        // The Rust code always expects handles written as 8 bytes,
        // and will fail to compile if they don't fit.
        buf.putLong(lower(value));
    }
}
{%- else %}
{#-
   The interface can be implemented in Rust or Java.
   * Generate a callback interface implementation to handle the Java side.
   * In the FfiConverter, check which side a handle came from to know how to handle it correctly.
-#}
{%- let vtable = obj.vtable().expect("trait interface should have a vtable") %}
{%- let vtable_methods = obj.vtable_methods() %}
{%- let ffi_init_callback = obj.ffi_init_callback() %}
{% include "CallbackInterfaceImpl.java" %}

package {{ config.package_name() }};

public enum {{ ffi_converter_name }} implements FfiConverter<{{ type_name }}, java.lang.Long> {
    INSTANCE;

    public final UniffiHandleMap<{{ type_name }}> handleMap = new UniffiHandleMap<>();

    @Override
    public java.lang.Long lower({{ type_name }} value) {
        if (value instanceof {{ impl_class_name }}) {
            // Rust-implemented object. Clone the handle and return it.
            return (({{ impl_class_name }}) value).uniffiCloneHandle();
        } else {
            // Java object, generate a new handle and return that.
            return handleMap.insert(value);
        }
    }

    @Override
    public {{ type_name }} lift(java.lang.Long value) {
        // Check the LSB: Rust handles have LSB = 0, Java handles have LSB = 1
        if ((value & 1L) == 0L) {
            // Rust-generated handle
            return new {{ impl_class_name }}(UniffiWithHandle.INSTANCE, value);
        } else {
            // Java-generated handle - remove from handleMap (handles are single-use)
            return handleMap.remove(value);
        }
    }

    @Override
    public {{ type_name }} read(java.nio.ByteBuffer buf) {
        // The Rust code always writes handles as 8 bytes, and will
        // fail to compile if they don't fit.
        return lift(buf.getLong());
    }

    @Override
    public long allocationSize({{ type_name }} value) {
        return 8L;
    }

    @Override
    public void write({{ type_name }} value, java.nio.ByteBuffer buf) {
        // The Rust code always expects handles written as 8 bytes,
        // and will fail to compile if they don't fit.
        buf.putLong(lower(value));
    }
}
{%- endif %}