package fr.cph.chicago.parser;

import com.annimon.stream.Stream;
import com.univocity.parsers.common.CommonSettings;
import com.univocity.parsers.common.Format;
import com.univocity.parsers.common.ParsingContext;
import com.univocity.parsers.common.processor.RowProcessor;

import java.util.ArrayList;
import java.util.List;

import fr.cph.chicago.entity.BusStop;
import fr.cph.chicago.entity.Position;
import io.realm.Realm;

class BusStopCsvProcessor implements RowProcessor {

    private List<BusStop> rows;
    private Realm realm;

    @Override
    public void processStarted(final ParsingContext context) {
        rows = new ArrayList<>(12000);
        realm = Realm.getDefaultInstance();
    }

    /**
     * Stores the row extracted by the parser into a list.
     *
     * @param row     the data extracted by the parser for an individual record. Note that:
     *                <ul>
     *                <li>it will never by null. </li>
     *                <li>it will never be empty unless explicitly configured using {@link CommonSettings#setSkipEmptyLines(boolean)}</li>
     *                <li>it won't contain lines identified by the parser as comments. To disable comment processing set {@link Format#setComment(char)} to '\0'</li>
     *                </ul>
     * @param context A contextual object with information and controls over the current state of the parsing process
     */
    @Override
    public void rowProcessed(final String[] row, final ParsingContext context) {
        final int stopId = Integer.parseInt(row[0]); // stop_id
        // String stopCode = TrainDirection.fromString(row[1]); // stop_code
        final String stopName = row[2]; // stop_name
        final String stopDesc = row[3]; // stop_desc

        final double latitude = Double.parseDouble(row[4]);// stop_lat
        final double longitude = Double.parseDouble(row[5]);// stop_lon

        final BusStop busStop = new BusStop();
        busStop.setId(stopId);
        busStop.setName(stopName);
        busStop.setDescription(stopDesc);
        final Position position = new Position();
        position.setLatitude(latitude);
        position.setLongitude(longitude);
        busStop.setPosition(position);

        rows.add(busStop);
    }

    @Override
    public void processEnded(final ParsingContext context) {
        realm.beginTransaction();
        Stream.of(rows).forEach(busStop -> realm.copyToRealm(busStop));
        realm.commitTransaction();
        realm.close();
    }
}

@@ -1577,17 +1577,17 @@
     
-// String
+final int
  sto
@@ -1598,56 +1598,103 @@
 e = 
-TrainDirection.fromString(row%5B1%5D); // stop_code%0A
+row%5B1%5D == null ? -1 : Integer.parseInt(row%5B1%5D); // stop_code%0A        if (stopCode != -1) %7B%0A    
     
@@ -1742,32 +1742,36 @@
 op_name%0A        
+    
 final String sto
@@ -1800,16 +1800,20 @@
 p_desc%0A%0A
+    
         
@@ -1875,32 +1875,36 @@
 top_lat%0A        
+    
 final double lon
@@ -1956,24 +1956,28 @@
 on%0A%0A        
+    
 final BusSto
@@ -1999,24 +1999,28 @@
  BusStop();%0A
+    
         busS
@@ -2038,32 +2038,36 @@
 topId);%0A        
+    
 busStop.setName(
@@ -2073,24 +2073,28 @@
 (stopName);%0A
+    
         busS
@@ -2127,24 +2127,28 @@
 c);%0A        
+    
 final Positi
@@ -2177,32 +2177,36 @@
 tion();%0A        
+    
 position.setLati
@@ -2221,32 +2221,36 @@
 itude);%0A        
+    
 position.setLong
@@ -2259,32 +2259,36 @@
 ude(longitude);%0A
+    
         busStop.
@@ -2315,24 +2315,28 @@
 );%0A%0A        
+    
 rows.add(bus
@@ -2342,16 +2342,26 @@
 sStop);%0A
+        %7D%0A
     %7D%0A%0A 

Improve the way bus stops are parsed. Now do not add a bus that does not contain a stopCode