Example usage for android.util SparseArray append

List of usage examples for android.util SparseArray append

Introduction

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

Prototype

public void append(int key, E value) 

Source Link

Document

Puts a key/value pair into the array, optimizing for the case where the key is greater than all existing keys in the array.

Usage

From source file:com.staggeredgrid.library.StaggeredGridView.java

/***
 * Our mColumnTops and mColumnBottoms need to be re-built up to the
 * mSyncPosition - the following layout request will then
 * layout the that position and then fillUp and fillDown appropriately.
 *//*w  w  w .  jav a  2  s. c  om*/
private void onColumnSync() {
    // re-calc tops for new column count!
    int syncPosition = Math.min(mSyncPosition, getCount() - 1);

    SparseArray<Double> positionHeightRatios = new SparseArray<Double>(syncPosition);
    for (int pos = 0; pos < syncPosition; pos++) {
        // check for weirdness
        final GridItemRecord rec = mPositionData.get(pos);
        if (rec == null)
            break;

        Log.d(TAG, "onColumnSync:" + pos + " ratio:" + rec.heightRatio);
        positionHeightRatios.append(pos, rec.heightRatio);
    }

    mPositionData.clear();

    // re-calc our relative position while at the same time
    // rebuilding our GridItemRecord collection

    if (DBG)
        Log.d(TAG, "onColumnSync column width:" + mColumnWidth);

    for (int pos = 0; pos < syncPosition; pos++) {
        //Check for weirdness again
        final Double heightRatio = positionHeightRatios.get(pos);
        if (heightRatio == null) {
            break;
        }

        final GridItemRecord rec = getOrCreateRecord(pos);
        final int height = (int) (mColumnWidth * heightRatio);
        rec.heightRatio = heightRatio;

        int top;
        int bottom;
        // check for headers
        if (isHeaderOrFooter(pos)) {
            // the next top is the bottom for that column
            top = getLowestPositionedBottom();
            bottom = top + height;

            for (int i = 0; i < mColumnCount; i++) {
                mColumnTops[i] = top;
                mColumnBottoms[i] = bottom;
            }
        } else {
            // what's the next column down ?
            final int column = getHighestPositionedBottomColumn();
            // the next top is the bottom for that column
            top = mColumnBottoms[column];
            bottom = top + height + getChildTopMargin(pos) + getChildBottomMargin();

            mColumnTops[column] = top;
            mColumnBottoms[column] = bottom;

            rec.column = column;
        }

        if (DBG)
            Log.d(TAG, "onColumnSync position:" + pos + " top:" + top + " bottom:" + bottom + " height:"
                    + height + " heightRatio:" + heightRatio);
    }

    // our sync position will be displayed in this column
    final int syncColumn = getHighestPositionedBottomColumn();
    setPositionColumn(syncPosition, syncColumn);

    // we want to offset from height of the sync position
    // minus the offset
    int syncToBottom = mColumnBottoms[syncColumn];
    int offset = -syncToBottom + mSpecificTop;
    // offset all columns by
    offsetAllColumnsTopAndBottom(offset);

    // sync the distance to top
    mDistanceToTop = -syncToBottom;

    // stash our bottoms in our tops - though these will be copied back to the bottoms
    System.arraycopy(mColumnBottoms, 0, mColumnTops, 0, mColumnCount);
}

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. ja v  a 2  s.  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// ww w  .j a v  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:android.support.design.widget.CoordinatorLayout.java

@Override
protected Parcelable onSaveInstanceState() {
    final SavedState ss = new SavedState(super.onSaveInstanceState());

    final SparseArray<Parcelable> behaviorStates = new SparseArray<>();
    for (int i = 0, count = getChildCount(); i < count; i++) {
        final View child = getChildAt(i);
        final int childId = child.getId();
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final Behavior b = lp.getBehavior();

        if (childId != NO_ID && b != null) {
            // If the child has an ID and a Behavior, let it save some state...
            Parcelable state = b.onSaveInstanceState(this, child);
            if (state != null) {
                behaviorStates.append(childId, state);
            }//from ww  w .  j ava 2 s .c  o m
        }
    }
    ss.behaviorStates = behaviorStates;
    return ss;
}

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  ava  2  s.  co 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_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 ww .  j a  v a2  s . c o  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;
}