Skip to content

Instantly share code, notes, and snippets.

@luigieai
Created June 5, 2016 21:39
Show Gist options
  • Select an option

  • Save luigieai/e7c258f5adfa085c8b68d699b6a740bf to your computer and use it in GitHub Desktop.

Select an option

Save luigieai/e7c258f5adfa085c8b68d699b6a740bf to your computer and use it in GitHub Desktop.
GenericCooldown
import java.util.concurrent.TimeUnit;
/**
*
* @author @LuigiOliveira__
* @version 0.5
* Classe que armaneza uma Chave com um valor adaptada para cooldown!
*
* @param <K> - Um valor chave
* @param <V> - Um valor complementar
*/
@SuppressWarnings("serial")
public class GenericCooldown<K,V> {
private long expiration;
private boolean isExpirated;
private K key;
private V value;
public GenericCooldown(K key,V value, int seconds){
this.key = key;
this.value = value;
this.isExpirated = false;
this.expiration = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(seconds);
}
/**
*
* @return Key do objeto
*/
public K getKey(){
return key;
}
/**
*
* @return Value do objeto
*/
public V getValue(){
return value;
}
/**
* Checa se este cooldown já esta expirado ou não
* @return Se esta expirado
*/
public boolean isExpirated(){
if(expiration < System.currentTimeMillis()){
isExpirated = true;
}
return isExpirated;
}
/**
* Calcula o tempo restante (em millis)
* @return O tempo restante em millis
*/
public long getTimeLeft(){
if(isExpirated()){
return 0L;
}else{
return expiration - System.currentTimeMillis();
}
}
/**
* Adiciona uma quantidade de segundos a expiração do cooldown, porém se ele estiver expirado não ira fazer muita diferença!
* @param seconds
*/
public void addTime(int seconds){
this.expiration += TimeUnit.SECONDS.toMillis(seconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment