Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package com.ownxile.util.web.vote;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.util.ArrayList;
  10.  
  11. /**
  12.  * @author Robbie
  13.  *
  14.  */
  15. public class VoteCache {
  16.  
  17.     private final static int HOUR_IN_MILLIS = 3600000;
  18.     private final static int HOURS_PER_CLAIM = 12;
  19.  
  20.     private static ArrayList<CachedVoteClaim> cachedVotes = new ArrayList<CachedVoteClaim>();
  21.  
  22.     @SuppressWarnings("unused")
  23.     private static final File VOTE_FILE = new File(
  24.             "./etc/data/vote/vote_cache.dat");
  25.  
  26.     public static void addClaimToCache(String playerName) {
  27.         cachedVotes.add(new CachedVoteClaim(playerName));
  28.     }
  29.  
  30.     public static boolean canVote(String userName) {
  31.         for (CachedVoteClaim cached : cachedVotes) {
  32.             if (cached.getUserName() == userName
  33.                     && System.currentTimeMillis() - cached.getVoteTimeMillis() < HOUR_IN_MILLIS
  34.                             * HOURS_PER_CLAIM) {
  35.                 return false;
  36.             }
  37.         }
  38.         return true;
  39.     }
  40.  
  41.     public static void purgeCache() {
  42.         int i = 0;
  43.         for (CachedVoteClaim cached : cachedVotes) {
  44.             if (System.currentTimeMillis() - cached.getVoteTimeMillis() < HOUR_IN_MILLIS
  45.                     * HOURS_PER_CLAIM) {
  46.                 cachedVotes.remove(cached);
  47.                 i++;
  48.             }
  49.         }
  50.         if (i > 0)
  51.             System.out.println("Cleared " + i + " cached votes.");
  52.     }
  53.  
  54.     @SuppressWarnings("unchecked")
  55.     public static void loadCache(File file) {
  56.         try {
  57.             FileInputStream fileIn = new FileInputStream(file);
  58.             ObjectInputStream in = new ObjectInputStream(fileIn);
  59.             try {
  60.                 cachedVotes = (ArrayList<CachedVoteClaim>) in.readObject();
  61.             } catch (ClassNotFoundException e) {
  62.                 e.printStackTrace();
  63.             }
  64.             in.close();
  65.             fileIn.close();
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.     }
  70.  
  71.     public static void saveCache(ArrayList<CachedVoteClaim> voteCache, File file) {
  72.         try {
  73.             FileOutputStream fileOut = new FileOutputStream(file);
  74.             ObjectOutputStream out = new ObjectOutputStream(fileOut);
  75.             out.writeObject(voteCache);
  76.             out.close();
  77.             fileOut.close();
  78.         } catch (IOException i) {
  79.             i.printStackTrace();
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement