package com.xrtb.tools;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.xrtb.bidder.Controller;
import com.xrtb.common.Configuration;

public class LookingGlass {

	public static Map<String, Object> symbols = new HashMap();
	
	Map myMap = new HashMap();
	
	public LookingGlass() {
		
	}
	
	public LookingGlass(String name, String file) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(file));

		String[] parts = null;	
		String message = "Initialize Simple Map: " + file + " as " + name;
		for (String line; (line = br.readLine()) != null;) {
			parts = eatquotedStrings(line);
			for (int i=0;i<parts.length;i++) {
				parts[i] = parts[i].replaceAll("\"","");
			}
			myMap.put(parts[0], parts);
		}
		
		symbols.put(name, this);
		System.out.format("[%s] - %d - %s - %s - %s\n",Controller.sdf.format(new Date()), 1, Configuration.instanceName, this.getClass().getName(),message);
	}
	
	public Object query(Object key) {
		return myMap.get(key);
	}
	
	/**
	 * Read comma separated items, except, ignore all comments in double quoted strings.
	 * @param line String. The line to parse.
	 * @return String[]. The tokens parsed from the line.
	 */
	public static String[] eatquotedStrings(String line) {

		String otherThanQuote = " [^\"] ";
		String quotedString = String.format(" \" %s* \" ", otherThanQuote);
		String regex = String.format(
				"(?x) " + // enable comments, ignore white spaces
						",                         " + // match a comma
						"(?=                       " + // start positive look
														// ahead
						"  (?:                     " + // start non-capturing
														// group 1
						"    %s*                   " + // match 'otherThanQuote'
														// zero or more times
						"    %s                    " + // match 'quotedString'
						"  )*                      " + // end group 1 and repeat
														// it zero or more times
						"  %s*                     " + // match 'otherThanQuote'
						"  $                       " + // match the end of the
														// string
						")                         ", // stop positive look
														// ahead
				otherThanQuote, quotedString, otherThanQuote);

        //for (String t : tokens) {
		//	System.out.println("> " + t);
		// }
		return line.split(regex, -1);
	}
}

@@ -146,16 +146,63 @@
 til.Map;
+%0Aimport java.util.concurrent.ConcurrentHashMap;
 %0A%0Aimport
@@ -298,16 +298,64 @@
 lass %7B%0A%0A
+%09// The symbol table used throughout the bidder%0A
 %09public 
@@ -361,16 +361,25 @@
  static 
+volatile 
 Map%3CStri
@@ -404,28 +404,65 @@
 s = new 
-HashMap();%0A%09
+ConcurrentHashMap%3CString, Object%3E();%0A%09%0A%09// My map
 %0A%09Map my
@@ -471,16 +471,26 @@
 p = new 
+Concurrent
 HashMap(
@@ -499,39 +499,336 @@
 %0A%09%0A%09
-public LookingGlass() %7B%0A%09%09%0A%09%7D%0A%09
+/**%0A%09 * Default constructor%0A%09 */%0A%09public LookingGlass() %7B%0A%09%09%0A%09%7D%0A%09%0A%09/**%0A%09 * A Class that implements a map from a 2 element comma separated list.%0A%09 * @param name String. The symbol name this object is known by in the bidder.%0A%09 * @param file String. The filename of the csv data file.%0A%09 * @throws Exception on File I/O errors.%0A%09 */
 %0A%09pu
@@ -1449,16 +1449,189 @@
 );%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Return the value stored at key.%0A%09 * @param key Object. The key to use in the lookup.%0A%09 * @return Object. Returns the value at key, or null if not in the list.%0A%09 */%0A
 %09public 

Abort on aerospike error