// // text_manipulate.bsh // // Sep/02/2015 // // -------------------------------------------------------------- import java.text.DateFormat; import java.text.SimpleDateFormat; // -------------------------------------------------------------- // [2]: Map text_to_dict_proc (fname_in) { String delimit = "[\t]"; dict_aa = text_to_dict_proc_exec (fname_in,delimit); return dict_aa; } // -------------------------------------------------------------- // [2-4]: Map text_to_dict_proc_exec (fname_in,delimit) { Map dict_aa = new HashMap (); FileReader in_file = new FileReader (fname_in); BufferedReader buff = new BufferedReader (in_file); String line; while ((line = buff.readLine ()) != null) { Map dict_unit = new HashMap (); String [] cols = line.split (delimit,-1); dict_unit.put ("name",cols[1]); dict_unit.put ("population",cols[2]); dict_unit.put ("date_mod",cols[3]); dict_aa.put (cols[0],dict_unit); } return dict_aa; } // -------------------------------------------------------------- // [4]: void dict_display_proc (Map dict_aa) { String delimit = "\t"; Set set_aaa = dict_aa.keySet (); Set ss = new TreeSet (set_aaa); for (Object key_aa: ss) { String key = (String)key_aa; if ((!key.equals ("_id")) && (!key.equals ("_rev"))) { Map dict_unit = dict_aa.get (key_aa); String str_out = unit_to_string_proc (delimit,key_aa,dict_unit); System.out.println (str_out); } } } // -------------------------------------------------------------- Map dict_append_proc (dict_aa, String key_aa,String name,int population,String date_mod) { String str_population = Integer.toString (population); Map dict_unit = new HashMap (); dict_unit.put ("name",name); dict_unit.put ("population",str_population); dict_unit.put ("date_mod",date_mod); dict_aa.put (key_aa,dict_unit); return dict_aa; } // -------------------------------------------------------------- // [8]: dict_to_text_proc (file_out,dict_aa) { String delimit = "\t"; dict_to_text_proc_exec (file_out,dict_aa,delimit); } // -------------------------------------------------------------- // [8-4]: dict_to_text_proc_exec (file_out,dict_aa,delimit) { File file = new File (file_out); FileWriter filewriter = new FileWriter (file); BufferedWriter bw = new BufferedWriter (filewriter); PrintWriter pw = new PrintWriter (bw); Set set_aaa = dict_aa.keySet (); for (Object key_aa: set_aaa) { dict_unit = dict_aa.get (key_aa); String str_out = unit_to_string_proc (delimit,key_aa,dict_unit); pw.println (str_out); } pw.close (); } // -------------------------------------------------------------- // [8-4-6]: String unit_to_string_proc (delimit,key_aa,dict_unit) { String str_out = key_aa + delimit; str_out += dict_unit.get ("name") + delimit; str_out += dict_unit.get ("population") + delimit; str_out += dict_unit.get ("date_mod"); return str_out; } // -------------------------------------------------------------- Map dict_update_proc (dict_aa,String key_aa,int population) { String str_population = Integer.toString (population); if (dict_aa.containsKey (key_aa)) { String today = get_current_date_proc (); Map dict_unit = dict_aa.get (key_aa); dict_unit.put ("population",str_population); dict_unit.put ("date_mod",today); dict_aa.put (key_aa,dict_unit); } return dict_aa; } // -------------------------------------------------------------- String get_current_date_proc () { Date ddx = new Date (); DateFormat dfm = new SimpleDateFormat ("yyyy-MM-dd"); String str_ddx = dfm.format (ddx); return str_ddx; } // -------------------------------------------------------------- Map dict_delete_proc (dict_aa,String key_aa) { if (dict_aa.containsKey (key_aa)) { dict_aa.remove (key_aa); } return dict_aa; } // -------------------------------------------------------------- // [10]: Map csv_to_dict_proc (fname_in) { String delimit = "[,]"; dict_aa = text_to_dict_proc_exec (fname_in,delimit); return dict_aa; } // -------------------------------------------------------------- // [12]: dict_to_csv_proc (file_out,dict_aa) { String delimit = ","; dict_to_text_proc_exec (file_out,dict_aa,delimit); } // -------------------------------------------------------------- String file_to_str_proc (String fname_in) { FileReader in_file = new FileReader (fname_in); BufferedReader buff = new BufferedReader (in_file); String line; String str_in = ""; while ((line = buff.readLine ()) != null) { str_in += line; } return str_in; } // -------------------------------------------------------------- file_write_proc (String file_out,String str_out) { File file = new File (file_out); FileWriter filewriter = new FileWriter (file); BufferedWriter bw = new BufferedWriter (filewriter); PrintWriter pw = new PrintWriter (bw); pw.println (str_out); pw.close (); } // --------------------------------------------------------------