Java tutorial
/* * This file is part of the InfinityCubed Launcher source code. * Copyright (C) 2014 InfinityCubed Team. * * 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 net.chris54721.infinitycubed.workers; import com.google.common.base.Charsets; import com.google.common.io.Files; import com.google.gson.reflect.TypeToken; import net.chris54721.infinitycubed.Launcher; import net.chris54721.infinitycubed.data.Account; import net.chris54721.infinitycubed.utils.LogHelper; import net.chris54721.infinitycubed.utils.Reference; import net.chris54721.infinitycubed.utils.Resources; import org.apache.commons.io.FileUtils; import java.io.File; import java.lang.reflect.Type; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; public class AccountLoader implements Runnable { @Override public void run() { try { LogHelper.info("Loading saved accounts"); Collection<File> accountJsons = FileUtils.listFiles(Resources.getFolder(Reference.ACCOUNTS_FOLDER), new String[] { "json" }, false); Type stringMap = new TypeToken<LinkedHashMap<String, String>>() { }.getType(); for (File accountJson : accountJsons) { Map<String, String> data = Reference.DEFAULT_GSON .fromJson(Files.toString(accountJson, Charsets.UTF_8), stringMap); if (data == null) throw new NullPointerException( "Content of file " + accountJson.getName() + " deserializes to null"); Account account = new Account(data.get("uuid"), data.get("username"), data.get("nickname"), data.get("accessToken"), data.get("clientToken")); boolean selected = Launcher.getSettings().getProperty("selectedAccount") .equalsIgnoreCase(data.get("uuid")); account.fetchSkin(selected); Launcher.accounts.add(account); if (selected) selectAccount(account); } } catch (Exception e) { LogHelper.fatal("Failed loading accounts", e); } } public static void selectAccount(Account account) { Launcher.selectedAccount = account; Launcher.getSettings().setProperty("selectedAccount", account.getUUID().toString()); } }