001/*
002 * To change this template, choose Tools | Templates and open the template in the editor.
003 */
004
005package armyc2.c2sd.renderer.utilities;
006
007import armyc2.c2sd.renderer.R;
008
009import android.content.Context;
010import android.util.Log;
011
012import java.io.BufferedInputStream;
013import java.io.DataInputStream;
014import java.io.IOException;
015import java.util.HashMap;
016import java.util.Map;
017import java.util.logging.Level;
018
019/**
020 * responsible for character index lookups for single point.
021 * 
022 * @author michael.spinelli
023 *
024 */
025public class SinglePointLookup
026{
027
028    private static boolean _initCalled = false;
029    private static SinglePointLookup _instance;
030    private boolean _ready = false;
031    private static Map<String, SinglePointLookupInfo> hashMapB = null;
032    private static Map<String, SinglePointLookupInfo> hashMapC = null;
033
034    /**
035     *
036     */
037    public boolean getReady()
038    {
039        return this._ready;
040    }
041
042    private SinglePointLookup()
043    {
044
045    }
046
047    /**
048     *
049     */
050    public static synchronized SinglePointLookup getInstance()
051    {
052        if (_instance == null) {
053            _instance = new SinglePointLookup();
054        }
055        return _instance;
056    }
057
058    public synchronized void init(Context context)
059    {
060
061        if (_initCalled == false) {
062            _instance = new SinglePointLookup();
063
064            try {
065                DataInputStream dis = new DataInputStream(new BufferedInputStream(context.getResources().openRawResource(R.raw.singlepoint)));
066                readBinary(dis);
067                dis.close();
068            } catch (IOException e) {
069                Log.e("SinglePointLookup", "Could not load", e);
070            }
071
072            _initCalled = true;
073        }
074    }
075
076    /**
077     * given the milstd symbol code, find the font index for the symbol.
078     * 
079     * @param symbolCode
080     * @return
081     */
082    public int getCharCodeFromSymbol(String symbolCode, int symStd)
083    {
084
085        try {
086            String strSymbolLookup = symbolCode;
087
088            Map<String, SinglePointLookupInfo> hashMap = null;
089
090            if (symStd == RendererSettings.Symbology_2525B)
091                hashMap = hashMapB;
092            else if (symStd == RendererSettings.Symbology_2525C)
093                hashMap = hashMapC;
094
095            SinglePointLookupInfo spli = null;
096            if (SymbolUtilities.isWeather(strSymbolLookup) || symbolCode.contains("FILL")) {
097                spli = hashMap.get(strSymbolLookup);
098                if (spli != null)
099                    return spli.getMappingP();
100                else
101                    return -1;
102
103            } else {
104                if (!hashMap.containsKey(strSymbolLookup))
105                    strSymbolLookup = SymbolUtilities.getBasicSymbolID(strSymbolLookup);
106
107                spli = hashMap.get(strSymbolLookup);
108                if (spli != null) {
109                    if (SymbolUtilities.getStatus(symbolCode).equals("A") == true)
110                        return spli.getMappingA();
111                    else
112                        return spli.getMappingP();
113                } else {
114                    return -1;
115                }
116            }
117        } catch (Exception exc) {
118            ErrorLogger.LogException("SinglePointLookup", "getCharCodeFromSymbol", exc, Level.WARNING);
119        }
120        return -1;
121
122    }
123
124    /**
125     * Method that retrieves a reference to a SPSymbolDef object from the SinglePointLookup Dictionary.
126     *
127     * @param strSymbolID - IN - The 15 character symbol Id.
128     * @return SPSymbolDef, or null if there was an error.
129     */
130    /*
131     * public SPSymbolDef getSPSymbolDef(String strSymbolID) { String strGenericSymbolID = "";
132     * if(strSymbolID.substring(0, 1).equals("G")) { strGenericSymbolID = strSymbolID.substring(0, 1) + "*" +
133     * strSymbolID.substring(2, 12); } else { strGenericSymbolID = strSymbolID.substring(0, 10); }
134     * 
135     * SPSymbolDef data = hashMap.get(strGenericSymbolID); return data; }
136     */
137
138    /**
139     * Method that retrieves a reference to a SinglePointLookupInfo object from the SinglePointLookup
140     * Dictionary.
141     * 
142     * @param basicSymbolID
143     * @return
144     */
145    public SinglePointLookupInfo getSPLookupInfo(String basicSymbolID, int symStd)
146    {
147        SinglePointLookupInfo spli = null;
148        if (symStd == RendererSettings.Symbology_2525B)
149            spli = hashMapB.get(basicSymbolID);
150        else if (symStd == RendererSettings.Symbology_2525C)
151            spli = hashMapC.get(basicSymbolID);
152        return spli;
153    }
154
155    /*
156     * public static void main(String[] args) { SPSymbolDef data =
157     * SinglePointLookup.instance().getSPSymbolDef("G*FPPTC---****X"); int mapping =
158     * SinglePointLookup.instance().getCharCodeFromSymbol("G*FPPTC---****X"); String junk = ""; }
159     */
160
161    private void readBinary(DataInputStream dis) throws IOException
162    {
163        int count = dis.readInt();
164        hashMapB = new HashMap<>(count);
165
166        for (int i = 0; i < count; i++) {
167            SinglePointLookupInfo def = SinglePointLookupInfo.readBinary(dis);
168            hashMapB.put(def.getBasicSymbolID(), def);
169        }
170
171        count = dis.readInt();
172        hashMapC = new HashMap<>(count);
173        for (int i = 0; i < count; i++) {
174            SinglePointLookupInfo def = SinglePointLookupInfo.readBinary(dis);
175            hashMapC.put(def.getBasicSymbolID(), def);
176        }
177    }
178}