Unexpected token while using EnumSet<RouteOptions.HazardousGoodType!>

95 Views Asked by At

I'm using HERE MAPS SDK PREMIUM V3.18.3 and I'm trying to get truck routing with routeOptions.truckShippedHazardousGoods = RouteOptions.HazardousGoodType.COMBUSTIBLE This gives an error stating: Type mismatch. Required: EnumSet<RouteOptions.HazardousGoodType!> Found: RouteOptions.HazardousGoodType

val routeOptions = RouteOptions()
    routeOptions.setTransportMode(RouteOptions.TransportMode.TRUCK)
    routeOptions.setHighwaysAllowed(true)

    routeOptions.truckHeight = truckHeightMeters
    routeOptions.truckLength = truckLengthMeters
    routeOptions.truckWidth = truckWidthMeters
    routeOptions.truckLimitedWeight = truckLimitedWeightTon
    routeOptions.truckTrailersCount = numberOfTrailers
    routeOptions.truckShippedHazardousGoods = Hazmat.COMBUSTABLE
    routeOptions.speedProfile
    routeOptions.routeType = RouteOptions.Type.FASTEST
    routeOptions.routeCount = 1

    routeOptions.setTruckTunnelCategory(RouteOptions.TunnelCategory.E)
            .setTruckLength(truckLengthMeters)
            .setTruckHeight(truckHeightMeters)
            .setTruckTrailersCount(numberOfTrailers)

So I created an enum class:

enum class Hazmat : EnumSet<RouteOptions.HazardousGoodType!> {
COMBUSTABLE {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        COMBUSTABLE
        return iterator()
    }

    override val size: Int
        get() = 0
},
CORROSIVE {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        CORROSIVE
        return iterator()
    }

    override val size: Int
        get() = 1
},
EXPLOSIVE {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        EXPLOSIVE
        return iterator()
    }

    override val size: Int
        get() = 2
},
FLAMMABLE {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        FLAMMABLE
        return iterator()
    }

    override val size: Int
        get() = 3
},
GAS {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        GAS
        return iterator()
    }

    override val size: Int
        get() = 4
},
HARMFUL_TO_WATER {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        HARMFUL_TO_WATER
        return iterator()
    }

    override val size: Int
        get() = 5
},
ORGANIC {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        ORGANIC
        return iterator()
    }

    override val size: Int
        get() = 6
},
OTHER {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        OTHER
    return iterator()}

    override val size: Int
        get() = 7
},
POISON {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        POISON
        return iterator()
    }

    override val size: Int
        get() = 8
},
RADIOACTIVE {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        RADIOACTIVE
    return iterator()
    }

    override val size: Int
        get() = 9
},
POISONOUS_INHALATION {
    override fun iterator(): MutableIterator<RouteOptions.HazardousGoodType> {
        POISONOUS_INHALATION
    return iterator()
    }

    override val size: Int
        get() = 10
}

}

If I keep the ! in EnumSet<RouteOptions.HazardousGoodType!> I get the error: Unexpected token. When I change the code to

        routeOptions.truckShippedHazardousGoods = Hazmat.COMBUSTABLE

it shows: public final enum class Hazmat : EnumSet<RouteOptions.HazardousGoodType> for the Hazmat and gets the correct entry and const for COMBUSTABLE.

But like I said, I get the Unexpected error token when I leave the ! in the EnumSet<RouteOptions.HazardousGoodType!>

if I take the ! out I get this error: Enum class cannot inherit from classes This type has a constructor, and thus must be initialized here

I'm not sure what I'm missing? I've looked on the HERE.com docs I found this API ref but it also leaves out the ! from the EnumSet<RouteOptions.HazardousGoodType!> but the link also just uses the Enum<RouteOptions.HazardousGoodType> not the EnumSet. I've tried the Enum but it still gave an error when for mismatch type Hazmat found and EnumSet<RouteOptions.HazardousGoodType!> required. any ideas?

EDIT!! So I tried this and it seems to work right:

val hazmatCorrosive: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.CORROSIVE
                )

                val hazmatExplosive: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.EXPLOSIVE
                )

                val hazmatFlammable: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.FLAMMABLE
                )

                val hazmatGas: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.GAS
                )

                val hazmatHarmfulToWater: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.HARMFUL_TO_WATER
                )

                val hazmatOther: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.OTHER
                )

                val hazmatOrganic: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.ORGANIC
                )

                val hazmatPoison: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.POISON
                )

                val hazmatPoisonousInhilation: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.POISONOUS_INHALATION
                )

                val hazmatRadioactive: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.RADIOACTIVE
                )

                val hazmatCombustable: EnumSet<RouteOptions.HazardousGoodType> = EnumSet.of(
                        RouteOptions.HazardousGoodType.COMBUSTIBLE
                )


                MapLoader.getInstance().selectDataGroup(MapPackage.SelectableDataGroup.TruckAttributes)

                val coreRouter = CoreRouter()

                val routePlan = RoutePlan()

                val routeOptions = RouteOptions()
                routeOptions.transportMode = RouteOptions.TransportMode.TRUCK
                routeOptions.setHighwaysAllowed(true)

                routeOptions.truckHeight = truckHeightMeters
                routeOptions.truckLength = truckLengthMeters
                routeOptions.truckWidth = truckWidthMeters
                routeOptions.truckLimitedWeight = truckLimitedWeightTon
                routeOptions.truckTrailersCount = numberOfTrailers


                if (combustable or corrosive or explosive or flammable or gas or harmfulToWater or other or organic or poisonousInhilation or poison or radioactive) {
                    try {
                        routeOptions.truckShippedHazardousGoods = if (combustable) {
                            hazmatCombustable
                        } else if (corrosive) {
                            hazmatCorrosive
                        } else if (explosive) {
                            hazmatExplosive
                        } else if (flammable) {
                            hazmatFlammable
                        } else if (gas) {
                            hazmatGas
                        } else if (harmfulToWater) {
                            hazmatHarmfulToWater
                        } else if (other) {
                            hazmatOther
                        } else if (organic) {
                            hazmatOrganic
                        } else if (poison) {
                            hazmatPoison
                        } else if (poisonousInhilation) {
                            hazmatPoisonousInhilation
                        } else if (radioactive) {
                            hazmatRadioactive
                        } else return

                    } catch (e: Exception) {
                        e.printStackTrace()
                    }

                    Toast.makeText(this, "Hazmat routing selected. If you're not currently hauling hazmat, update the vehicle profile.", Toast.LENGTH_LONG).show()
                }
                routeOptions.speedProfile
                routeOptions.isTruckDifficultTurnsAllowed = false
                routeOptions.truckRestrictionsMode = RouteOptions.TruckRestrictionsMode.NO_VIOLATIONS
                routeOptions.routeType = RouteOptions.Type.FASTEST
                routeOptions.routeCount = 1

is there a better way to do this?

1

There are 1 best solutions below

0
On

The Error is self-explanatory as it expects input type of Enumset whereas you are putting value of a single Enum. Let’s Understand this better

An EnumSet is a specialized Set collection to work with enum classes and should always be preferred over any other Set implementation when we are storing enum values.

If we want to create an EnumSet with a subset of the enum elements we can use the overloaded of() methods.

Please have a look at the parameter input type it is having input type of Enumset of Enum RoutingOptions.HazardousGoodType.

So it might be possible that truck might be carrying multiple hazardous materials so to accommodate this functionality the input parameter is an Enumset.

In this case as I can see you have used only one enum value so same can be used to create an Enumset and then pass it to the method.

Please have a look at the example below to differentiate better.

public RouteOptions setTransportMode(RouteOptions.TransportMode mode) This method expects input parameter as single Enum value.

public RouteOptions setTruckShippedHazardousGoods(@NonNull java.util.EnumSet<RouteOptions.HazardousGoodType> types) This method expects input parameter as EnumSet of Enum value(s)