graphhopper / graphhopper

Open source routing engine for OpenStreetMap. Use it as Java library or standalone web server.

Home Page:https://www.graphhopper.com/open-source/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting a EncodedValueFactory fails if the EnumEncodedValue class is not within `com.graphhopper.routing.ev` package

lukasalexanderweber opened this issue · comments

Describe the bug
I needed some time to understand that setting a EncodedValueFactory fails if the EnumEncodedValue class is not within com.graphhopper.routing.ev package.

final StringBuilder importSourceCode = new StringBuilder("import com.graphhopper.routing.ev.*;\n");

To Reproduce

Create a package com.test.publictransport with an EncodedValueFactory (I used GH7):

public class BusEncodedValueFactory extends DefaultEncodedValueFactory {

	public BusEncodedValueFactory() {
		super();
	}

	@Override
	public EncodedValue create(String name, PMap properties) {
		if (Bus.KEY.equals(name)) {
			return new EnumEncodedValue<>(Bus.KEY, Bus.class);
		}  else {
			return super.create(name, properties);
		}

	}
}

and a Bus Enum:

public enum Bus {
	MISSING, YES, DESIGNATED, DESTINATION, NO;

	public static final String KEY = "bus";

	public static EnumEncodedValue<Bus> create() {
		return new EnumEncodedValue<>(Bus.KEY, Bus.class);
	}

	@Override
	public String toString() {
		return Helper.toLowerCase(super.toString());
	}

	public static Bus find(String name) {
		if (name == null)
			return MISSING;
		try {
			return Bus.valueOf(Helper.toUpperCase(name));
		} catch (IllegalArgumentException ex) {
			return MISSING;
		}
	}
}

If you set a BusEncodedValueFactory instance via hopper.setEncodedValueFactory you should get the following error:

java.lang.IllegalStateException: Could not deserialize encoded value: {"className":"com.graphhopper.routing.ev.EnumEncodedValue","name":"bus","bits":3,"min_storable_value":0,"max_storable_value":7,"max_value":4,"negate_reverse_direction":false,"store_two_directions":false,"fwd_data_index":2,"bwd_data_index":0,"fwd_shift":8,"bwd_shift":-1,"fwd_mask":1792,"bwd_mask":0,"enum_type":"com.graphhopper.routing.ev.Bus"}, error: Cannot construct instance of java.lang.Class, problem: com.graphhopper.routing.ev.Bus

Expected behavior

  • the factory should be loaded correctly OR
  • it should be documented e.g. in setEncodedValueFactory that you have to place the enums into com.graphhopper.routing.ev OR
  • give the user the possibility to specify another location for Janino imports