/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.teamdave.schwimmer.util;

import ca.teamdave.schwimmer.control.HighStaticPWD;
import com.sun.squawk.util.LineReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.microedition.io.Connector;

/**
 *
 * @author leighpauls
 */
public class Const {
    private final Hashtable mValueTable;
    private static Const sInstance = null;
    
    private Const() {
        mValueTable = new Hashtable();
    }
    
    public static Const getInstance() {
        if (sInstance == null) {
            sInstance = new Const();
        }
        return sInstance;
    }
    
    public void reloadConstants() {
        mValueTable.clear();
        InputStream is = null;
        
        try {
            is = Connector.openInputStream("file:///dave_consts.conf");
            LineReader r = new LineReader(new InputStreamReader(is));
            readFile(r);
            
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();           
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private void readFile(LineReader r) throws IOException {
        String curLine;
        int curLineNum = 0;
        
        while ((curLine = r.readLine()) != null) {
            int curIdx = 0;
            while (curIdx < curLine.length()) {
                // discard whitespace
                char curChar = curLine.charAt(curIdx);
                if (curChar == ' ' 
                        || curChar == '\t'
                        || curChar == '\n'
                        || curChar == '\r') {
                    curLine = curLine.substring(0, curIdx) + curLine.substring(curIdx+1);
                }
            }
            // don't care about comments
            if (curLine.startsWith("#") || curLine.length() == 0) {
                continue;
            }
            int equalBreak = curLine.indexOf("=");
            if (equalBreak < 0) {
                System.err.print("Bad line format at line " + curLineNum);
                continue;
            }
            String name = curLine.substring(0, equalBreak);
            String value = curLine.substring(equalBreak+1);
            mValueTable.put(name, value);
            
            curLineNum++;
        }
    }
    
    public int getInt(String name, int default_val) {
        String value = (String)mValueTable.get(name);
        if (value == null) {
            System.err.println("No Value for " + name);
            return default_val;
        }
        int res;
        try {
            res = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            System.err.println("Invalid int at " + name);
            return default_val;
        }
        if (res != default_val) {
            System.err.println(name + ": default does not match real value");
        }
        return res;
    }
    
    public double getDouble(String name, double default_val) {
        String value = (String)mValueTable.get(name);
        if (value == null) {
            System.err.println("No Value for " + name);
            return default_val;
        }
        double res;
        try {
            res = Double.parseDouble(value);
        } catch (NumberFormatException e) {
            System.err.println("Invalid int at " + name);
            return default_val;
        }
        if (res != default_val) {
            System.err.println(name + ": default does not match real value");
        }
        return res;
    }
    
    public HighStaticPWD pwdFromConst(String namePrefx, double defaultP,
            double defaultW, double defaultD, double defaultError) {
        return new HighStaticPWD(
                getDouble(namePrefx + "_p", defaultP),
                getDouble(namePrefx + "_w", defaultW),
                getDouble(namePrefx + "_d", defaultD),
                getDouble(namePrefx + "_error", defaultError));
    }
}

@@ -303,32 +303,62 @@
 utStreamReader;%0A
+import java.util.Enumeration;%0A
 import java.util
@@ -505,16 +505,61 @@
 eTable;%0A
+    private final Hashtable mEffectiveTable;%0A
     priv
@@ -655,24 +655,67 @@
 ashtable();%0A
+        mEffectiveTable = new Hashtable();%0A
     %7D%0A    %0A 
@@ -2689,61 +2689,863 @@
   %7D%0A
-    %0A    public int getInt(String name, int default_v
+%0A    /**%0A     * Print the equivalent constants file to get the same functionality of the %0A     * current Const object state%0A     */%0A    public void dumpEffectiveTable() %7B%0A        Enumeration keys = mEffectiveTable.keys();%0A        while (keys.hasMoreElements()) %7B%0A            String key = (String)keys.nextElement();%0A            System.out.println(key + %22 = %22 + mEffectiveTable.get(key));%0A        %7D%0A    %7D%0A    %0A    public int getInt(String name, int defaultVal) %7B%0A        int val = getIntImpl(name, defaultVal);%0A        mEffectiveTable.put(name, Integer.toString(val));%0A        return val;%0A    %7D%0A    %0A    public double getDouble(String name, double defaultVal) %7B%0A        double val = getDoubleImpl(name, defaultVal);%0A        mEffectiveTable.put(name, Double.toString(val));%0A        return val;%0A    %7D%0A    %0A    private int getIntImpl(String name, int defaultV
 al) 
@@ -3703,34 +3703,33 @@
   return default
-_v
+V
 al;%0A        %7D%0A  
@@ -3920,34 +3920,33 @@
   return default
-_v
+V
 al;%0A        %7D%0A  
@@ -3961,34 +3961,33 @@
  (res != default
-_v
+V
 al) %7B%0A          
@@ -4092,37 +4092,38 @@
     %7D%0A    %0A    p
-ublic
+rivate
  double getDoubl
@@ -4115,32 +4115,36 @@
 double getDouble
+Impl
 (String name, do
@@ -4147,34 +4147,33 @@
 , double default
-_v
+V
 al) %7B%0A        St
@@ -4319,34 +4319,33 @@
   return default
-_v
+V
 al;%0A        %7D%0A  
@@ -4549,18 +4549,17 @@
  default
-_v
+V
 al;%0A    
@@ -4594,10 +4594,9 @@
 ault
-_v
+V
 al) 

Add effective table functionality