Created
June 5, 2016 21:39
-
-
Save luigieai/e7c258f5adfa085c8b68d699b6a740bf to your computer and use it in GitHub Desktop.
GenericCooldown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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