code: private CloseableDataStore asCloseableDataStore(final DataStore dataStore, final Optional<Runnable> onClose) {
          return (CloseableDataStore) Proxy.newProxyInstance(
                  DataStore.class.getClassLoader(), new Class[] { CloseableDataStore.class },
                  new AbstractInvocationHandler() {
                      @Override
                      protected Object handleInvocation(Object proxy, Method method, Object[] args)
                              throws Throwable {
                          if ("close".equals(method.getName())) {
                              if (onClose.isPresent()) {
                                  onClose.get().run();
                              }
                              return null;
                          } else {
                              return method.invoke(dataStore, args);
                          }
                      }
                  });
      }
docs: Creates a proxy which delegates all DataStore operations to the provided instance and delegates the Closeable's
  close() method to the provided Runnable if necessary.