Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sylvyrfysh.terminusengine.config;
- import com.sylvyrfysh.terminusengine.config.ConfigObject;
- import java.io.*;
- import java.io.concurrent.ConcurrentHashMap;
- public class ConfigReader{
- public enum Errors{
- NO_ERROR,FILE_NOT_CREATED,IO_ERROR,GENERAL_EXCEPTION;
- }
- private static Errors errState=null;
- private static Exception e;
- private static ConcurrentHashMap<String,ConfigObject> coList;
- private static boolean init=false;
- public static boolean readConfig(String configLocation,int approximatedConfigSize){
- try(ObjectInputStream ois=new ObjectInputStream(new FileInputStream(configLocation))){
- coList=new ConcurrentHashMap<String,ConfigObject>(approximatedConfigSize);
- ConfigObject co;
- while((co=(ConfigObject)ois.readObject())!=null){
- coList.put(co.getKey(),co);
- }
- init=true;
- }catch(FileNotFoundException fnfe){
- this.e=ioe;
- errState=Errors.FILE_NOT_CREATED;
- return false;
- }catch(IOException ioe){
- this.e=ioe;
- errState=Errors.IO_ERROR;
- return false;
- }catch(EOFException eofe){
- init=true;
- return true;
- }catch(Exception ee){
- this.e=ee;
- init=false;
- return false;
- }
- init=true;
- return true;
- }
- public static Errors getError(){
- return this.errState;
- this.errState=null;
- }
- public static Exception getException(){
- return this.e;
- }
- public static ConfigObject getObject(String key){
- if(!init){return null;}else{
- return co.get(key);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement