You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
johnynek edited this page Jan 12, 2013
·
3 revisions
Implementing a Bijection
Here's an example of implementing a bijection in Java:
importcom.twitter.bijection.AbstractBijection;
// This is not a mathematically correct bijection for all strings,// since only the subset of canonical representations of Long as valid inputs.// It is here only as a convenient example implementationpublicclassStringToLongextendsAbstractBijection<String,Long> {
@OverridepublicLongapply(Stringa) {
returnLong.parseLong(a);
}
@OverridepublicStringinvert(Longb) {
returnb.toString();
}
}
Accessing default Bijections from Java:
To access default bijections, find them in the scala object Bijection, which is compiled to java bytecode as:
The details of how scala compiles to JVM bytecode are beyond the scope of this document, but the rule is: to access vals/methods of a scala object A then call the method you want on A$.MODULE$.
(Scala <=> Java) Collection Bijections:
See: Collection Bijections for bijections between Iterator/List/Map and more in their Java and Scala versions. This might make Java interop with scala easier.