Advertisement
johnnic431

ConfigReader

Jul 9th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package com.sylvyrfysh.terminusengine.config;
  2.  
  3. import com.sylvyrfysh.terminusengine.config.ConfigObject;
  4. import java.io.*;
  5. import java.io.concurrent.ConcurrentHashMap;
  6.  
  7. public class ConfigReader{
  8.     public enum Errors{
  9.         NO_ERROR,FILE_NOT_CREATED,IO_ERROR,GENERAL_EXCEPTION;
  10.     }
  11.     private static Errors errState=null;
  12.     private static Exception e;
  13.     private static ConcurrentHashMap<String,ConfigObject> coList;
  14.     private static boolean init=false;
  15.     public static boolean readConfig(String configLocation,int approximatedConfigSize){
  16.         try(ObjectInputStream ois=new ObjectInputStream(new FileInputStream(configLocation))){
  17.             coList=new ConcurrentHashMap<String,ConfigObject>(approximatedConfigSize);
  18.             ConfigObject co;
  19.             while((co=(ConfigObject)ois.readObject())!=null){
  20.                 coList.put(co.getKey(),co);
  21.             }
  22.             init=true;
  23.         }catch(FileNotFoundException fnfe){
  24.             this.e=ioe;
  25.             errState=Errors.FILE_NOT_CREATED;
  26.             return false;
  27.         }catch(IOException ioe){
  28.             this.e=ioe;
  29.             errState=Errors.IO_ERROR;
  30.             return false;
  31.         }catch(EOFException eofe){
  32.             init=true;
  33.             return true;
  34.         }catch(Exception ee){
  35.             this.e=ee;
  36.             init=false;
  37.             return false;
  38.         }
  39.         init=true;
  40.         return true;
  41.     }
  42.    
  43.     public static Errors getError(){
  44.         return this.errState;
  45.         this.errState=null;
  46.     }
  47.     public static Exception getException(){
  48.         return this.e;
  49.     }
  50.     public static ConfigObject getObject(String key){
  51.         if(!init){return null;}else{
  52.             return co.get(key);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement