001package armyc2.c5isr.renderer.utilities;
002
003import android.content.Context;
004
005import java.io.BufferedReader;
006import java.io.IOException;
007import java.io.InputStream;
008import java.io.InputStreamReader;
009import java.util.ArrayList;
010import java.util.HashMap;
011import java.util.List;
012import java.util.Map;
013
014import armyc2.c5isr.renderer.R;
015
016/**
017 * Utility class that takes the 3 digit country code from the symbol ID and returns the 3 character string representation
018 * of that country. For example, 840 turns into "USA" for the United States.
019 */
020public class GENCLookup
021{
022    private static GENCLookup _instance = null;
023    private static Boolean _initCalled = false;
024
025    private static Map<Integer, String> _GENCLookup = null;
026    private String TAG = "GENCLookup";
027    private List<String> _IDList = new ArrayList<String>();
028
029    private GENCLookup() {
030        // init(null);
031        // _initCalled=true;
032    }
033
034    public static synchronized GENCLookup getInstance() {
035        if (_instance == null) {
036            _instance = new GENCLookup();
037        }
038
039        return _instance;
040    }
041
042    public final synchronized void init(Context context)
043    {
044        if (_initCalled == false)
045        {
046            _GENCLookup = new HashMap<>();
047            String[] temp = null;
048            String delimiter = "\t";
049
050            try {
051                InputStream is = context.getResources().openRawResource(R.raw.genc);
052
053                BufferedReader br = new BufferedReader(new InputStreamReader(is));
054
055
056                String line = br.readLine();
057                while (line != null) {
058                    //parse first line
059                    temp = line.split(delimiter);
060
061                    if(temp != null && temp.length >= 2 && SymbolUtilities.isNumber(temp[1]))
062                        _GENCLookup.put(Integer.valueOf(temp[1]),temp[0]);
063
064                    //read next line for next loop
065                    line = br.readLine();
066                }
067
068                _initCalled = true;
069            } catch (IOException ioe) {
070                System.out.println(ioe.getMessage());
071            } catch (Exception e) {
072                System.out.println(e.getMessage());
073            }
074        }
075    }
076
077    public String get3CharCode(int id)
078    {
079        if(_GENCLookup != null && _GENCLookup.containsKey(id))
080        {
081            return _GENCLookup.get(id);
082        }
083        return "";
084    }
085}