All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.skjolber.dc.gtfs.st.AgencyAdapter Maven / Gradle / Ivy

package com.github.skjolber.dc.gtfs.st;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

import com.github.skjolber.dc.model.Agency;
import com.github.skjolber.stcsv.CsvMapper;
import com.github.skjolber.stcsv.CsvMapper2;
import com.github.skjolber.stcsv.CsvReader;
import com.github.skjolber.unzip.FileEntryChunkStreamHandler;
import com.github.skjolber.unzip.FileEntryHandler;
import com.github.skjolber.unzip.FileEntryStreamHandler;
import com.github.skjolber.unzip.csv.AbstractSesselTjonnaCsvFileEntryStreamHandler;
import com.github.skjolber.unzip.csv.CsvLineHandler;
import com.github.skjolber.unzip.csv.CsvLineHandlerFactory;

/**
 * For single-threaded parsing.
 *
 */

public class AgencyAdapter implements CsvLineHandlerFactory, CsvLineHandler {

	protected static CsvMapper parser;

	static {
		// agency_id,agency_name,agency_url,agency_timezone,agency_phone
		parser = CsvMapper2.builder(Agency.class)
			.stringField("agency_id")
				.setter(Agency::setId)
				.optional()
			.stringField("agency_name")
				.setter(Agency::setName)
				.quoted()
				.required()
			.stringField("agency_url")
				.setter(Agency::setUrl)
				.required()
			.stringField("agency_timezone")
				.setter(Agency::setTimezone)
				.required()
			.stringField("agency_lang")
				.setter(Agency::setLang)
				.optional()				
			.stringField("agency_phone")
				.setter(Agency::setPhone)
				.optional()				
			.stringField("agency_fare_url")
				.setter(Agency::setFareUrl)
				.optional()				
			.stringField("agency_email")
				.setter(Agency::setEmail)
				.optional()				
			.build();
	}

	protected List agencies = new ArrayList<>();

	public List getAgencies() {
		return agencies;
	}

	@Override
	public void handleLine(Agency value) {
		agencies.add(value);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  CsvLineHandler getHandler(String fileName, ThreadPoolExecutor executor) {
		if(!fileName.equals("agency.txt")) {
			throw new IllegalArgumentException();
		}
		return (CsvLineHandler)this;
	}
	
	private class RouteCsvFileEntryStreamHandler extends AbstractSesselTjonnaCsvFileEntryStreamHandler {

		public RouteCsvFileEntryStreamHandler(String name, CsvLineHandlerFactory csvLineHandlerFactory, long size, FileEntryHandler fileEntryHandler, ThreadPoolExecutor executor) {
			super(name, csvLineHandlerFactory, fileEntryHandler, executor);
		}

		@Override
		protected CsvReader createCsvReader(Reader reader, ThreadPoolExecutor executorService) throws Exception {
			return parser.create(reader);
		}
	}

	public FileEntryStreamHandler getFileEntryStreamHandler(FileEntryHandler fileEntryHandler, ThreadPoolExecutor executor, long size) throws Exception {
		return new RouteCsvFileEntryStreamHandler("agency.txt", this, size, fileEntryHandler, executor);
	}

	public FileEntryChunkStreamHandler getFileEntryChunkedStreamHandler() throws Exception {
		return null;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy