Example usage for android.util SparseArray get

List of usage examples for android.util SparseArray get

Introduction

In this page you can find the example usage for android.util SparseArray get.

Prototype

@SuppressWarnings("unchecked")
public E get(int key, E valueIfKeyNotFound) 

Source Link

Document

Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.

Usage

From source file:com.dgsd.android.ShiftTracker.Adapter.WeekAdapter.java

@Override
public Cursor swapCursor(Cursor cursor) {
    clearCaches();// w  w w  .j  a  va 2 s.  co  m

    if (cursor == null)
        return super.swapCursor(null);

    MatrixCursor mc = new MatrixCursor(DbTable.SHIFTS.getFieldNames(), cursor.getCount() + 7);

    int jd = mStartingJulianDay;
    final int colCount = cursor.getColumnCount();

    SparseArray<List<Object[]>> jdToRowArray = new SparseArray<List<Object[]>>();

    if (cursor.moveToFirst()) {
        final int jdIndex = cursor.getColumnIndex(DbField.JULIAN_DAY.name);
        do {
            Object[] row = new Object[colCount];
            for (int i = 0; i < colCount; i++)
                row[i] = cursor.getString(i);

            final int shiftDay = cursor.getInt(jdIndex);

            List<Object[]> rowsOnSameJd = jdToRowArray.get(shiftDay, null);
            if (rowsOnSameJd == null)
                rowsOnSameJd = new ArrayList<Object[]>();

            rowsOnSameJd.add(row);

            jdToRowArray.put(shiftDay, rowsOnSameJd);
        } while (cursor.moveToNext());
    }

    for (int i = jd; i < jd + 7; i++) {
        List<Object[]> rows = jdToRowArray.get(i);
        if (rows != null)
            for (Object[] row : rows)
                mc.addRow(row);

        //Add a 'Add Shift' row
        Object[] row = new Object[colCount];
        row[0] = mRand.nextInt(Integer.MAX_VALUE); // DbField.ID
        row[1] = i; // DbField.JULIAN_DAY
        row[2] = i; // DbField.END_JULIAN_DAY
        row[3] = -1; // DbField.START_TIME
        row[4] = -1; // DbField.END_TIME
        row[5] = -1; // DbField.PAY_RATE
        row[6] = NEW_ROW_KEY; // DbField.NAME
        row[7] = null; // DbField.NOTE
        row[8] = -1; // DbField.BREAK_DURATION
        row[9] = 0; // DbField.IS_TEMPLATE
        row[10] = -1; // DbField.REMINDER

        mc.addRow(row);
    }

    return super.swapCursor(mc);
}

From source file:fr.cph.chicago.core.fragment.NearbyFragment.java

private void loadAroundBusArrivals(@NonNull final BusStop busStop,
        @NonNull final SparseArray<Map<String, List<BusArrival>>> busArrivalsMap) {
    try {/* www . j ava 2 s  .c  om*/
        if (isAdded()) {
            final CtaConnect cta = CtaConnect.getInstance(getContext());
            int busStopId = busStop.getId();
            // Create
            final Map<String, List<BusArrival>> tempMap = busArrivalsMap.get(busStopId,
                    new ConcurrentHashMap<>());
            if (!tempMap.containsKey(Integer.toString(busStopId))) {
                busArrivalsMap.put(busStopId, tempMap);
            }

            final MultiValuedMap<String, String> reqParams = new ArrayListValuedHashMap<>(1, 1);
            reqParams.put(requestStopId, Integer.toString(busStopId));
            final InputStream is = cta.connect(BUS_ARRIVALS, reqParams);
            final XmlParser xml = XmlParser.getInstance();
            final List<BusArrival> busArrivals = xml.parseBusArrivals(is);
            for (final BusArrival busArrival : busArrivals) {
                final String direction = busArrival.getRouteDirection();
                if (tempMap.containsKey(direction)) {
                    final List<BusArrival> temp = tempMap.get(direction);
                    temp.add(busArrival);
                } else {
                    final List<BusArrival> temp = new ArrayList<>();
                    temp.add(busArrival);
                    tempMap.put(direction, temp);
                }
            }
            trackWithGoogleAnalytics(activity, R.string.analytics_category_req,
                    R.string.analytics_action_get_bus, BUSES_ARRIVAL_URL, 0);
        }
    } catch (final Throwable throwable) {
        throw Exceptions.propagate(throwable);
    }
}

From source file:fr.cph.chicago.core.fragment.NearbyFragment.java

private void hideStationsAndStopsIfNeeded(@NonNull final List<BusStop> busStops,
        @NonNull final SparseArray<Map<String, List<BusArrival>>> busArrivalsMap,
        @NonNull final List<Station> trainStations, @NonNull final SparseArray<TrainArrival> trainArrivals) {
    if (hideStationsStops && isAdded()) {
        final List<BusStop> busStopTmp = new ArrayList<>();
        for (final BusStop busStop : busStops) {
            if (busArrivalsMap.get(busStop.getId(), new ConcurrentHashMap<>()).size() == 0) {
                busArrivalsMap.remove(busStop.getId());
            } else {
                busStopTmp.add(busStop);
            }//from w w  w . j av  a 2  s .c o  m
        }
        busStops.clear();
        busStops.addAll(busStopTmp);

        final List<Station> trainStationTmp = new ArrayList<>();
        for (final Station station : trainStations) {
            if (trainArrivals.get(station.getId()) == null
                    || trainArrivals.get(station.getId()).getEtas().size() == 0) {
                trainArrivals.remove(station.getId());
            } else {
                trainStationTmp.add(station);
            }
        }
        trainStations.clear();
        trainStations.addAll(trainStationTmp);
    }
}

From source file:org.indywidualni.centrumfm.util.ChangeLog.java

/**
 * Returns the merged change log.//from w ww  . j  av  a2  s .c o  m
 *
 * @param full If this is {@code true} the full change log is returned. Otherwise only changes for
 *             versions newer than the last version are returned.
 * @return A sorted {@code List} containing {@link ReleaseItem}s representing the (partial)
 * change log.
 * @see #getChangeLogComparator()
 */
public List<ReleaseItem> getChangeLog(boolean full) {
    SparseArray<ReleaseItem> masterChangelog = getMasterChangeLog(full);
    SparseArray<ReleaseItem> changelog = getLocalizedChangeLog(full);

    List<ReleaseItem> mergedChangeLog = new ArrayList<>(masterChangelog.size());

    for (int i = 0, len = masterChangelog.size(); i < len; i++) {
        int key = masterChangelog.keyAt(i);

        // Use release information from localized change log and fall back to the master file
        // if necessary.
        ReleaseItem release = changelog.get(key, masterChangelog.get(key));

        mergedChangeLog.add(release);
    }

    Collections.sort(mergedChangeLog, getChangeLogComparator());

    return mergedChangeLog;
}

From source file:fr.cph.chicago.xml.Xml.java

public final List<Eta> parseTrainsFollow(final String xml, final TrainData data) throws ParserException {
    InputStream is = new ByteArrayInputStream(xml.getBytes());
    SparseArray<TrainArrival> arrivals = null;
    try {/*from  w  w w . j  ava2s. c  o m*/
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        XmlArrivalTrainTag tag = null;
        Date tmst = null;
        Integer errCd = null;
        String errNum = null;
        String tagName = null;
        Integer staId = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_DOCUMENT) {
                arrivals = new SparseArray<TrainArrival>();
            } else if (eventType == XmlPullParser.START_TAG) {
                tagName = parser.getName();
                if (tagName.equals("tmst")) {
                    tag = XmlArrivalTrainTag.TMST;
                } else if (tagName.equals("errCd")) {
                    tag = XmlArrivalTrainTag.ERRCD;
                } else if (tagName.equals("errNm")) {
                    tag = XmlArrivalTrainTag.ERRNM;
                } else if (tagName.equals("eta")) {
                    tag = XmlArrivalTrainTag.ETA;
                } else {
                    tag = XmlArrivalTrainTag.OTHER;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                tag = null;
            } else if (eventType == XmlPullParser.TEXT) {
                String text = parser.getText();
                switch (tag) {
                case ETA:
                    break;
                case OTHER:
                    if (tagName.equals("staId")) {
                        staId = Integer.valueOf(text);
                        TrainArrival arri = arrivals.get(staId, new TrainArrival());
                        arri.setErrorCode(errCd);
                        arri.setErrorMessage(errNum);
                        arri.setTimeStamp(tmst);
                        List<Eta> etas = arri.getEtas();
                        if (etas == null) {
                            etas = new ArrayList<Eta>();
                            arri.setEtas(etas);
                        }
                        Eta eta = new Eta();
                        Station station = data.getStation(Integer.valueOf(text));
                        eta.setStation(station);
                        etas.add(eta);

                        arrivals.append(staId, arri);
                    } else if (tagName.equals("stpId")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Stop stop = data.getStop(Integer.valueOf(text));
                            currentEta.setStop(stop);
                        }
                    } else if (tagName.equals("staNm")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Station station = currentEta.getStation();
                            station.setName(text);
                        }

                    } else if (tagName.equals("stpDe")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Stop stop = currentEta.getStop();
                            stop.setDescription(text);
                        }
                    } else if (tagName.equals("rn")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setRunNumber(Integer.valueOf(text));
                        }
                    } else if (tagName.equals("rt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            TrainLine line = TrainLine.fromXmlString(text);
                            currentEta.setRouteName(line);
                        }
                    } else if (tagName.equals("destSt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Integer i = Integer.valueOf(text);
                            currentEta.setDestSt(i);
                        }
                    } else if (tagName.equals("destNm")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setDestName(text);
                        }
                    } else if (tagName.equals("trDr")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setTrainRouteDirectionCode(Integer.valueOf(text));
                        }
                    } else if (tagName.equals("prdt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setPredictionDate(dfTrain.parse(text));
                        }
                    } else if (tagName.equals("arrT")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setArrivalDepartureDate(dfTrain.parse(text));
                        }
                    } else if (tagName.equals("isApp")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsApp(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("isSch")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsSch(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("isDly")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsDly(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("isFlt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsFlt(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    }
                    break;
                case TMST:
                    tmst = dfTrain.parse(text);
                    break;
                case ERRCD:
                    errCd = Integer.valueOf(text);
                    break;
                case ERRNM:
                    errNum = text;
                    break;
                default:
                    break;
                }
            }
            eventType = parser.next();
        }
    } catch (XmlPullParserException e) {
        throw new ParserException(TrackerException.ERROR, e);
    } catch (ParseException e) {
        throw new ParserException(TrackerException.ERROR, e);
    } catch (IOException e) {
        throw new ParserException(TrackerException.ERROR, e);
    }
    List<Eta> res = new ArrayList<Eta>();
    int index = 0;
    while (index < arrivals.size()) {
        TrainArrival arri = arrivals.valueAt(index++);
        List<Eta> etas = arri.getEtas();
        if (etas != null && etas.size() != 0) {
            res.add(etas.get(0));
        }
    }
    Collections.sort(res);
    return res;
}

From source file:fr.cph.chicago.xml.Xml.java

/**
 * Parse arrivals//from   www . j  av a 2 s.  com
 * 
 * @param xml
 *            the xml string
 * @param data
 *            the train data
 * @return a list of train arrival
 * @throws ParserException
 *             the parser exception
 */
public final SparseArray<TrainArrival> parseArrivals(final String xml, final TrainData data)
        throws ParserException {
    InputStream is = new ByteArrayInputStream(xml.getBytes());
    SparseArray<TrainArrival> arrivals = null;
    try {
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        XmlArrivalTrainTag tag = null;
        Date tmst = null;
        Integer errCd = null;
        String errNum = null;
        String tagName = null;
        Integer staId = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_DOCUMENT) {
                arrivals = new SparseArray<TrainArrival>();
            } else if (eventType == XmlPullParser.START_TAG) {
                tagName = parser.getName();
                if (tagName.equals("tmst")) {
                    tag = XmlArrivalTrainTag.TMST;
                } else if (tagName.equals("errCd")) {
                    tag = XmlArrivalTrainTag.ERRCD;
                } else if (tagName.equals("errNm")) {
                    tag = XmlArrivalTrainTag.ERRNM;
                } else if (tagName.equals("eta")) {
                    tag = XmlArrivalTrainTag.ETA;
                } else {
                    tag = XmlArrivalTrainTag.OTHER;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                tag = null;
            } else if (eventType == XmlPullParser.TEXT) {
                String text = parser.getText();
                switch (tag) {
                case ETA:
                    break;
                case OTHER:
                    if (tagName.equals("staId")) {
                        staId = Integer.valueOf(text);
                        TrainArrival arri = arrivals.get(staId, new TrainArrival());
                        arri.setErrorCode(errCd);
                        arri.setErrorMessage(errNum);
                        arri.setTimeStamp(tmst);
                        List<Eta> etas = arri.getEtas();
                        if (etas == null) {
                            etas = new ArrayList<Eta>();
                            arri.setEtas(etas);
                        }
                        Eta eta = new Eta();
                        Station station = data.getStation(Integer.valueOf(text));
                        eta.setStation(station);
                        etas.add(eta);

                        arrivals.append(staId, arri);
                    } else if (tagName.equals("stpId")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Stop stop = data.getStop(Integer.valueOf(text));
                            currentEta.setStop(stop);
                        }
                    } else if (tagName.equals("staNm")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Station station = currentEta.getStation();
                            station.setName(text);
                        }

                    } else if (tagName.equals("stpDe")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Stop stop = currentEta.getStop();
                            stop.setDescription(text);
                        }
                    } else if (tagName.equals("rn")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setRunNumber(Integer.valueOf(text));
                        }
                    } else if (tagName.equals("rt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            TrainLine line = TrainLine.fromXmlString(text);
                            currentEta.setRouteName(line);
                        }
                    } else if (tagName.equals("destSt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Integer i = Integer.valueOf(text);
                            currentEta.setDestSt(i);
                        }
                    } else if (tagName.equals("destNm")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setDestName(text);
                        }
                    } else if (tagName.equals("trDr")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setTrainRouteDirectionCode(Integer.valueOf(text));
                        }
                    } else if (tagName.equals("prdt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setPredictionDate(dfTrain.parse(text));
                        }
                    } else if (tagName.equals("arrT")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setArrivalDepartureDate(dfTrain.parse(text));
                        }
                    } else if (tagName.equals("isApp")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsApp(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("isSch")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsSch(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("isDly")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsDly(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("isFlt")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setIsFlt(BooleanUtils.toBoolean(Integer.valueOf(text)));
                        }
                    } else if (tagName.equals("flags")) {

                    } else if (tagName.equals("lat")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Position position = new Position();
                            position.setLatitude(Double.valueOf(text));
                            currentEta.setPosition(position);
                        }
                    } else if (tagName.equals("lon")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            Position position = currentEta.getPosition();
                            position.setLongitude(Double.valueOf(text));
                        }
                    } else if (tagName.equals("heading")) {
                        TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setHeading(Integer.valueOf(text));
                        }
                    }
                    break;
                case TMST:
                    tmst = dfTrain.parse(text);
                    break;
                case ERRCD:
                    errCd = Integer.valueOf(text);
                    break;
                case ERRNM:
                    errNum = text;
                    break;
                default:
                    break;
                }
            }
            eventType = parser.next();
        }
    } catch (XmlPullParserException e) {
        throw new ParserException(TrackerException.ERROR, e);
    } catch (ParseException e) {
        throw new ParserException(TrackerException.ERROR, e);
    } catch (IOException e) {
        throw new ParserException(TrackerException.ERROR, e);
    }
    return arrivals;
}

From source file:fr.cph.chicago.parser.XmlParser.java

@NonNull
public final synchronized List<Eta> parseTrainsFollow(@NonNull final InputStream is,
        @NonNull final TrainData data) throws ParserException {
    SparseArray<TrainArrival> arrivals = new SparseArray<>();
    try {//from  w w w . j a  va2  s .c om
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        XmlArrivalTrainTag tag = null;
        //         Date tmst = null;
        //         Integer errCd = null;
        //         String errNum = null;
        String tagName = null;
        Integer staId = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                tagName = parser.getName();
                switch (tagName) {
                case "tmst":
                    tag = XmlArrivalTrainTag.TMST;
                    break;
                case "errCd":
                    tag = XmlArrivalTrainTag.ERRCD;
                    break;
                case "errNm":
                    tag = XmlArrivalTrainTag.ERRNM;
                    break;
                case "eta":
                    tag = XmlArrivalTrainTag.ETA;
                    break;
                default:
                    tag = XmlArrivalTrainTag.OTHER;
                    break;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                tag = null;
            } else if (eventType == XmlPullParser.TEXT) {
                String text = parser.getText();
                switch (tag) {
                case ETA:
                    break;
                case OTHER:
                    switch (tagName) {
                    case "staId": {
                        staId = Integer.parseInt(text);
                        final TrainArrival arri = arrivals.get(staId, new TrainArrival());
                        //arri.setErrorCode(errCd);
                        //arri.setErrorMessage(errNum);
                        //arri.setTimeStamp(tmst);
                        List<Eta> etas = arri.getEtas();
                        if (etas == null) {
                            etas = new ArrayList<>();
                            arri.setEtas(etas);
                        }
                        final Eta eta = new Eta();
                        final Optional<Station> station = data.getStation(Integer.parseInt(text));
                        eta.setStation(station.orElse(new Station()));
                        etas.add(eta);

                        arrivals.append(staId, arri);
                        break;
                    }
                    case "stpId": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Stop stop = data.getStop(Integer.parseInt(text));
                            currentEta.setStop(stop);
                        }
                        break;
                    }
                    case "staNm": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Station station = currentEta.getStation();
                            station.setName(text);
                        }
                        break;
                    }
                    case "stpDe": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Stop stop = currentEta.getStop();
                            stop.setDescription(text);
                        }
                        break;
                    }
                    case "rn": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setRunNumber(Integer.parseInt(text));
                        }
                        break;
                    }
                    case "rt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final TrainLine line = TrainLine.fromXmlString(text);
                            currentEta.setRouteName(line);
                        }
                        break;
                    }
                    case "destSt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Integer i = Integer.parseInt(text);
                            currentEta.setDestSt(i);
                        }
                        break;
                    }
                    case "destNm": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setDestName(text);
                        }
                        break;
                    }
                    case "trDr": {
                        //                     final TrainArrival arri = arrivals.get(staId, null);
                        //                     if (arri != null) {
                        //                        final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                        //                        currentEta.setTrainRouteDirectionCode(Integer.parseInt(text));
                        //                     }
                        break;
                    }
                    case "prdt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setPredictionDate(simpleDateFormatTrain.parse(text));
                        }
                        break;
                    }
                    case "arrT": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setArrivalDepartureDate(simpleDateFormatTrain.parse(text));
                        }
                        break;
                    }
                    case "isApp": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setApp(Util.textNumberToBoolean(text));
                        }
                        break;
                    }
                    case "isSch": {
                        //                     final TrainArrival arri = arrivals.get(staId, null);
                        //                     if (arri != null) {
                        //                        final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                        //                        currentEta.setIsSch(Util.textNumberToBoolean(text));
                        //                     }
                        break;
                    }
                    case "isDly": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setDly(Util.textNumberToBoolean(text));
                        }
                        break;
                    }
                    case "isFlt": {
                        //                     final TrainArrival arri = arrivals.get(staId, null);
                        //                     if (arri != null) {
                        //                        final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                        //                        currentEta.setIsFlt(Util.textNumberToBoolean(text));
                        //                     }
                        break;
                    }
                    }
                    break;
                case TMST:
                    //tmst = simpleDateFormatTrain.parse(text);
                    break;
                case ERRCD:
                    //errCd = Integer.parseInt(text);
                    break;
                case ERRNM:
                    //errNum = text;
                    break;
                default:
                    break;
                }
            }
            eventType = parser.next();
        }
    } catch (XmlPullParserException | ParseException | IOException e) {
        throw new ParserException(TrackerException.ERROR, e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    final List<Eta> res = new ArrayList<>();
    int index = 0;
    while (index < arrivals.size()) {
        final TrainArrival arri = arrivals.valueAt(index++);
        final List<Eta> etas = arri.getEtas();
        if (etas != null && etas.size() != 0) {
            res.add(etas.get(0));
        }
    }
    Collections.sort(res);
    return res;
}

From source file:fr.cph.chicago.parser.XmlParser.java

/**
 * Parse arrivals/*w  w  w .j  av a2  s. co  m*/
 *
 * @param is        the xml string
 * @param trainData the train data
 * @return a list of train arrival
 * @throws ParserException the parser exception
 */
@NonNull
public final synchronized SparseArray<TrainArrival> parseArrivals(@NonNull final InputStream is,
        @NonNull final TrainData trainData) throws ParserException {
    final SparseArray<TrainArrival> arrivals = new SparseArray<>();
    try {
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        XmlArrivalTrainTag tag = null;
        //Date tmst = null;
        //Integer errCd = null;
        //String errNum = null;
        String tagName = null;
        Integer staId = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                tagName = parser.getName();
                switch (tagName) {
                case "tmst":
                    tag = XmlArrivalTrainTag.TMST;
                    break;
                case "errCd":
                    tag = XmlArrivalTrainTag.ERRCD;
                    break;
                case "errNm":
                    tag = XmlArrivalTrainTag.ERRNM;
                    break;
                case "eta":
                    tag = XmlArrivalTrainTag.ETA;
                    break;
                default:
                    tag = XmlArrivalTrainTag.OTHER;
                    break;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                tag = null;
            } else if (eventType == XmlPullParser.TEXT) {
                final String text = parser.getText();
                switch (tag) {
                case ETA:
                    break;
                case OTHER:
                    switch (tagName) {
                    case "staId": {
                        staId = Integer.parseInt(text);
                        final TrainArrival arri = arrivals.get(staId, new TrainArrival());
                        //arri.setErrorCode(errCd);
                        //arri.setErrorMessage(errNum);
                        //arri.setTimeStamp(tmst);
                        List<Eta> etas = arri.getEtas();
                        if (etas == null) {
                            etas = new ArrayList<>();
                            arri.setEtas(etas);
                        }
                        final Eta eta = new Eta();
                        final Optional<Station> station = trainData.getStation(staId);
                        eta.setStation(station.orElse(new Station()));
                        etas.add(eta);

                        arrivals.append(staId, arri);
                        break;
                    }
                    case "stpId": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Stop stop = trainData.getStop(Integer.parseInt(text));
                            currentEta.setStop(stop);
                        }
                        break;
                    }
                    case "staNm": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.getStation().setName(text);
                        }
                        break;
                    }
                    case "stpDe": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.getStop().setDescription(text);
                        }
                        break;
                    }
                    case "rn": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setRunNumber(Integer.parseInt(text));
                        }
                        break;
                    }
                    case "rt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setRouteName(TrainLine.fromXmlString(text));
                        }
                        break;
                    }
                    case "destSt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Integer i = Integer.parseInt(text);
                            currentEta.setDestSt(i);
                        }
                        break;
                    }
                    case "destNm": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            if ("See train".equalsIgnoreCase(text)
                                    && currentEta.getStop().getDescription().contains("Loop")
                                    && currentEta.getRouteName() == TrainLine.GREEN) {
                                currentEta.setDestName("Loop");
                            } else if ("See train".equalsIgnoreCase(text)
                                    && currentEta.getStop().getDescription().contains("Loop")
                                    && currentEta.getRouteName() == TrainLine.BROWN) {
                                currentEta.setDestName("Loop");
                            } else if ("Loop, Midway".equalsIgnoreCase(text)
                                    && currentEta.getRouteName() == TrainLine.BROWN) {
                                currentEta.setDestName("Loop");
                            } else {
                                currentEta.setDestName(text);
                            }
                        }
                        break;
                    }
                    case "trDr": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setTrainRouteDirectionCode(Integer.parseInt(text));
                        }
                        break;
                    }
                    case "prdt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setPredictionDate(simpleDateFormatTrain.parse(text));
                        }
                        break;
                    }
                    case "arrT": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setArrivalDepartureDate(simpleDateFormatTrain.parse(text));
                        }
                        break;
                    }
                    case "isApp": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setApp(BooleanUtils.toBoolean(Integer.parseInt(text)));
                        }
                        break;
                    }
                    case "isSch": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setSch(BooleanUtils.toBoolean(Integer.parseInt(text)));
                        }
                        break;
                    }
                    case "isDly": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setDly(BooleanUtils.toBoolean(Integer.parseInt(text)));
                        }
                        break;
                    }
                    case "isFlt": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setFlt(BooleanUtils.toBoolean(Integer.parseInt(text)));
                        }
                        break;
                    }
                    case "flags":
                        break;
                    case "lat": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Position position = new Position();
                            position.setLatitude(Double.parseDouble(text));
                            currentEta.setPosition(position);
                        }
                        break;
                    }
                    case "lon": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            final Position position = currentEta.getPosition();
                            position.setLongitude(Double.parseDouble(text));
                        }
                        break;
                    }
                    case "heading": {
                        final TrainArrival arri = arrivals.get(staId, null);
                        if (arri != null) {
                            final Eta currentEta = arri.getEtas().get(arri.getEtas().size() - 1);
                            currentEta.setHeading(Integer.parseInt(text));
                        }
                        break;
                    }
                    }
                    break;
                case TMST:
                    //tmst = simpleDateFormatTrain.parse(text);
                    break;
                case ERRCD:
                    //errCd = Integer.parseInt(text);
                    break;
                case ERRNM:
                    //errNum = text;
                    break;
                default:
                    break;
                }
            }
            eventType = parser.next();
        }
    } catch (final XmlPullParserException | ParseException | IOException e) {
        Log.e(TAG, e.getMessage(), e);
        throw new ParserException(TrackerException.ERROR, e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return arrivals;
}