下面列出了怎么用com.google.gwt.core.client.Duration的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Registers the Gadget object as RPC event listener with the Gadget RPC
* Controller after waiting for the Gadget RPC library to load.
*/
private void scheduleControllerRegistration(
final String url, final long width, final long height) {
new ScheduleTimer() {
private double loadWarningTime =
Duration.currentTimeMillis() + GADGET_RPC_LOAD_WARNING_TIMEOUT_MS;
@Override
public void run() {
if (!isActive()) {
cancel();
log("Not active.");
return;
} else if (gadgetLibraryLoaded()) {
cancel();
controllerRegistration(url, width, height);
} else {
if (Duration.currentTimeMillis() > loadWarningTime) {
log("Gadget RPC script failed to load on time.");
loadWarningTime += GADGET_RPC_LOAD_WARNING_TIMEOUT_MS;
}
}
}
}.scheduleRepeating(GADGET_RPC_LOAD_TIMER_MS);
}
/**
* Entry point to the program
*/
public void onModuleLoad() {
JavascriptLib.init();
// Feature detection
if (!featureHandler.detect()) return;
// Atlas to look up position of textures
xhr = Browser.getWindow().newXMLHttpRequest();
xhr.open("GET", "http://" + getConfigAdddress() + "/resources/blocks.json", true);
xhr.setOnload(this);
xhr.send();
Platform.runRepeated(new Runnable() {
@Override
public void run() {
if (connection == null) return;
if (Duration.currentTimeMillis() - lastKeepAlive > 1000) {
lastKeepAlive = Duration.currentTimeMillis();
connection.send(new KeepAlive());
}
}
}, 1000);
}
/**
* Registers the Gadget object as RPC event listener with the Gadget RPC
* Controller after waiting for the Gadget RPC library to load.
*/
private void scheduleControllerRegistration(
final String url, final long width, final long height) {
new ScheduleTimer() {
private double loadWarningTime =
Duration.currentTimeMillis() + GADGET_RPC_LOAD_WARNING_TIMEOUT_MS;
@Override
public void run() {
if (!isActive()) {
cancel();
log("Not active.");
return;
} else if (gadgetLibraryLoaded()) {
cancel();
controllerRegistration(url, width, height);
} else {
if (Duration.currentTimeMillis() > loadWarningTime) {
log("Gadget RPC script failed to load on time.");
loadWarningTime += GADGET_RPC_LOAD_WARNING_TIMEOUT_MS;
}
}
}
}.scheduleRepeating(GADGET_RPC_LOAD_TIMER_MS);
}
/**
* Initializes the tracking information on each link in the chain
*
* @param actionName the name of action this chain of commands represents
* @param chainDuration duration to be used for the entire chain
*/
private void initTrackingInformation(String actionName, Duration chainDuration) {
this.actionName = actionName;
// Note that all links in the chain have a reference to the same chainDuration.
this.chainDuration = chainDuration;
if (nextCommand != null) {
nextCommand.initTrackingInformation(actionName, chainDuration);
}
}
/**
* Kick off the chain of commands.
*
* @param actionName the name of action this chain of commands represents
* @param node the project node to which the chain of commands is applied
* @param finallyCommand a command to execute after the chain is finished,
* regardless of whether it succeeds
*/
public final void startExecuteChain(String actionName, ProjectNode node,
Command finallyCommand) {
// The node must not be null.
// If you are calling startExecuteChain with null for the node parameter, maybe you should
// question why you are using a ChainableCommand at all. ChainableCommands were designed to
// perform an operation on a ProjectNode.
Preconditions.checkNotNull(node);
setFinallyCommand(finallyCommand);
initTrackingInformation(actionName, new Duration());
executeLink(node);
}
/**
* Outputs the time passed since various events recorded in
* performance.timing if supported by the browser.
*/
public static void logBootstrapTimings() {
if (isEnabled()) {
double now = Duration.currentTimeMillis();
String[] keys = new String[] { "navigationStart",
"unloadEventStart", "unloadEventEnd", "redirectStart",
"redirectEnd", "fetchStart", "domainLookupStart",
"domainLookupEnd", "connectStart", "connectEnd",
"requestStart", "responseStart", "responseEnd",
"domLoading", "domInteractive",
"domContentLoadedEventStart", "domContentLoadedEventEnd",
"domComplete", "loadEventStart", "loadEventEnd" };
LinkedHashMap<String, Double> timings = new LinkedHashMap<>();
for (String key : keys) {
double value = getPerformanceTiming(key);
if (value == 0) {
// Ignore missing value
continue;
}
timings.put(key, Double.valueOf(now - value));
}
if (timings.isEmpty()) {
Console.log(
"Bootstrap timings not supported, please ensure your browser supports performance.timing");
return;
}
if (getConsumer() != null) {
getConsumer().addBootstrapData(timings);
}
}
}
/**
* @return Timestamp for use in log
*/
private static String timeStamp() {
double ts = Duration.currentTimeMillis() / 1000.0;
// divide the startTime to second from millsecond and seconds is much easier to read
// for the user.
if (TIMESTAMP_FORMAT != null) {
return TIMESTAMP_FORMAT.format(ts);
} else {
return Double.toString(ts);
}
}
@Override
public double currentTimeMillis() {
// Replace this with just Duration.currentTimeMillis() when it is itself
// implemented with a GWT.isClient() check.
return GWT.isClient()
? Duration.currentTimeMillis()
: System.currentTimeMillis();
}
@Override
public void moveTo(double location) {
startLocation = getViewport().getStart();
endLocation = location;
counter = new Duration();
execute();
scheduler.scheduleDelayed(this, 0);
}
/**
* This is used to get a efficient time for JS.
* Warning! Use TimerService if you want to actually test and control the time.
*/
public double currentTimeMillis() {
// Use an optimised time for JS when running in JS.
if (!GWT.isClient()) {
return System.currentTimeMillis();
} else {
return Duration.currentTimeMillis();
}
}
private boolean onKeyDown(char keyCode) {
switch (keyCode) {
case 'W':
movingDirection = 1;
return true;
case 'S':
movingDirection = -1;
return true;
case 'A':
sideDirection = -1;
return true;
case 'D':
sideDirection = 1;
return true;
case 32:
double now = Duration.currentTimeMillis();
if (now - lastJump > 120 && now - lastJump < 250) {
flying = !flying;
} else {
if (onGround) {
vSpeed = 0.15f;
}
}
lastJump = now;
return true;
}
return false;
}
/**
* @return Timestamp for use in log
*/
private static String timeStamp() {
double ts = Duration.currentTimeMillis() / 1000.0;
// divide the startTime to second from millsecond and seconds is much easier to read
// for the user.
if (TIMESTAMP_FORMAT != null) {
return TIMESTAMP_FORMAT.format(ts);
} else {
return Double.toString(ts);
}
}
@Override
public double currentTimeMillis() {
// Replace this with just Duration.currentTimeMillis() when it is itself
// implemented with a GWT.isClient() check.
return GWT.isClient()
? Duration.currentTimeMillis()
: System.currentTimeMillis();
}
@Override
public void moveTo(double location) {
startLocation = getViewport().getStart();
endLocation = location;
counter = new Duration();
execute();
scheduler.scheduleDelayed(this, 0);
}
/**
* This is used to get a efficient time for JS.
* Warning! Use TimerService if you want to actually test and control the time.
*/
public double currentTimeMillis() {
// Use an optimised time for JS when running in JS.
if (!GWT.isClient()) {
return System.currentTimeMillis();
} else {
return Duration.currentTimeMillis();
}
}
/**
* Resets the start time for the link duration timer
*
*/
private final void resetLinkDuration() {
// Note that each link in the chain has a unique linkDuration.
linkDuration = new Duration();
}
/**
* Load a stylesheet and notify a listener when the stylesheet is loaded.
* Calling this method when the stylesheet is currently loading or already
* loaded doesn't cause the stylesheet to be loaded again, but the listener
* will still be notified when appropriate.
*
* @param stylesheetUrl
* the url of the stylesheet to load
* @param resourceLoadListener
* the listener that will get notified when the stylesheet is
* loaded
*/
public void loadStylesheet(final String stylesheetUrl,
final ResourceLoadListener resourceLoadListener) {
final String url = WidgetUtil.getAbsoluteUrl(stylesheetUrl);
final ResourceLoadEvent event = new ResourceLoadEvent(this, url);
if (loadedResources.has(url)) {
if (resourceLoadListener != null) {
resourceLoadListener.onLoad(event);
}
return;
}
if (addListener(url, resourceLoadListener, loadListeners)) {
LinkElement linkElement = getDocument().createLinkElement();
linkElement.setRel("stylesheet");
linkElement.setType("text/css");
linkElement.setHref(url);
if (BrowserInfo.get().isSafariOrIOS()) {
// Safari doesn't fire any events for link elements
// See http://www.phpied.com/when-is-a-stylesheet-really-loaded/
Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
private final Duration duration = new Duration();
@Override
public boolean execute() {
int styleSheetLength = getStyleSheetLength(url);
if (getStyleSheetLength(url) > 0) {
fireLoad(event);
return false; // Stop repeating
} else if (styleSheetLength == 0) {
// "Loaded" empty sheet -> most likely 404 error
fireError(event);
return true;
} else if (duration.elapsedMillis() > 60 * 1000) {
fireError(event);
return false;
} else {
return true; // Continue repeating
}
}
}, 10);
} else {
addOnloadHandler(linkElement, new StyleSheetLoadListener(url),
event);
if (BrowserInfo.get().isOpera()) {
// Opera onerror never fired, assume error if no onload in x
// seconds
new Timer() {
@Override
public void run() {
if (!loadedResources.has(url)) {
fireError(event);
}
}
}.schedule(5 * 1000);
}
}
getHead().appendChild(linkElement);
}
}
CacheElement(GadgetMetadata metadata, String securityToken) {
this.metadata = metadata;
this.securityToken = securityToken;
expirationTime = Duration.currentTimeMillis() + CACHE_EXPIRATION_TIME_MS;
}
boolean expired() {
return Duration.currentTimeMillis() > expirationTime;
}
private double getCurrentTime() {
return GWT.isClient()
? Duration.currentTimeMillis()
: System.currentTimeMillis();
}
/**
* @return true if and only if there is time remaining in the current slice.
*/
private boolean hasTimeLeft() {
return Duration.currentTimeMillis() < timeSliceEnd;
}
/** {@inheritDoc} */
public double getTime() {
return Duration.currentTimeMillis();
}
@Override
public String generateId() {
return Double.toString(Duration.currentTimeMillis());
}
public long start() {
stopTime = null;
startTime = Duration.currentTimeMillis();
return startTime.longValue() * 1000000;
}
public long stop() {
stopTime = Duration.currentTimeMillis();
return stopTime.longValue() * 1000000;
}
private String generateClientNonce(String nonce, String nc) {
return MD5.hash(nc + nonce + Duration.currentTimeMillis() + getRandom());
}
@Override
public double now() {
return Duration.currentTimeMillis();
}
CacheElement(GadgetMetadata metadata, String securityToken) {
this.metadata = metadata;
this.securityToken = securityToken;
expirationTime = Duration.currentTimeMillis() + CACHE_EXPIRATION_TIME_MS;
}
boolean expired() {
return Duration.currentTimeMillis() > expirationTime;
}
/**
* @return true if and only if there is time remaining in the current slice.
*/
private boolean hasTimeLeft() {
return Duration.currentTimeMillis() < timeSliceEnd;
}