diff --git a/Java/Loon-Neo/src/loon/Asyn.java b/Java/Loon-Neo/src/loon/Asyn.java index 468f0815e..91d7a027d 100644 --- a/Java/Loon-Neo/src/loon/Asyn.java +++ b/Java/Loon-Neo/src/loon/Asyn.java @@ -1,18 +1,18 @@ /** * Copyright 2008 - 2015 The Loon Game Engine Authors - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. - * + * * @project loon * @author cping * @email:javachenpeng@yahoo.com @@ -26,129 +26,137 @@ import loon.utils.reply.Port; public abstract class Asyn { - - public static class Default extends Asyn { - - /** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/ - private class CallDefaultPort extends Port{ - - private Default _def; - - CallDefaultPort(Default d){ - this._def = d; - } - - @Override - public void onEmit(Object event) { - _def.dispatch(); - } - - } - - private final TArray pending = new TArray<>(); - private final TArray running = new TArray<>(); - protected final Log log; - - public Default(Log log, Act frame) { - this.log = log; - frame.connect(new CallDefaultPort(this)).setPriority(Short.MAX_VALUE); - } - - @Override - public boolean isAsyncSupported() { - return false; - } - - @Override - public void invokeAsync(Runnable action) { - throw new UnsupportedOperationException(); - } - - @Override - public synchronized void invokeLater(Runnable action) { - pending.add(action); - } - - private void dispatch() { - synchronized (this) { - running.addAll(pending); - pending.clear(); - } - - for (int ii = 0, ll = running.size; ii < ll; ii++) { - Runnable action = running.get(ii); - try { - action.run(); - } catch (Exception e) { - log.warn("invokeLater Runnable failed: " + action, e); - } - } - running.clear(); - } - } - - public abstract void invokeLater(Runnable action); - - /** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/ - private static class DeferredPromiseRunnable implements Runnable { - - private GoPromise _promise; - - private int _mode = 0; - - private T _value; - - private Throwable _cause; - - public DeferredPromiseRunnable(int m, GoPromise p, T val, Throwable c) { - this._mode = m; - this._promise = p; - this._value = val; - this._cause = c; - } - - @Override - public void run() { - switch (_mode) { - case 0: - _promise.succeed(_value); - break; - default: - _promise.fail(_cause); - break; - } - } - } - - /** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/ - private class CallDeferredPromise extends GoPromise { - - private Asyn _asyn; - - public CallDeferredPromise(Asyn a) { - this._asyn = a; - } - - @Override - public void succeed(final T value) { - _asyn.invokeLater(new DeferredPromiseRunnable(0, this, value, - null)); - } - - @Override - public void fail(final Throwable cause) { - _asyn.invokeLater(new DeferredPromiseRunnable(1, this, null, - cause)); - } - } - - public GoPromise deferredPromise() { - return new CallDeferredPromise(this); - } - - public abstract boolean isAsyncSupported(); - - public void invokeAsync(Runnable action) { - throw new UnsupportedOperationException(); - } + + public static class Default extends Asyn { + + /** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/ + private class CallDefaultPort extends Port{ + + private Default _def; + + CallDefaultPort(Default d){ + this._def = d; + } + + @Override + public void onEmit(Object event) { + _def.dispatch(); + } + + } + + private final TArray pending = new TArray<>(); + private final TArray running = new TArray<>(); + protected final Log log; + + public Default(Log log, Act frame) { + this.log = log; + frame.connect(new CallDefaultPort(this)).setPriority(Short.MAX_VALUE); + } + + @Override + public boolean isAsyncSupported() { + return false; + } + + @Override + public void invokeAsync(Runnable action) { + throw new UnsupportedOperationException(); + } + + @Override + public synchronized void invokeLater(Runnable action) { + pending.add(action); + } + + private void dispatch() { + synchronized (this) { + running.addAll(pending); + pending.clear(); + } + + for (int ii = 0, ll = running.size; ii < ll; ii++) { + Runnable action = running.get(ii); + try { + action.run(); + } catch (Exception e) { + log.warn("invokeLater Runnable failed: " + action, e); + } + } + running.clear(); + } + } + + public abstract void invokeLater(Runnable action); + + /** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/ + private static class DeferredPromiseRunnable implements Runnable { + + private CallDeferredPromise _promise; + + private int _mode = 0; + + private T _value; + + private Throwable _cause; + + public DeferredPromiseRunnable(int m, CallDeferredPromise p, T val, Throwable c) { + this._mode = m; + this._promise = p; + this._value = val; + this._cause = c; + } + + @Override + public void run() { + switch (_mode) { + case 0: + _promise.invokeSucceed(_value); + break; + default: + _promise.invokeFail(_cause); + break; + } + } + } + + /** 为了语法转换到C#和C++,只能忍痛放弃匿名构造类了…… **/ + private class CallDeferredPromise extends GoPromise { + + private Asyn _asyn; + + public CallDeferredPromise(Asyn a) { + this._asyn = a; + } + + @Override + public void succeed(final T value) { + _asyn.invokeLater(new DeferredPromiseRunnable(0, this, value, + null)); + } + + @Override + public void fail(final Throwable cause) { + _asyn.invokeLater(new DeferredPromiseRunnable(1, this, null, + cause)); + } + + public void invokeSucceed(final T value) { + super.succeed(value); + } + + public void invokeFail(final Throwable cause) { + super.fail(cause); + } + } + + public GoPromise deferredPromise() { + return new CallDeferredPromise(this); + } + + public abstract boolean isAsyncSupported(); + + public void invokeAsync(Runnable action) { + throw new UnsupportedOperationException(); + } }