-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUsingScala.java
157 lines (137 loc) · 4.06 KB
/
UsingScala.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package demo;
import com.twitter.util.Function;
import scala.collection.JavaConversions;
import scala.collection.Seq;
import scala.collection.Traversable;
public class UsingScala {
/**
* For Scala objects without companions, referring to members is easy.
*/
String someString = CompanionlessObject.someString();
/**
* Scala objects with class companions are the same.
*/
int someInt1 = CompanionedClass.someInt();
/**
* Scala objects with trait companions aren't.
*/
int someInt2 = CompanionedTrait$.MODULE$.someInt();
/**
* We can't directly refer to a Scala object.
*/
CompanionedTrait ct = ObjectAsInstance$.MODULE$;
/**
* Methods with default arguments in Scala require all arguments to be
* provided explicitly.
*/
String twice = DefaultArguments.multiplyString(2, "s");
/**
* If we want the default, we have to rely on an undocumented synthetic
* method (don't do this).
*/
String twiceWithDefault = DefaultArguments.multiplyString(
2,
DefaultArguments.multiplyString$default$2()
);
/**
* Varargs work with the appropriate annotation on the Scala side.
*/
int howManyStrings = ScalaJavaVarargs.countStrings("a", "b", "c");
/**
* Multiple parameter sections in Scala are combined in Java.
*/
String thrice = MultipleParamSections.multiplyString(3, "s");
/**
* In Scala this would be `"foo" :: Nil`, but we have to mangle the operator
* name. Also note that we can't use the `scala.List` type alias.
*/
scala.collection.immutable.List<String> stringList =
scala.collection.immutable.Nil.<String>$colon$colon("foo");
/**
* Ugh, implicits. In Scala this would be `strings.flatten`.
*/
public static scala.collection.GenTraversableOnce<String> flattenSeq(Seq<Seq<String>> strings) {
return strings.<String>flatten(
scala.Predef.<Seq<String>>conforms().andThen(
new Function<
Seq<String>,
scala.collection.GenTraversableOnce<String>
>() {
public scala.collection.GenTraversableOnce<String> apply(Seq<String> seq) {
return seq;
}
}
)
);
}
/**
* Java can't tell that `String => List[String]` is a subtype of
* `String => Seq[String]`.
*/
scala.Function1<String, Seq<String>> fromListFunc(
scala.Function1<String, scala.collection.immutable.List<String>> values
) {
//return values;
return null;
}
/**
* More generally, Java just doesn't get contravariance.
*/
ContravariantClass<Dog> fromAnimal(ContravariantClass<Animal> cca) {
return null;
//return cca;
}
/**
* Or covariance.
*/
CovariantClass<Animal> fromDog(CovariantClass<Dog> ccd) {
return null;
//return ccd;
}
/**
* This doesn't work!
*/
/*public Traversable<String> moreThanThreeChars(Seq<String> xs) {
return xs.filter(
new com.twitter.util.Function<String, scala.Boolean>() {
public scala.Boolean apply(String x) {
return scala.Predef.Boolean2boolean(x.length() > 3);
}
}
);
}*/
/**
* Instead we have to use `Object` for the primitive.
*/
public static Traversable<String> moreThanThreeChars(Seq<String> xs) {
return xs.filter(
new com.twitter.util.Function<String, Object>() {
public Object apply(String x) {
return x.length() > 3;
}
}
);
}
/**
* Converting from Scala to Java.
*/
java.util.List<String> fromScalaList(Seq<String> strings) {
return JavaConversions.seqAsJavaList(strings);
}
/**
* Converting from Java to a Scala mutable collection.
*/
scala.collection.mutable.Buffer<String> toScalaBuffer(java.util.List<String> strings) {
return JavaConversions.asScalaBuffer(strings);
}
/**
* Converting from Java to a Scala immutable collection.
*/
Seq<String> toScalaImmutableSeq(java.util.List<String> strings) {
return com.twitter.util.javainterop.Scala.asImmutableSeq(strings);
}
/**
* Yay! Runtime exceptions!
*/
public static ImplicitClasses.RichInt rich4() { return new ImplicitClasses.RichInt(4); }
}