Created
July 9, 2015 14:15
-
-
Save chadselph/8474c626cb7a582d1e2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A command ID for SMPP | |
*/ | |
trait FieldHelpers { | |
val className = getClass.getName.dropRight(1) | |
val missingLabel = "unknown" | |
// fragile. also, needs to be lazy for initialization order. | |
lazy val names = getClass.getMethods.toList.filter(m => m.getParameterTypes.isEmpty | |
&& m.getReturnType == classOf[Int] && m.getName != "hashCode").map(m => | |
m.invoke(this).asInstanceOf[Int] -> m.getName).toMap | |
def fieldName(v: Int): String = { | |
val fieldName = names.getOrElse(v, missingLabel) | |
f"$className.$fieldName(0x$v%x)" | |
} | |
} | |
class CommandId(val intValue: Int) extends AnyVal { | |
override def toString(): String = { | |
CommandId.fieldName(intValue) | |
} | |
} | |
object CommandId extends FieldHelpers { | |
val generic_nack = new CommandId(0x80000000) | |
val bind_receiver = new CommandId(0x00000001) | |
val bind_receiver_resp = new CommandId(0x80000001) | |
val bind_transmitter = new CommandId(0x00000002) | |
val bind_transmitter_resp = new CommandId(0x80000002) | |
val query_sm = new CommandId(0x00000003) | |
val query_sm_resp = new CommandId(0x80000003) | |
val submit_sm = new CommandId(0x00000004) | |
val submit_sm_resp = new CommandId(0x80000004) | |
} | |
object Demo extends App { | |
println(CommandId.generic_nack) | |
println(new CommandId(100)) | |
/* | |
CommandId.generic_nack(0x80000000) | |
CommandId.unknown(0x64) | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment