Skip to content

New Java interop snippet with both Java and Scala. #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions _includes/bullet-java-interop.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
<figure class="code">
<figcaption>Java interoperability</figcaption>
<pre><code>import java.io.{PrintStream,FileOutputStream}
<div class="java-interop-wrapper" style="width: 500px">
<figure class="code java-interop1" style="margin-top: 20px;">
<figcaption>Author.scala</figcaption>
<pre><code>class Author(val firstName: String,
val lastName: String) extends Comparable[Author] {

val people: Array[Person]
override def compareTo(that: Author) = {
val lastNameComp = this.lastName compareTo that.lastName
if (lastNameComp != 0) lastNameComp
else this.firstName compareTo that.firstName
}
}

// Create Java output streams for writing to `log.txt`.
val ps = new PrintStream(new FileOutputStream("log.txt"))
object Author {
def loadAuthorsFromFile(file: java.io.File): List[Author] = ???
}</code></pre>
</figure>
<figure class="code java-interop2" style="margin-top: 10px;">
<figcaption>App.java</figcaption>
<pre><code>import static scala.collection.JavaConversions.asJavaCollection;

public class App {
public List&lt;Author&gt; loadAuthorsFromFile(File file) {
return new ArrayList&lt;Author&gt;(asJavaCollection(
Author.loadAuthorsFromFile(file)));
}

// Print contents of `people` to `log.txt`.
people foreach { p =>
ps.println(s"${p.name} is ${p.age} years old") // uses string interpolation
public void sortAuthors(List&lt;Author&gt; authors) {
Collections.sort(authors);
}

public void displaySortedAuthors(File file) {
List&lt;Author&gt; authors = loadAuthorsFromFile(file);
sortAuthors(authors);
for (Author author : authors) {
System.out.println(
author.lastName() + ", " + author.firstName());
}
}
}</code></pre>
</figure>
</div>

<div class="snippet-explanation" style="width: 370px;">
<h3>Combine Scala and Java seamlessly</h3>
<p>Scala classes are ultimately JVM classes. You can create Java objects, call
their methods and inherit from Java classes transparently from Scala.
Similarly, Java code can reference Scala classes and objects.</p>
<p>
In this example, the scala class <code>Author</code> implements the Java
interface <code>Comparable&lt;T&gt;</code> and work with Java
<code>File</code>s. The Java code uses a method from the companion object
<code>Author</code>, and accesses fields of the <code>Author</code> class.
It also uses <code>JavaConversions</code> to convert between Scala collections
and Java collections.
</p></div>
26 changes: 13 additions & 13 deletions resources/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ a.brand {
height: inherit;
vertical-align: 6px;
text-align: left;
-webkit-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-moz-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-ms-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-o-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-webkit-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-moz-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-ms-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-o-transition: all 300ms cubic-bezier(.6, 0, .6, 1);
transition: all 300ms cubic-bezier(.6, 0, .6, 1);
-webkit-transform: translateX(-115px);
-moz-transform: translateX(-115px);
-ms-transform: translateX(-115px);
-o-transform: translateX(-115px);
-webkit-transform: translateX(-115px);
-moz-transform: translateX(-115px);
-ms-transform: translateX(-115px);
-o-transform: translateX(-115px);
transform: translateX(-115px);
}
#download-button:hover .slider {
Expand All @@ -258,10 +258,10 @@ a.brand {
vertical-align: -7px;
}
#download-button:hover span {
-webkit-transform: translateX(0);
-moz-transform: translateX(0);
-ms-transform: translateX(0);
-o-transform: translateX(0);
-webkit-transform: translateX(0);
-moz-transform: translateX(0);
-ms-transform: translateX(0);
-o-transform: translateX(0);
transform: translateX(0);
}
#download-button:hover .icon {
Expand Down Expand Up @@ -2403,7 +2403,7 @@ div#toc ul > li + ul > li { margin-top: 12px; }
margin-bottom: 10px;
}

.traits, .concurrency {
.traits, .concurrency, .java-interop-wrapper {
float: left;
}

Expand Down