Skip to content

Commit

Permalink
Removed 20 warnings (#2651)
Browse files Browse the repository at this point in the history
* removed 9 warnings - replaced size with length for arrays and strings

* removed 10 warnings - resource not closed

* removed 1 warning - simplified expression

* applied scalaFmt

* removed unused import

* fixed loading in advance all lines

* fixed readAllLines method
  • Loading branch information
carloscaldas authored May 20, 2020
1 parent d33ee33 commit ab2a13b
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 66 deletions.
20 changes: 8 additions & 12 deletions src/main/scala/beam/agentsim/agents/TransitSystem.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package beam.agentsim.agents

import akka.actor.SupervisorStrategy.Stop
import scala.util.{Random, Try}

import akka.actor.{Actor, ActorLogging, ActorRef, OneForOneStrategy, Terminated}
import akka.actor.SupervisorStrategy.Stop
import beam.agentsim.agents.BeamAgent.Finish
import beam.agentsim.agents.vehicles.EnergyEconomyAttributes.Powertrain
import beam.agentsim.agents.vehicles.{BeamVehicle, BeamVehicleType}
import beam.agentsim.agents.vehicles.EnergyEconomyAttributes.Powertrain
import beam.agentsim.scheduler.BeamAgentScheduler.{CompletionNotice, ScheduleTrigger}
import beam.agentsim.scheduler.Trigger.TriggerWithId
import beam.router.{BeamRouter, Modes, TransitInitializer}
Expand All @@ -14,16 +16,13 @@ import beam.router.osm.TollCalculator
import beam.sim.BeamScenario
import beam.sim.common.GeoUtils
import beam.sim.config.BeamConfig
import beam.utils.NetworkHelper
import beam.utils.{FileUtils, NetworkHelper}
import beam.utils.logging.ExponentialLazyLogging
import com.conveyal.r5.transit.{RouteInfo, TransitLayer, TransportNetwork}
import org.matsim.api.core.v01.{Id, Scenario}
import org.matsim.core.api.experimental.events.EventsManager
import org.matsim.vehicles.Vehicle

import scala.io.Source
import scala.util.{Random, Try}

class TransitSystem(
val beamScenario: BeamScenario,
val scenario: Scenario,
Expand Down Expand Up @@ -162,13 +161,10 @@ class TransitVehicleInitializer(val beamConfig: BeamConfig, val vehicleTypes: Ma
}
}

private def loadTransitVehicleTypesMap() = {
private def loadTransitVehicleTypesMap(): Map[String, Map[String, String]] = {
val file = beamConfig.beam.agentsim.agents.vehicles.transitVehicleTypesByRouteFile
Try(
Source
.fromFile(beamConfig.beam.agentsim.agents.vehicles.transitVehicleTypesByRouteFile)
.getLines()
.toList
.tail
FileUtils.readAllLines(file).toList.tail
).getOrElse(List())
.map(_.trim.split(","))
.filter(_.length > 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package beam.agentsim.agents.choice.mode

import java.io.FileNotFoundException
import java.io.{File, FileNotFoundException}
import java.nio.file.{Files, Paths}

import scala.collection.mutable.ListBuffer
import scala.util.Try

import beam.agentsim.agents.choice.mode.ModeIncentive.Incentive
import beam.router.Modes.BeamMode
import beam.sim.common._
import beam.sim.population.AttributesOfIndividual

import scala.collection.mutable.ListBuffer
import scala.io.Source
import scala.util.Try
import beam.utils.FileUtils

case class ModeIncentive(modeIncentives: Map[BeamMode, List[Incentive]]) {

Expand Down Expand Up @@ -44,7 +44,7 @@ object ModeIncentive {
if (Files.notExists(Paths.get(incentivesFile)))
throw new FileNotFoundException(s"ModeIncentive file not found at location: $incentivesFile")
val incentives: ListBuffer[Incentive] = ListBuffer()
val lines = Try(Source.fromFile(incentivesFile).getLines().toList.tail).getOrElse(List())
val lines = Try(FileUtils.readAllLines(incentivesFile).toList.tail).getOrElse(List())
for (line <- lines) {
val row = line.split(",")

Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/beam/agentsim/agents/choice/mode/PtFares.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package beam.agentsim.agents.choice.mode

import java.io.FileNotFoundException
import java.io.{File, FileNotFoundException}
import java.nio.file.{Files, Paths}

import scala.collection.mutable.ListBuffer
import scala.util.Try

import beam.agentsim.agents.choice.mode.PtFares.FareRule
import beam.sim.common.Range
import beam.utils.FileUtils
import org.slf4j.LoggerFactory

import scala.collection.mutable.ListBuffer
import scala.io.Source
import scala.util.Try

case class PtFares(ptFares: List[FareRule]) {

def getPtFare(agencyId: Option[String], routeId: Option[String], age: Option[Int]): Option[Double] = {
Expand Down Expand Up @@ -75,7 +75,7 @@ object PtFares {
throw new FileNotFoundException(s"PtFares file not found at location: $ptFaresFile")
}
val fareRules: ListBuffer[FareRule] = ListBuffer()
val lines = Try(Source.fromFile(ptFaresFile).getLines().toList.tail).getOrElse(List())
val lines = Try(FileUtils.readAllLines(ptFaresFile).toList.tail).getOrElse(List())
for (line <- lines) {
val row = line.split(",")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ class RideHailManager(
}
legOpt.foreach { leg =>
passengersToAdd.foreach { pass =>
val legsForPerson = pickDropsForGrouping.get(pass).getOrElse(List()) :+ leg
val legsForPerson = pickDropsForGrouping.getOrElse(pass, List()) :+ leg
pickDropsForGrouping = pickDropsForGrouping + (pass -> legsForPerson)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ case class TNCIterationStats(
val vehicleToCoordAssignment = if (topScored.nonEmpty) {
val coords =
if (repositioningMethod
.equalsIgnoreCase("TOP_SCORES") || topScored.size <= vehicles.size) {
.equalsIgnoreCase("TOP_SCORES") || topScored.length <= vehicles.size) {
// Not using
val scoreExpSumOverAllTAZInRadius =
topScored.map(taz => taz.score).sum
Expand Down Expand Up @@ -203,7 +203,7 @@ case class TNCIterationStats(
val clusterInput = topScored.map(t => new LocationWrapper(t.taz.coord))

val clusterSize =
if (clusterInput.size < vehicles.size) clusterInput.size
if (clusterInput.length < vehicles.size) clusterInput.length
else vehicles.size
val kMeans =
new KMeansPlusPlusClusterer[LocationWrapper](clusterSize, 1000)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package beam.calibration.impl.example

import java.io.File
import java.net.URI
import java.nio.file.Paths

import scala.io.Source
import scala.util.Try

import beam.calibration.impl.example.ModeChoiceObjectiveFunction.ModeChoiceStats
import beam.utils.FileUtils
import io.circe._
import io.circe.generic.semiauto._
import io.circe.parser._
import org.apache.http.client.fluent.{Content, Request}
import beam.calibration.impl.example.ModeChoiceObjectiveFunction.ModeChoiceStats

class ModeChoiceObjectiveFunction(benchmarkDataFileLoc: String) {

Expand Down Expand Up @@ -115,7 +116,7 @@ class ModeChoiceObjectiveFunction(benchmarkDataFileLoc: String) {
}

def getStatsFromFile(fileLoc: String): Map[String, Double] = {
val lines = Source.fromFile(fileLoc).getLines().toArray
val lines = FileUtils.readAllLines(fileLoc).toArray
val header = lines.head.split(",").tail
val lastIter = lines.reverse.head.split(",").tail.map(_.toDouble)
val total = lastIter.sum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package beam.calibration.impl.example

import scala.io.Source

import beam.calibration.api.ObjectiveFunction
import beam.utils.FileUtils.using
import beam.utils.FileUtils

object RideHailObjectiveFunction {

Expand All @@ -14,7 +11,7 @@ object RideHailObjectiveFunction {
}

def getStatsFromFile(fileLoc: String): Map[String, Double] = {
val lines = Source.fromFile(fileLoc).getLines().toArray
val lines = FileUtils.readAllLines(fileLoc).toArray
val header = lines.head.split(",").tail
val lastIter = lines.reverse.head.split(",").tail.map(_.toDouble)
val total = lastIter.sum
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/beam/router/BeamRouter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ class BeamRouter(
case t: TryToSerialize =>
if (log.isDebugEnabled) {
val byteArray = kryoSerializer.toBinary(t)
log.debug("TryToSerialize size in bytes: {}, MBytes: {}", byteArray.size, byteArray.size.toDouble / 1024 / 1024)
log.debug(
"TryToSerialize size in bytes: {}, MBytes: {}",
byteArray.length,
byteArray.length.toDouble / 1024 / 1024
)
}
case msg: UpdateTravelTimeLocal =>
traveTimeOpt = Some(msg.travelTime)
Expand Down
17 changes: 8 additions & 9 deletions src/main/scala/beam/router/osm/TollCalculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import java.io._
import java.nio.file.{Files, Path, Paths}
import java.util
import java.util.Collections
import javax.inject.Inject

import scala.collection.JavaConverters._
import scala.util.Try
import scala.util.control.NonFatal

import beam.router.model.BeamPath
import beam.router.osm.TollCalculator.Toll
import beam.sim.common.Range
import beam.sim.config.BeamConfig
import beam.utils.FileUtils
import com.conveyal.osmlib.OSM
import com.google.common.collect.Maps
import com.typesafe.scalalogging.LazyLogging
import gnu.trove.map.hash.TIntObjectHashMap
import javax.inject.Inject
import org.apache.commons.collections4.MapUtils

import scala.collection.JavaConverters._
import scala.io.Source
import scala.util.Try
import scala.util.control.NonFatal

class TollCalculator @Inject()(val config: BeamConfig) extends LazyLogging {
import beam.utils.FileUtils._

Expand Down Expand Up @@ -71,9 +71,8 @@ class TollCalculator @Inject()(val config: BeamConfig) extends LazyLogging {

private def readTollPrices(tollPricesFile: String): java.util.Map[Int, Array[Toll]] = {
if (Files.exists(Paths.get(tollPricesFile))) {
val rowList = Source
.fromFile(tollPricesFile)
.getLines()
val rowList = FileUtils
.readAllLines(tollPricesFile)
.drop(1) // table header
.toArray
.map(_.split(","))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package beam.router.r5

import java.nio.file.attribute.BasicFileAttributes
import java.nio.file._
import java.nio.file.attribute.BasicFileAttributes
import java.util

import beam.sim.BeamServices
import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConverters._

import beam.sim.config.BeamConfig
import beam.utils.FileUtils
import com.conveyal.gtfs.GTFSFeed
import com.conveyal.gtfs.model.Trip
import com.conveyal.r5.analyst.scenario.{AddTrips, AdjustFrequency, Scenario}
import com.conveyal.r5.transit.TransportNetwork
import org.matsim.api.core.v01.network.Network

import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConverters._
import scala.io.Source

case class FrequencyAdjustingNetworkCoordinator(beamConfig: BeamConfig) extends NetworkCoordinator {

override var transportNetwork: TransportNetwork = _
Expand All @@ -24,7 +23,8 @@ case class FrequencyAdjustingNetworkCoordinator(beamConfig: BeamConfig) extends
var frequencyData: Set[FrequencyAdjustmentInput] = _

def loadFrequencyData(): Set[FrequencyAdjustmentInput] = {
val lines = Source.fromFile(beamConfig.beam.agentsim.scenarios.frequencyAdjustmentFile).getLines().drop(1)
val frequencyAdjustmentFile: String = beamConfig.beam.agentsim.scenarios.frequencyAdjustmentFile
val lines = FileUtils.readAllLines(frequencyAdjustmentFile).drop(1)
val dataRows = for { line <- lines } yield {
line.split(",")
}.toSeq
Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/beam/utils/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import java.nio.file.{Files, Path, Paths}
import java.text.SimpleDateFormat
import java.util.stream

import scala.io.Source

import beam.sim.config.BeamConfig
import beam.utils.UnzipUtility.unzip
import com.typesafe.scalalogging.LazyLogging
Expand Down Expand Up @@ -207,6 +209,16 @@ object FileUtils extends LazyLogging {
unpackedPath
}

def readAllLines(file: File): Seq[String] = {
using(Source.fromFile(file.getPath)) { source =>
source.getLines().toList
}
}

def readAllLines(file: String): Seq[String] = {
readAllLines(new File(file))
}

private def isZipArchive(sourceFilePath: String): Boolean = {
assert(sourceFilePath != null)
"zip".equalsIgnoreCase(getExtension(sourceFilePath))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/beam/utils/GeoJsonReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object GeoJsonReader extends LazyLogging {
}
val features = it.map(mapper).toArray
val end = System.currentTimeMillis()
logger.info(s"Read ${features.size} features from '$geoJsonPath' in ${end - start} ms")
logger.info(s"Read ${features.length} features from '$geoJsonPath' in ${end - start} ms")
features
} finally {
fileIn.close()
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/beam/utils/map/GeoFenceVerifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ object GeoFenceVerifier extends LazyLogging {
}
val errors = geofenceErrorPerPte.flatten.flatten.filter(p => p.offset > 0)
logger.info(
s"Number of PTE for ride-hail is ${pathTraversalEvents.size}. There are ${errors.length} points which violate geofence"
s"Number of PTE for ride-hail is ${pathTraversalEvents.length}. There are ${errors.length} points which violate geofence"
)
logger.info("Stats about violations:")
logger.info(s"Distance: ${Statistics(errors.map(_.offset))}")
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/beam/utils/map/TazTravelTimeAnalyzer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ object TazTravelTimeAnalyzer extends LazyLogging {
}
.filter(x => x.observedTravelTime.isDefined)

logger.info(s"withObservedTravelTimes size: ${withObservedTravelTimes.size}")
logger.info(s"withObservedTravelTimes size: ${withObservedTravelTimes.length}")

val writer =
IOUtils.getBufferedWriter("c:/temp/travel_time_investigation/tazODTravelTimeObservedVsSimulated_derived.csv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object MatsimConversionTool extends App {

val dummyGtfsPath = "test/input/beamville/r5/dummy.zip"

if (null != args && args.size > 0) {
if (null != args && args.length > 0) {
val beamConfigFilePath = args(0) //"test/input/beamville/beam.conf"

val config = parseFileSubstitutingInputDirectory(beamConfigFilePath)
Expand Down
18 changes: 9 additions & 9 deletions src/test/scala/beam/integration/EventsFileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package beam.integration

import java.io.File

import scala.collection.JavaConverters._
import scala.util.Try

import beam.agentsim.agents.planning.BeamPlan
import beam.agentsim.events.PathTraversalEvent
import beam.analysis.plots.TollRevenueAnalysis
import beam.router.Modes.BeamMode.{BIKE, CAR}
import beam.sim.BeamHelper
import beam.sim.config.BeamExecutionConfig
import beam.utils.EventReader._
import com.typesafe.config.{Config, ConfigValueFactory}
import beam.utils.FileUtils
import com.google.inject
import com.typesafe.config.{Config, ConfigValueFactory}
import org.matsim.api.core.v01.Id
import org.matsim.api.core.v01.population.{Activity, Leg, Person}
import org.matsim.core.config.ConfigUtils
Expand All @@ -20,10 +24,6 @@ import org.matsim.core.scenario.{MutableScenario, ScenarioUtils}
import org.matsim.households.Household
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}

import scala.collection.JavaConverters._
import scala.io.Source
import scala.util.Try

class EventsFileSpec extends FlatSpec with BeforeAndAfterAll with Matchers with BeamHelper with IntegrationSpecCommon {

private lazy val config: Config = baseConfig
Expand Down Expand Up @@ -86,8 +86,8 @@ class EventsFileSpec extends FlatSpec with BeforeAndAfterAll with Matchers with
trips.toSet
}

private def tripsFromGtfs(file: File) = {
val trips = for (line <- Source.fromFile(file.getPath).getLines.drop(1))
private def tripsFromGtfs(file: File): Set[String] = {
val trips = for (line <- FileUtils.readAllLines(file).drop(1))
yield line.split(",")(2)
trips.toSet
}
Expand All @@ -113,8 +113,8 @@ class EventsFileSpec extends FlatSpec with BeforeAndAfterAll with Matchers with
eventsByTrip.map { case (k, v) => (k, v.size) }
}

private def stopToStopLegsFromGtfsByTrip(stopTimesFile: String) = {
val stopTimes = for (line <- Source.fromFile(new File(stopTimesFile).getPath).getLines.drop(1))
private def stopToStopLegsFromGtfsByTrip(stopTimesFile: String): Map[String, Int] = {
val stopTimes = for (line <- FileUtils.readAllLines(stopTimesFile).drop(1))
yield line.split(",")
val stopTimesByTrip = stopTimes.toList.groupBy(_(0))
stopTimesByTrip.map { case (k, v) => (k, v.size - 1) }
Expand Down

0 comments on commit ab2a13b

Please sign in to comment.