public synchronized Promise<T> then(UniCallback<T> onFulfilled) { return then(onFulfilled, t -> { }); }
public synchronized Promise<T> whenThrown(UniCallback<Throwable> onThrown) { return then(v -> { }, onThrown); }
public static Promise<Void> race(Promise<?>... promises) { return new Promise<>((self, resolve, reject) -> { for (Promise<?> promise : promises) promise.then(v -> { if (self.state != State.REJECTED) resolve.invoke(null); }, reject); }); }
public static Promise<Void> all(Promise<?>... promises) { return new Promise<>((resolve, reject) -> { final int[] done = { 0 }; for (Promise<?> promise : promises) promise.then(v -> { done[0]++; if (done[0] == promises.length) resolve.invoke(null); }, reject); }); }
@Override public Promise<List<FilePath>> listFiles() { return new Promise<>((resolve, reject) -> isDirectory().then(isDirectory -> exists().then(exists -> { if (!isDirectory) throw new SilenceException("Cannot list files in a path which is not a directory."); if (!exists) throw new SilenceException("Cannot list files in a non existing directory."); List<FilePath> list = new ArrayList<>(); File file = new File(path); File[] children = file.listFiles(); if (children != null) for (File child : children) list.add(new LwjglExternalFilePath(path + SEPARATOR + child.getPath().replace(file.getPath(), ""))); resolve.invoke(Collections.unmodifiableList(list)); }, reject), reject)); }
@Override public Promise<Boolean> isFile() { return new Promise<>((resolve, reject) -> isDirectory().then(value -> resolve.invoke(!value))); }
@Override public Promise<Boolean> isFile() { return new Promise<>((resolve, reject) -> isDirectory().then(isDirectory -> resolve.invoke(!isDirectory))); }
@Override public Promise<List<FilePath>> listFiles() { return new Promise<>((resolve, reject) -> isDirectory().then(isDirectory -> exists().then(exists -> { if (!isDirectory) throw new SilenceException("Cannot list files in a path which is not a directory."); if (!exists) throw new SilenceException("Cannot list files in a non existing directory."); List<FilePath> list = new ArrayList<>(); File[] children = file.listFiles(); if (children != null) for (File child : children) list.add(new AndroidExternalFilePath(path + SEPARATOR + child.getPath().replace(file.getPath(), ""))); resolve.invoke(Collections.unmodifiableList(list)); }, reject), reject)); }
@Override public Promise<Boolean> delete() { return new Promise<>((resolve, reject) -> exists().then(exists -> isDirectory().then(isDirectory -> { if (!exists) throw new SilenceException("Cannot delete non existing file."); if (isDirectory) { listFiles().then(files -> { // Delete all the children first for (FilePath filePath : files) filePath.delete(); }); } resolve.invoke(file.delete()); }, reject), reject)); }
@Override public Promise<Void> moveTo(FilePath path) { return new Promise<>((resolve, reject) -> { InputStream in = null; try { in = getInputStream(); OutputStream out = ((AndroidFilePath) path).getOutputStream(false); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); out.flush(); out.close(); delete().then(deleted -> resolve.invoke(null), reject); } catch (IOException e) { reject.invoke(e); } }); }
public synchronized Promise<T> then(UniCallback<T> onFulfilled, UniCallback<Throwable> onRejected) { if (next != null) return next.then(onFulfilled, onRejected); next = new Promise<>(onFulfilled, onRejected); switch (state) { case REJECTED: next.reject(throwable); break; case FULFILLED: next.resolve(value); break; } return next; }
@Override public Promise<Void> mkdirs() { BiCallback<Boolean, Boolean> function = (isFile, exists) -> { if (isFile && !exists) getParent().mkdirs(); else file.mkdirs(); }; return new Promise<>((resolve, reject) -> isFile().then(isFile -> exists().then(exists -> { function.invoke(isFile, exists); resolve.invoke(null); }, reject), reject)); }
@Override public Promise<Long> sizeInBytes() { return new Promise<>((resolve, reject) -> exists().then(exists -> { if (exists) try { resolve.invoke(Files.size(Paths.get(path))); } catch (IOException e) { reject.invoke(e); } else resolve.invoke(-1L); }, reject)); }
private static void imageLoadHelper(FilePath path, ISubmitter<Image> submitter) { SilenceEngine.io.getImageReader() .readImage(path) .then(img -> submitter.submit(img, path)) .whenThrown(SilenceException::reThrow); }
@Override public Promise<List<FilePath>> listFiles() { return new Promise<>((resolve, reject) -> isDirectory().then(isDirectory -> exists().then(exists -> { try { if (!isDirectory) throw new SilenceException("Cannot list files in a path which is not a directory."); if (!exists) throw new SilenceException("Cannot list files in a non existing directory."); List<FilePath> filePaths = new ArrayList<>(); for (String s : assetManager.list(getPath())) filePaths.add(new AndroidResourceFilePath(getPath() + SEPARATOR + s)); resolve.invoke(Collections.unmodifiableList(filePaths)); } catch (IOException e) { reject.invoke(e); } }, reject), reject)); }
@Override public Promise<Void> createFile() { return new Promise<>((resolve, reject) -> isDirectory().then(isDirectory -> { if (isDirectory) throw new SilenceException("Cannot convert a directory to a file"); try { file.createNewFile(); resolve.invoke(null); } catch (IOException e) { reject.invoke(e); } })); }
private static void textureLoadHelper(FilePath path, ISubmitter<Texture> submitter) { SilenceEngine.io.getImageReader() .readImage(path) .then(img -> { submitter.submit(Texture.fromImage(img), path); img.dispose(); }) .whenThrown(SilenceException::reThrow); }
@Override public Promise<Boolean> isDirectory() { return new Promise<>((resolve, reject) -> exists().then(exists -> { if (!exists) resolve.invoke(false); else try { resolve.invoke(assetManager.list(getPath()).length != 0); } catch (IOException e) { resolve.invoke(false); } })); }
@Override public Promise<Long> sizeInBytes() { return new Promise<>((resolve, reject) -> exists().then(exists -> { if (!exists) resolve.invoke(-1L); else try { AssetFileDescriptor fd = assetManager.openFd(getPath()); long size = fd.getLength(); fd.close(); resolve.invoke(size); } catch (IOException e) { resolve.invoke(-1L); } })); }