PolarApiAccessor Class
Introduction#
The PolarApiAccessor class provides a way to access the Polar API. It serves as a factory for obtaining instances of the PolarApi interface.
The copy of this class inside the API artifact is a stub that always throws
PolarNotLoadedException. The real implementation is supplied by Polar at runtime, which is why the API must not be shaded.Methods#
access#
java
public static synchronized WeakReference<PolarApi> access() throws PolarNotLoadedException;Description:
This static method retrieves an instance of the PolarApi interface, wrapped in a WeakReference. If Polar is not loaded, a PolarNotLoadedException is thrown.
The method is synchronized; calling it concurrently from several threads causes contention. Call it once and keep the returned reference rather than calling it on every access.
Returns:
A WeakReference to the PolarApi instance. WeakReference#get() returns null if the API object has been collected, for example after Polar was unloaded.
Throws:
PolarNotLoadedException - If Polar is not loaded in the current environment. See Exceptions.
Example#
java
try {
PolarApi api = PolarApiAccessor.access().get();
if (api == null) {
// Polar was unloaded
return;
}
// use api
} catch (PolarNotLoadedException e) {
// Polar is not loaded
}