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 static Map<String, String> _GENCLookupAlpha = null;
027    private String TAG = "GENCLookup";
028    private List<String> _IDList = new ArrayList<String>();
029
030    private GENCLookup() {
031        // init(null);
032        // _initCalled=true;
033    }
034
035    public static synchronized GENCLookup getInstance() {
036        if (_instance == null) {
037            _instance = new GENCLookup();
038        }
039
040        return _instance;
041    }
042
043    public final synchronized void init(Context context)
044    {
045        if (_initCalled == false)
046        {
047            _GENCLookup = new HashMap<>();
048            _GENCLookupAlpha = new HashMap<>();
049            String[] temp = null;
050            String delimiter = "\t";
051
052            try {
053                InputStream is = context.getResources().openRawResource(R.raw.genc);
054
055                BufferedReader br = new BufferedReader(new InputStreamReader(is));
056
057
058                String line = br.readLine();
059                while (line != null) {
060                    //parse first line
061                    temp = line.split(delimiter);
062
063                    if(temp.length >= 3) {
064                        _GENCLookup.put(Integer.valueOf(temp[2]), temp[1]);
065                        if(temp[0].length()==2)
066                            _GENCLookupAlpha.put((temp[0]), temp[2]);
067                    }
068
069                    //read next line for next loop
070                    line = br.readLine();
071                }
072
073                _initCalled = true;
074            } catch (IOException ioe) {
075                System.out.println(ioe.getMessage());
076            } catch (Exception e) {
077                System.out.println(e.getMessage());
078            }
079        }
080    }
081
082    /**
083     *
084     * @param id 3 digit code from 2525D+ symbol code
085     * @return
086     */
087    public String get3CharCode(int id)
088    {
089        if(_GENCLookup != null && _GENCLookup.containsKey(id))
090        {
091            return _GENCLookup.get(id);
092        }
093        return "";
094    }
095
096    /**
097     *
098     * @param id 2 char string from 2525C symbol code
099     * @return
100     */
101    public String get3DigitCode(String id)
102    {
103        if(_GENCLookupAlpha != null && _GENCLookupAlpha.containsKey(id))
104        {
105            String code = _GENCLookupAlpha.get(id);
106            while(code.length()<3)
107                code = "0" + code;
108            return code;
109        }
110        return "000";
111    }
112}