Skip to content

Commit e84ca18

Browse files
authored
Merge pull request #76 from szeiger/wip/scala-2.12.0-rc1-compat
Provide compatibility with current Scala 2.12.x (close to RC1)
2 parents 6bf2436 + c7bf67b commit e84ca18

File tree

5 files changed

+54
-93
lines changed

5 files changed

+54
-93
lines changed

src/main/scala/scala/compat/java8/PrimitiveIteratorConversions.scala

+12-12
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ object PrimitiveIteratorConverters {
4141
/** Packages a `scala.collection.Iterator[Double]` as a `java.util.PrimitiveIterator.OfDouble` */
4242
def fromScala(it: Iterator[Double]): PrimitiveIterator.OfDouble = new PrimitiveIterator.OfDouble {
4343
def hasNext = it.hasNext
44-
def next() = it.next()
44+
override def next() = it.next().asInstanceOf[java.lang.Double]
4545
def nextDouble() = it.next()
46-
def remove() { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
47-
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]) {
46+
override def remove() { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
47+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]) {
4848
while (it.hasNext) c.accept(it.next)
4949
}
50-
def forEachRemaining(c: java.util.function.DoubleConsumer) {
50+
override def forEachRemaining(c: java.util.function.DoubleConsumer) {
5151
while (it.hasNext) c.accept(it.next)
5252
}
5353
}
@@ -62,13 +62,13 @@ object PrimitiveIteratorConverters {
6262
/** Packages a `scala.collection.Iterator[Int]` as a `java.util.PrimitiveIterator.OfInt` */
6363
def fromScala(it: Iterator[Int]): PrimitiveIterator.OfInt = new PrimitiveIterator.OfInt {
6464
def hasNext = it.hasNext
65-
def next() = it.next()
65+
override def next() = it.next().asInstanceOf[java.lang.Integer]
6666
def nextInt() = it.next()
67-
def remove() { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
68-
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Integer]) {
67+
override def remove() { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
68+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Integer]) {
6969
while (it.hasNext) c.accept(it.next)
7070
}
71-
def forEachRemaining(c: java.util.function.IntConsumer) {
71+
override def forEachRemaining(c: java.util.function.IntConsumer) {
7272
while (it.hasNext) c.accept(it.next)
7373
}
7474
}
@@ -83,13 +83,13 @@ object PrimitiveIteratorConverters {
8383
/** Packages a `scala.collection.Iterator[Long]` as a `java.util.PrimitiveIterator.OfLong` */
8484
def fromScala(it: Iterator[Long]): PrimitiveIterator.OfLong = new PrimitiveIterator.OfLong {
8585
def hasNext = it.hasNext
86-
def next() = it.next()
86+
override def next() = it.next().asInstanceOf[java.lang.Long]
8787
def nextLong() = it.next()
88-
def remove() { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
89-
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Long]) {
88+
override def remove() { throw new UnsupportedOperationException("remove on scala.collection.Iterator") }
89+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Long]) {
9090
while (it.hasNext) c.accept(it.next)
9191
}
92-
def forEachRemaining(c: java.util.function.LongConsumer) {
92+
override def forEachRemaining(c: java.util.function.LongConsumer) {
9393
while (it.hasNext) c.accept(it.next)
9494
}
9595
}

src/main/scala/scala/compat/java8/collectionImpl/Stepper.scala

+10-10
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private[collectionImpl] class ProxySpliteratorViaTry[A](underlying: TryStepper[A
227227

228228
/** Any `AnyStepper` combines the functionality of a Java `Iterator`, a Java `Spliterator`, and a `Stepper`. */
229229
trait AnyStepper[A] extends Stepper[A] with java.util.Iterator[A] with Spliterator[A] with StepperLike[A, AnyStepper[A]] {
230-
def forEachRemaining(c: java.util.function.Consumer[_ >: A]) { while (hasNext) { c.accept(next) } }
230+
override def forEachRemaining(c: java.util.function.Consumer[_ >: A]) { while (hasNext) { c.accept(next) } }
231231
def hasStep = hasNext()
232232
def knownSize = getExactSizeIfKnown
233233
def nextStep = next
@@ -267,12 +267,12 @@ private[collectionImpl] object AnyStepper {
267267

268268
/** A `DoubleStepper` combines the functionality of a Java `PrimitiveIterator`, a Java `Spliterator`, and a `Stepper`, all specialized for `Double` values. */
269269
trait DoubleStepper extends Stepper[Double] with java.util.PrimitiveIterator.OfDouble with Spliterator.OfDouble with StepperLike[Double, DoubleStepper] {
270-
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]) { while (hasNext) { c.accept(java.lang.Double.valueOf(nextDouble)) } }
271-
def forEachRemaining(c: java.util.function.DoubleConsumer) { while (hasNext) { c.accept(nextDouble) } }
270+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Double]) { while (hasNext) { c.accept(java.lang.Double.valueOf(nextDouble)) } }
271+
override def forEachRemaining(c: java.util.function.DoubleConsumer) { while (hasNext) { c.accept(nextDouble) } }
272272
def hasStep = hasNext()
273273
def knownSize = getExactSizeIfKnown
274274
def nextStep = nextDouble
275-
def tryAdvance(c: java.util.function.Consumer[_ >: java.lang.Double]): Boolean = if (hasNext) { c.accept(java.lang.Double.valueOf(nextDouble)); true } else false
275+
override def tryAdvance(c: java.util.function.Consumer[_ >: java.lang.Double]): Boolean = if (hasNext) { c.accept(java.lang.Double.valueOf(nextDouble)); true } else false
276276
def tryAdvance(c: java.util.function.DoubleConsumer): Boolean = if (hasNext) { c.accept(nextDouble); true } else false
277277
def tryStep(f: Double => Unit): Boolean = if (hasNext) { f(nextDouble); true } else false
278278
def trySplit() = substep
@@ -283,12 +283,12 @@ trait DoubleStepper extends Stepper[Double] with java.util.PrimitiveIterator.OfD
283283

284284
/** An `IntStepper` combines the functionality of a Java `PrimitiveIterator`, a Java `Spliterator`, and a `Stepper`, all specialized for `Int` values. */
285285
trait IntStepper extends Stepper[Int] with java.util.PrimitiveIterator.OfInt with Spliterator.OfInt with StepperLike[Int, IntStepper] {
286-
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Integer]) { while (hasNext) { c.accept(java.lang.Integer.valueOf(nextInt)) } }
287-
def forEachRemaining(c: java.util.function.IntConsumer) { while (hasNext) { c.accept(nextInt) } }
286+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Integer]) { while (hasNext) { c.accept(java.lang.Integer.valueOf(nextInt)) } }
287+
override def forEachRemaining(c: java.util.function.IntConsumer) { while (hasNext) { c.accept(nextInt) } }
288288
def hasStep = hasNext()
289289
def knownSize = getExactSizeIfKnown
290290
def nextStep = nextInt
291-
def tryAdvance(c: java.util.function.Consumer[_ >: java.lang.Integer]): Boolean = if (hasNext) { c.accept(java.lang.Integer.valueOf(nextInt)); true } else false
291+
override def tryAdvance(c: java.util.function.Consumer[_ >: java.lang.Integer]): Boolean = if (hasNext) { c.accept(java.lang.Integer.valueOf(nextInt)); true } else false
292292
def tryAdvance(c: java.util.function.IntConsumer): Boolean = if (hasNext) { c.accept(nextInt); true } else false
293293
def tryStep(f: Int => Unit): Boolean = if (hasNext) { f(nextInt); true } else false
294294
def trySplit() = substep
@@ -299,12 +299,12 @@ trait IntStepper extends Stepper[Int] with java.util.PrimitiveIterator.OfInt wit
299299

300300
/** A `LongStepper` combines the functionality of a Java `PrimitiveIterator`, a Java `Spliterator`, and a `Stepper`, all specialized for `Long` values. */
301301
trait LongStepper extends Stepper[Long] with java.util.PrimitiveIterator.OfLong with Spliterator.OfLong with StepperLike[Long, LongStepper] {
302-
def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Long]) { while (hasNext) { c.accept(java.lang.Long.valueOf(nextLong)) } }
303-
def forEachRemaining(c: java.util.function.LongConsumer) { while (hasNext) { c.accept(nextLong) } }
302+
override def forEachRemaining(c: java.util.function.Consumer[_ >: java.lang.Long]) { while (hasNext) { c.accept(java.lang.Long.valueOf(nextLong)) } }
303+
override def forEachRemaining(c: java.util.function.LongConsumer) { while (hasNext) { c.accept(nextLong) } }
304304
def hasStep = hasNext()
305305
def knownSize = getExactSizeIfKnown
306306
def nextStep = nextLong
307-
def tryAdvance(c: java.util.function.Consumer[_ >: java.lang.Long]): Boolean = if (hasNext) { c.accept(java.lang.Long.valueOf(nextLong)); true } else false
307+
override def tryAdvance(c: java.util.function.Consumer[_ >: java.lang.Long]): Boolean = if (hasNext) { c.accept(java.lang.Long.valueOf(nextLong)); true } else false
308308
def tryAdvance(c: java.util.function.LongConsumer): Boolean = if (hasNext) { c.accept(nextLong); true } else false
309309
def tryStep(f: Long => Unit): Boolean = if (hasNext) { f(nextLong); true } else false
310310
def trySplit() = substep

src/test/java/scala/runtime/java8/BoxingTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class BoxingTest {
99
@Test
1010
public void nullBoxesInterpretedAsZeroF1() {
11-
JFunction1 jFunction1 = new JFunction1$mcII$sp() {
11+
JFunction1$mcII$sp jFunction1 = new JFunction1$mcII$sp() {
1212
@Override
1313
public int apply$mcII$sp(int v1) {
1414
return v1 + 1;
@@ -20,7 +20,7 @@ public void nullBoxesInterpretedAsZeroF1() {
2020

2121
@Test
2222
public void nullBoxesInterpretedAsZeroF2() {
23-
JFunction2 jFunction2 = new JFunction2$mcIII$sp() {
23+
JFunction2$mcIII$sp jFunction2 = new JFunction2$mcIII$sp() {
2424
@Override
2525
public int apply$mcIII$sp(int v1, int v2) {
2626
return v1 + v2 + 1;

src/test/java/scala/runtime/java8/LambdaTest.java

+30-49
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,20 @@
77
import scala.runtime.*;
88

99
import static junit.framework.Assert.assertEquals;
10-
import static scala.runtime.java8.JFunction.*;
1110
import static scala.runtime.java8.TestAPI.*;
1211

1312
import org.junit.Test;
1413

1514

1615
public class LambdaTest {
16+
/*
17+
// This version is for Scala 2.12.0-RC1 and is not compatible with 2.11. It's commented out to allow cross-building.
1718
@Test
1819
public void lambdaDemo() {
19-
// Not allowed with Scala 2.10 nor 2.11
20-
// "incompatible types: Function1 is not a functional interface"
21-
// scala.Function1<String, String> f = (String s) -> s;
22-
23-
// Function1 is not a functional interface because it has abstract
24-
// methods in addition to apply, namely `compose` and `andThen`
25-
// (which are implemented in Scala-derived subclasses with mixin
26-
// inheritance), and the specialized variants of apply (also provided
27-
// by scalac.)
28-
29-
// That's a pity, but we can get pretty close with this library!
30-
31-
// We have to tell javac to use `JFunction1` as the functional interface.
32-
JFunction1<String, String> f1 = (String s) -> s;
20+
scala.Function1<String, String> f1 = (String s) -> s;
3321
3422
// That's more or less equivalent to the old, anonymous class syntax:
35-
new JFunction1<String, String>() {
23+
new scala.Function1<String, String>() {
3624
public String apply(String s) { return s; }
3725
};
3826
@@ -47,80 +35,73 @@ public void lambdaDemo() {
4735
// F1 is a subclass of Function1:
4836
scala.Function1<String, String> f2 = f1;
4937
50-
// Factory methods in `JFunction` can reduce the verbosity a little:
51-
// `func` is actually just an identity method; it only exists to
52-
// trigger lambda creation using the `JFunction1` functional interface.
53-
scala.Function1<String, String> f3 = func((String s) -> s);
54-
55-
// Note that javac's type inference can infer the parameter type here,
56-
// based on the acribed type of `f4`.
57-
scala.Function1<String, String> f4 = func(s -> s);
58-
59-
// f1.apply("");
38+
scala.Function1<String, String> f3 = (String s) -> s;
39+
scala.Function1<String, String> f4 = s -> s;
6040
6141
// Specialized variants of the `apply` method are implenented in the
6242
// functional interface
63-
JFunction1<Integer, Integer> f5 = (i) -> -i;
43+
scala.Function1<Integer, Integer> f5 = (i -> -i);
6444
assert(f5.apply(1) == -1);
6545
assert(f5.apply$mcII$sp(1) == -1);
6646
6747
// as are `curried`, `tupled`, `compose`, `andThen`.
6848
f3.compose(f3).andThen(f3).apply("");
69-
scala.Function2<String, String, String> f6 = func((s1, s2) -> join(s1, s2));
49+
scala.Function2<String, String, String> f6 = ((s1, s2) -> join(s1, s2));
7050
assert(f6.curried().apply("1").apply("2").equals("12"));
7151
72-
// Functions returning unit must use the `JProcedure1`, ... functional interfaces
73-
// in order to convert a void lamdba return to Scala's Unit.
74-
//
75-
// The easiest way to do this is via `JFunction.proc`, ....
52+
// Functions returning unit must return BoxedUnit.UNIT explicitly.
7653
//
7754
// Note that the lambda has a return type of `void` if the last
7855
// statement is a call to a `void` returning method, or if it is
7956
// a `return`.
80-
scala.Function1<String, BoxedUnit> f7 = proc(s -> sideEffect());
81-
scala.Function1<String, BoxedUnit> f8 = proc(s -> {s.toUpperCase(); return;});
57+
scala.Function1<String, BoxedUnit> f7 = (s -> { sideEffect(); return scala.runtime.BoxedUnit.UNIT; });
58+
scala.Function1<String, BoxedUnit> f8 = (s -> { s.toUpperCase(); return scala.runtime.BoxedUnit.UNIT; });
8259
8360
// Function0 is also available
84-
scala.Function0<String> f9 = func(() -> "42");
61+
scala.Function0<String> f9 = (() -> "42");
8562
assert(f9.apply().equals("42"));
8663
8764
// You can go up to 22 (the highest arity function defined in the Scala standard library.)
88-
assert(acceptFunction1(func(v1 -> v1.toUpperCase())).equals("1"));
89-
acceptFunction1Unit(proc(v1 -> sideEffect()));
90-
acceptFunction1Unit(proc(v1 -> {v1.toUpperCase(); return;}));
65+
assert(acceptFunction1((v1 -> v1.toUpperCase())).equals("1"));
66+
acceptFunction1Unit((v1 -> {sideEffect(); return scala.runtime.BoxedUnit.UNIT;}));
67+
acceptFunction1Unit((v1 -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT;}));
9168
92-
assert(acceptFunction2(func((v1, v2) -> join(v1, v2))).equals("12"));
93-
acceptFunction2Unit(proc((v1, v2) -> {v1.toUpperCase(); return;}));
69+
assert(acceptFunction2(((v1, v2) -> join(v1, v2))).equals("12"));
70+
acceptFunction2Unit(((v1, v2) -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT;}));
9471
95-
assert(acceptFunction3(func((v1, v2, v3) -> join(v1, v2, v3))).equals("123"));
96-
acceptFunction3Unit(proc((v1, v2, v3) -> {v1.toUpperCase(); return;}));
72+
assert(acceptFunction3(((v1, v2, v3) -> join(v1, v2, v3))).equals("123"));
73+
acceptFunction3Unit(((v1, v2, v3) -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT;}));
9774
98-
assert(acceptFunction22(func((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -> join(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22))).equals("12345678910111213141516171819202122"));
99-
acceptFunction22Unit( proc((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -> {v1.toUpperCase(); return;}));
75+
assert(acceptFunction22(((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -> join(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22))).equals("12345678910111213141516171819202122"));
76+
acceptFunction22Unit( ((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -> {v1.toUpperCase(); return scala.runtime.BoxedUnit.UNIT;}));
10077
}
78+
*/
10179

10280
/*
10381
// The JFunctions in 2.12.0-M4 are not Serializable anymore
10482
@Test
10583
public void isSerializable() {
106-
scala.runtime.java8.JFunction0<String> f0 = () -> "foo";
84+
scala.Function0<String> f0 = () -> "foo";
10785
assertEquals("foo", SerializationUtils.clone(f0).apply());
10886
109-
scala.runtime.java8.JFunction1<String, String> f1 = (a) -> a.toUpperCase();
87+
scala.Function1<String, String> f1 = (a) -> a.toUpperCase();
11088
assertEquals("FOO", SerializationUtils.clone(f1).apply("foo"));
11189
112-
scala.runtime.java8.JFunction2<String, String, String> f2 = (a, b) -> a + b;
90+
scala.Function2<String, String, String> f2 = (a, b) -> a + b;
11391
assertEquals("foobar", SerializationUtils.clone(f2).apply("foo", "bar"));
11492
115-
scala.runtime.java8.JFunction3<String, String, String, String> f3 = (a, b, c) -> a + b + c;
93+
scala.Function3<String, String, String, String> f3 = (a, b, c) -> a + b + c;
11694
assertEquals("foobarbaz", SerializationUtils.clone(f3).apply("foo", "bar", "baz"));
11795
}
11896
*/
11997

98+
/*
99+
// This version is for Scala 2.12.0-RC1 and is not compatible with 2.11. It's commented out to allow cross-building.
120100
private static scala.concurrent.Future<Integer> futureExample(
121101
scala.concurrent.Future<String> future, scala.concurrent.ExecutionContext ec) {
122-
return future.map(func(s -> s.toUpperCase()), ec).map(func(s -> s.length()), ec);
102+
return future.map(s -> s.toUpperCase(), ec).map(s -> s.length(), ec);
123103
}
104+
*/
124105

125106
private static void sideEffect() {
126107
}

src/test/java/scala/runtime/java8/SpecializedFactoryTest.java

-20
This file was deleted.

0 commit comments

Comments
 (0)