Skip to content

Commit b4c682d

Browse files
committed
New Java interop snippet with both Java and Scala.
1 parent 5cc6e4a commit b4c682d

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

_includes/bullet-java-interop.html

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
1-
<figure class="code">
2-
<figcaption>Java interoperability</figcaption>
3-
<pre><code>import java.io.{PrintStream,FileOutputStream}
1+
<div class="java-interop-wrapper" style="width: 500px">
2+
<figure class="code java-interop1" style="margin-top: 20px;">
3+
<figcaption>Author.scala</figcaption>
4+
<pre><code>class Author(val firstName: String,
5+
val lastName: String) extends Comparable[Author] {
46

5-
val people: Array[Person]
7+
override def compareTo(that: Author) = {
8+
val lastNameComp = this.lastName compareTo that.lastName
9+
if (lastNameComp != 0) lastNameComp
10+
else this.firstName compareTo that.firstName
11+
}
12+
}
613

7-
// Create Java output streams for writing to `log.txt`.
8-
val ps = new PrintStream(new FileOutputStream("log.txt"))
14+
object Author {
15+
def loadAuthorsFromFile(file: java.io.File): List[Author] = ???
16+
}</code></pre>
17+
</figure>
18+
<figure class="code java-interop2" style="margin-top: 10px;">
19+
<figcaption>App.java</figcaption>
20+
<pre><code>import static scala.collection.JavaConversions.asJavaCollection;
21+
22+
public class App {
23+
public List&lt;Author&gt; loadAuthorsFromFile(File file) {
24+
return new ArrayList&lt;Author&gt;(asJavaCollection(
25+
Author.loadAuthorsFromFile(file)));
26+
}
927

10-
// Print contents of `people` to `log.txt`.
11-
people foreach { p =>
12-
ps.println(s"${p.name} is ${p.age} years old") // uses string interpolation
28+
public void sortAuthors(List&lt;Author&gt; authors) {
29+
Collections.sort(authors);
30+
}
31+
32+
public void displaySortedAuthors(File file) {
33+
List&lt;Author&gt; authors = loadAuthorsFromFile(file);
34+
sortAuthors(authors);
35+
for (Author author : authors) {
36+
System.out.println(
37+
author.lastName() + ", " + author.firstName());
38+
}
39+
}
1340
}</code></pre>
1441
</figure>
42+
</div>
43+
44+
<div class="snippet-explanation" style="width: 370px;">
45+
<h3>Combine Scala and Java seamlessly</h3>
46+
<p>Scala classes are ultimately JVM classes. You can create Java objects, call
47+
their methods and inherit from Java classes transparently from Scala.
48+
Similarly, Java code can reference Scala classes and objects.</p>
49+
<p>
50+
In this example, the scala class <code>Author</code> implements the Java
51+
interface <code>Comparable&lt;T&gt;</code> and work with Java
52+
<code>File</code>s. The Java code uses a method from the companion object
53+
<code>Author</code>, and accesses fields of the <code>Author</code> class.
54+
It also uses <code>JavaConversions</code> to convert between Scala collections
55+
and Java collections.
56+
</p></div>

resources/css/main.css

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,15 @@ a.brand {
233233
height: inherit;
234234
vertical-align: 6px;
235235
text-align: left;
236-
-webkit-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
237-
-moz-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
238-
-ms-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
239-
-o-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
236+
-webkit-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
237+
-moz-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
238+
-ms-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
239+
-o-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
240240
transition: all 300ms cubic-bezier(.6, 0, .6, 1);
241-
-webkit-transform: translateX(-115px);
242-
-moz-transform: translateX(-115px);
243-
-ms-transform: translateX(-115px);
244-
-o-transform: translateX(-115px);
241+
-webkit-transform: translateX(-115px);
242+
-moz-transform: translateX(-115px);
243+
-ms-transform: translateX(-115px);
244+
-o-transform: translateX(-115px);
245245
transform: translateX(-115px);
246246
}
247247
#download-button:hover .slider {
@@ -258,10 +258,10 @@ a.brand {
258258
vertical-align: -7px;
259259
}
260260
#download-button:hover span {
261-
-webkit-transform: translateX(0);
262-
-moz-transform: translateX(0);
263-
-ms-transform: translateX(0);
264-
-o-transform: translateX(0);
261+
-webkit-transform: translateX(0);
262+
-moz-transform: translateX(0);
263+
-ms-transform: translateX(0);
264+
-o-transform: translateX(0);
265265
transform: translateX(0);
266266
}
267267
#download-button:hover .icon {
@@ -2403,7 +2403,7 @@ div#toc ul > li + ul > li { margin-top: 12px; }
24032403
margin-bottom: 10px;
24042404
}
24052405

2406-
.traits, .concurrency {
2406+
.traits, .concurrency, .java-interop-wrapper {
24072407
float: left;
24082408
}
24092409

0 commit comments

Comments
 (0)