Skip to content

Commit

Permalink
basic-example: Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed May 3, 2017
1 parent 8ce3efe commit d0ac35b
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 21 deletions.
7 changes: 6 additions & 1 deletion example-parent/basic-example/pom.xml
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.speedment.example</groupId>
<artifactId>example-parent</artifactId>
<version>3.0.7</version>
<version>3.0.8-SNAPSHOT</version>
<relativePath/>
</parent>

Expand Down Expand Up @@ -60,6 +60,11 @@
<version>${speedment.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.speedment.plugins</groupId>
<artifactId>json-stream</artifactId>
<version>${speedment.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
Expand Up @@ -17,13 +17,19 @@
package com.speedment.example.basic_example;

import com.company.sakila.SakilaApplication;
import com.company.sakila.db0.sakila.actor.ActorManager;
import com.company.sakila.db0.sakila.film.Film;
import com.company.sakila.db0.sakila.film.FilmManager;
import com.company.sakila.db0.sakila.language.Language;
import com.company.sakila.db0.sakila.language.LanguageManager;
import com.speedment.common.tuple.Tuples;
import com.speedment.oracle_java_magazine.util.ExampleUtil;
import static com.speedment.oracle_java_magazine.util.ExampleUtil.buildApplication;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;

/**
*
Expand All @@ -34,35 +40,43 @@ public class Join {
private final SakilaApplication app;
private final FilmManager films;
private final LanguageManager languages;
private final ActorManager actors;

public Join() {
app = buildApplication();
films = app.getOrThrow(FilmManager.class);
languages = app.getOrThrow(LanguageManager.class);
actors = app.getOrThrow(ActorManager.class);
}

public static void main(String[] args) {
new Join().run();
}

private void run() {
joinInMap();
oneToMany();
manyToMany();
join();
linked();
}

private void oneToMany() {
ExampleUtil.log("oneToMany");
private void joinInMap() {
ExampleUtil.log("joinInMap");

languages.stream()
.filter(Language.NAME.equal("English"))
.flatMap(films.finderBackwardsBy(Film.LANGUAGE_ID))
.forEach(System.out::println);
Map<Language, List<Film>> languageFilmMap = films.stream()
.collect(
// Apply this foreign key classifier
groupingBy(languages.finderBy(Film.LANGUAGE_ID))
);

languageFilmMap.forEach((l, fl)
-> System.out.format("%s: %s%n", l.getName(), fl.stream().map(Film::getTitle).collect(joining(", ")))
);

}
private void manyToMany() {
ExampleUtil.log("manyToMany");

private void oneToMany() {
ExampleUtil.log("oneToMany");

languages.stream()
.filter(Language.NAME.equal("English"))
Expand All @@ -74,12 +88,27 @@ private void manyToMany() {
private void join() {
ExampleUtil.log("join");

films.stream()
films.stream()
.filter(Film.RATING.in("G", "PG"))
.map(f -> Tuples.of(f, languages.findBy(Film.LANGUAGE_ID, f)))
.map(t2 -> String.format("The film '%s' is in %s", t2.get0().getTitle(), t2.get1().getName()))
.forEachOrdered(System.out::println);

}

private void linked() {
ExampleUtil.log("linked");

Optional<Film> anyFilmInEnglish = languages.stream()
.filter(Language.NAME.equal("english"))
.flatMap(films.finderBackwardsBy(Film.LANGUAGE_ID))
.findAny();

Optional<Language> languageOfFilmWithId42 = films.stream()
.filter(Film.FILM_ID.equal(42))
.map(languages.finderBy(Film.LANGUAGE_ID))
.findAny();

}

}
Expand Up @@ -61,19 +61,19 @@ private void run() {
private void manyToMany() {
ExampleUtil.log("manyToMany");

Map<Actor, List<Film>> filmography = filmActors.stream()
Map<Actor, List<Film>> filmographies = filmActors.stream()
.collect(
groupingBy(actors.finderBy(FilmActor.ACTOR_ID), // Applies the FilmActor to ACTOR classifier
mapping(
films.finderBy(FilmActor.FILM_ID), // Applies the FilmActor to Film finder
toList() // Use a List collector for downstream aggregation.
toList() // Use a List collector for downstream aggregation.
)
)
);



filmography.forEach((a, fl) -> {
filmographies.forEach((a, fl) -> {
System.out.format("%s -> %s %n",
a.getFirstName() + " " + a.getLastName(),
fl.stream().map(Film::getTitle).sorted().collect(toList())
Expand Down
@@ -0,0 +1,85 @@
/**
*
* Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.example.basic_example;

import com.company.sakila.SakilaApplication;
import com.company.sakila.db0.sakila.actor.Actor;
import com.company.sakila.db0.sakila.actor.ActorManager;
import com.company.sakila.db0.sakila.film.Film;
import com.company.sakila.db0.sakila.film.FilmManager;
import com.company.sakila.db0.sakila.film_actor.FilmActor;
import com.company.sakila.db0.sakila.film_actor.FilmActorManager;
import com.speedment.oracle_java_magazine.util.ExampleUtil;
import static com.speedment.oracle_java_magazine.util.ExampleUtil.buildApplication;
import com.speedment.plugins.json.JsonBundle;
import com.speedment.plugins.json.JsonCollector;
import com.speedment.plugins.json.JsonComponent;
import java.util.function.Function;
import java.util.stream.Stream;

/**
*
* @author Per Minborg
*/
public class Json {

private final SakilaApplication app;
private final FilmManager films;
private final FilmActorManager filmActors;
private final ActorManager actors;

public Json() {
app = buildApplication(b -> b.withBundle(JsonBundle.class));
films = app.getOrThrow(FilmManager.class);
filmActors = app.getOrThrow(FilmActorManager.class);
actors = app.getOrThrow(ActorManager.class);
}

public static void main(String[] args) {
new Json().run();
}

private void run() {
json();
}

private void json() {
ExampleUtil.log("json");

final JsonComponent jsonComponent = app.getOrThrow(JsonComponent.class);

Function<Film, Stream<Actor>> mapper = f -> filmActors.findBackwardsBy(FilmActor.FILM_ID, f).map(actors.finderBy(FilmActor.ACTOR_ID));

String json = films.stream()
.collect(JsonCollector.toJson(
jsonComponent.allOf(films)
.remove(Film.FILM_ID)
.remove(Film.DESCRIPTION)
.putStreamer(
"actors", // Declare a new attribute
mapper, // How it is calculated
jsonComponent.noneOf(actors) // How it is formatted
.put(Actor.FIRST_NAME)
)
));


System.out.println(json);

}

}
Expand Up @@ -21,6 +21,7 @@
import com.company.sakila.db0.sakila.film.FilmManager;
import com.speedment.oracle_java_magazine.util.ExampleUtil;
import static com.speedment.oracle_java_magazine.util.ExampleUtil.buildApplication;
import java.util.Optional;

/**
*
Expand All @@ -41,10 +42,22 @@ public static void main(String[] args) {
}

private void run() {
findALongFilm();
countPg13Films();
understandingOptimization();
}

private void findALongFilm() {
ExampleUtil.log("findALongFilm");

Optional<Film> longFilm = films.stream()
.filter(Film.LENGTH.greaterThan(120))
.findAny();

longFilm.ifPresent(System.out::println);

}

private void countPg13Films() {
ExampleUtil.log("countPg13Films");

Expand All @@ -53,10 +66,9 @@ private void countPg13Films() {
.count();

System.out.format(
"There are %d films rated 'PG-13'", count
"There are %d films rated 'PG-13'%n", count
);

System.out.println();
}

private void understandingOptimization() {
Expand All @@ -69,9 +81,7 @@ private void understandingOptimization() {
.sorted()
.count();

System.out.format("Found %d films", count);

System.out.println();
System.out.format("Found %d films%n", count);

}

Expand Down
Expand Up @@ -45,7 +45,7 @@ public static void main(String[] args) {
private void run() {
serveFilms("PG-13", 0).forEach(System.out::println);
serveFilms("PG-13", 1).forEach(System.out::println);
serveFilms("PG-13", 3).forEach(System.out::println);
serveFilms("PG-13", 3).forEach(System.out::println);
}

private static final int PAGE_SIZE = 50;
Expand Down

0 comments on commit d0ac35b

Please sign in to comment.