Skip to content

Instantly share code, notes, and snippets.

@Sythelux
Last active August 29, 2015 14:25
Show Gist options
  • Save Sythelux/58e90a7277160d8a54d5 to your computer and use it in GitHub Desktop.
Save Sythelux/58e90a7277160d8a54d5 to your computer and use it in GitHub Desktop.
A small Class that helps with Timing Stuff
/*
*Copyright (c) 2015, Sythelux Rikd All rights reserved.
*
*Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* This Class helps with many things, for example: you have a Timer that runs aith 50ms delay, but you only want a task in that timer to by at rate of 1000 and don't want to at more Timers.
* <p/>
* Created on 27.04.15.
*
* @author Sythelux Rikd
*/
public class MinimumTimeTicker {
private long timeOut;
private long lastReset;
/**
* You should give a Timeout, it would be silly to use it with <=0 of course you can do that
*
* @param timeOut should be >=0
*/
public MinimumTimeTicker(long timeOut) {
this.timeOut = timeOut;
}
/**
* checks if a call is allowed the calculation is as follows: current Time - last Reset > time Out
*
* @return true, if you can continue, false if not.
*/
public boolean isAllowed() {
return System.currentTimeMillis() - lastReset > timeOut;
}
/**
* resets the last Reset, so between the call and next next timeOut {@link #isAllowed} is true
*/
public void reset() {
lastReset = System.currentTimeMillis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment