Java tutorial
/* * Copyright 2010 Nikita Kuklev. * * This file is part of LPServer2 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ca.uviccscu.lp.persistence; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; /** * * @author Nikita Kuklev <nikkuk@hotmail.com> * @since 2010-11-10 14:27:42 -0800 */ public class GamelistStorage { static Logger l = Logger.getLogger(GamelistStorage.class.getName()); public static Map stringToGameMap = Collections.synchronizedSortedMap(new TreeMap<String, Game>()); public static int lastFileNumber; public static long lastCRC; public static long lastSize; public GamelistStorage() { } public static synchronized void addGame(Game g) { stringToGameMap.put(g.gameName, g); } public static synchronized void removeGame(String name) { stringToGameMap.remove(name); } public static synchronized Game getGame(String name) { return (Game) stringToGameMap.get(name); } public static synchronized List getAllGames() { List ll = new LinkedList(); Collection c = stringToGameMap.values(); ll.addAll(c); return ll; } public static synchronized Map getMapImplementation() { return stringToGameMap; } public static synchronized void getGamesFromSS() { List ll = SettingsManager.getStorage().getGames(); for (int i = 0; i < ll.size(); i++) { addGame((Game) ll.get(i)); } } public static synchronized boolean checkGameChanged(Game g) throws IOException { l.trace("Checking if game same: " + g.gameName); File f = new File(g.gameAbsoluteFolderPath); lastFileNumber = FileUtils.listFiles(f, null, true).size(); boolean a = (g.gameFileNumber == lastFileNumber); lastCRC = FileUtils.checksumCRC32(new File(g.gameAbsoluteExecPath)); boolean b = (g.gameExecCRC32 == lastCRC); lastSize = FileUtils.sizeOfDirectory(f); boolean c = (g.gameSize == lastSize); l.trace("File number same: " + a + "| Exec CRC32 same: " + b + "| Size same: " + c); return !(a && b && c); } public static synchronized int verifyConfig(Object[] vals, boolean edit) { boolean a = ((String) vals[0] != null) && !((String) vals[0]).equals(""); if (!edit) { a = a && (getGame((String) vals[0]) == null); } File fl = new File((String) vals[1]); boolean b = fl.canRead() && fl.isDirectory() && fl.isAbsolute(); fl = new File((String) vals[2]); boolean c = fl.canRead() && fl.isFile() && fl.isAbsolute(); boolean d = true; //boolean d = ((String) vals[3]).([]()/\+{}?*+.^$); boolean e = true; String ss1 = ((String) vals[4]); /* String[] sa1 = ((String) vals[4]).split("\\r?\\n"); for (String s : sa1) { * */ String[] ss1split = ss1.split("\\r?\\n"); for (String sss : ss1split) { if (!sss.equals("")) { if (!ss1.endsWith(".bat")) { e = false; l.trace("File location \"" + ss1 + "\" doesnt end with .bat"); } File f = new File(sss); if (!f.exists()) { e = false; l.trace("File location \"" + ss1 + "\" doesnt exist"); } } } if (a && b && c && d && e) { return -1; } else { if (a != true) { return 0; } else if (b != true) { return 1; } else if (c != true) { return 2; } else if (d != true) { return 3; } else if (e != true) { return 4; } else { return -1; } } } public static synchronized String gameStatusToString(int s) { switch (s) { case (-1): { return "Unknown"; } case (0): { return "Rdy to hash"; } case (1): { return "Hashing"; } case (2): { return "Hashed"; } case (3): { return "Invalid"; } case (4): { return "Invalid"; } case (5): { return "Error - see log"; } default: { return "Unknown"; } } } }