Testing Kotlin with Spock Part 2 – Enum instance method
The enum class with instance method in Kotlin is quite similar to its Java version, but they are look a bit different Read more
The enum class with instance method in Kotlin is quite similar to its Java version, but they are look a bit different Read more
sealed abstract class Color(red: Double, green: Double, blue: Double)
object Color extends EnumOf[Color] {
case object Red extends Color(1, 0, 0)
case object Green extends Color(0, 1, 0)
case object Blue extends Color(0, 0, 1)
case object White extends Color(0, 0, 0)
case object Black extends Color(1, 1, 1)
}
Color.values shouldEqual List(Red, Green, Blue, White, Black)
Color.valueOfOpt("Blue").value shouldEqual Blue
Color.valueOfOpt("NotExisiting").isEmpty shouldBe true
case class DistanceFrom(srcCity: String, srcCoordinates: Coordinate) extends EnumOf[DistanceBetween] {
case object ToBerlin extends DistanceFromSrcCityTo("Berlin", Coordinate(52.5075419, 13.4251364))
case object ToNewYork extends DistanceFromSrcCityTo("New York", Coordinate(40.7033127, -73.979681))
abstract class DistanceFromSrcCityTo(val destCity: String, val destCoordinates: Coordinate) extends DistanceBetween {
override def srcCoordinates: Coordinate = DistanceFrom.this.srcCoordinates
}
}
sealed abstract class DistanceBetween {
def srcCoordinates: Coordinate
def destCity: String
def destCoordinates: Coordinate
def inKm: Int = Coordinate.distanceInKm(srcCoordinates, destCoordinates).toInt
}
val DistanceFromWarsaw = DistanceFrom("Warsaw", Coordinate(52.232938, 21.0611941))
DistanceFromWarsaw.ToBerlin.inKm shouldEqual 519
DistanceFromWarsaw.ToNewYork.inKm shouldEqual 6856
DistanceFromWarsaw.values.map(_.inKm) shouldEqual List(519, 6856)
@XmlType(name ="ServiceType") @XmlEnum public enum ServiceType { @XmlEnumValue("stationary") STATIONARY("stationary"), @XmlEnumValue("mobile") MOBILE("mobile"); private final String value; ServiceType(String v) { value = v; } public String value() { return value; } public static ServiceType fromValue(String v) { for (ServiceType c: ServiceType.values()) { if (c.value.equals(v)) { return c; } } throw new IllegalArgumentException(v); } }"No big deal", you say. I beg to differ. What I wanted to achieve was a simple construction which would look like this when used for another Enum (CommonEnumTypeHandler is the name of my generic converter):
public class ServiceTypeHandler extends CommonEnumTypeHandler { }Unfortunately due to the fact, that Java does not have reified generics, which is described in multiple places, I had to stick with passing through a Class type of my enum. So it looks like this:
public class ServiceTypeHandler extends CommonEnumTypeHandler { public ServiceTypeHandler() { super(ServiceType.class); } }My final class has to look like this one below:
import java.sql.SQLException; import com.ibatis.sqlmap.client.extensions.ParameterSetter; import com.ibatis.sqlmap.client.extensions.ResultGetter; import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback; public abstract class CommonEnumTypeHandler implements TypeHandlerCallback { Class enumClass; public CommonEnumTypeHandler(Class clazz) { this.enumClass = clazz; } public void setParameter(ParameterSetter ps, Object o) throws SQLException { if (o.getClass().isAssignableFrom(enumClass)) { ps.setString(((T) o).name().toUpperCase()); } else throw new SQLException("Excpected " + enumClass + " object than: " + o); } public Object getResult(ResultGetter rs) throws SQLException { Object o = valueOf(rs.getString()); if (o == null) throw new SQLException("Unknown parameter type: " + rs.getString()); return o; } public Object valueOf(String s) { return Enum.valueOf(enumClass, s.toUpperCase()); } }