Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015
1. Wrap your Database in Java 8
Per-Åke Minborg
Carina Dreifeldt
Malin Weiss
Emil Forslund
2. Speedment
Company
• Company started 2010
• Speedment customers:
Fintech, manufacturing, media,
telecom, transport, retail,
publishing and entertainment.
•Office
PaloAlto, California
Gothenburg, Sweden
Open Source Project
• Apache 2, started 2015
• Fully Java 8 compatible
• www.speedment.org, GitHub
3. Bio
Per-Åke Minborg
• Master of Science Sweden
• Inventor and Serial Entrepreneur
• Java >15 years
• Speaker at Meetups and Javaforums around
the globe, (Java-One 2015)
• Java 8 and OpenSource blogger
5. Do You Recognize the following?
• When coding it is complex to connect to a database…..
…then you use an ORM
Source: ZeroTurnaround
6. SOME PROS of Using an ORM:
• You can work with a relational database as if it were object oriented.
• Increased productivity.
• Provides a certain degree of abstraction (you can replace your
DBMS).
7. SOME CONS of Using an ORM:
• Slows down the application
• You must still write SQL, HQL, et. al
• You can not access all Java 8 features
8. How to skip these ORMs and work with
Pure Java 8
• Lambda expressions
• Stream API
• Optional
9. Lambda Expressions
• Anonymous methods can be expressed using the new ”->”
syntax
Deck deck = Deck.createShuffledDeck();
List<Card> cards = deck.findWhere(card ->
card.isHeart() &&
card.getValue() > 10
);
10. Lambda Expressions
• Any single-method interface can be implemented using lambda
• can be annoted with @FunctionalInterface to show this
@FunctionalInterface
interface VectorOperation {
Vector apply(Vector a, Vector b);
}
VectorOperation plus = (a, b) -> new Vector(
a.getX() + b.getX(),
a.getY() + b.getY()
);
11. Lambda Expressions
Functional pointers can also be used with the ”::” syntax
Set<Hero> warriors = typicalParty.where(Hero::isWarrior);
…
interface Hero {
boolean isWarrior();
}
…
class Party {
private Set<Hero> heroes;
public Set<Hero> select(Predicate<Hero> selector) { … }
}
12. Stream API
• Streams are a modern way of working with groups of elements
• Defining a stream is always an O(1)-operation
• The elements are only traversed when used
• Streams can often be executed in parallell
13. Stream API
• Example: Get the average strength of all warriors with axes in the
party
Set<Hero> party = … ;
int averageStrength = party.stream()
.filter(hero -> Archtype.WARRIOR == hero.getArchtype())
.filter(hero -> Weapon.AXE == hero.getWeapon())
.mapToInt(Hero::getStrength)
.average();
By replacing .stream() with .parallelStream() the operation can
be performed using multiple processors
14. Optional
• Null is often described as ”The Billion Dollar Mistake” of modern
programming languages
• Using the Optional-interface we can now describe a value that might or
might not be present in a null-safe way
String getEmailAddress() { // Is always defined
return this.emailAddress;
}
Optional<String> getFirstname() { // Might not exist
return Optional.ofNullable(
this.firstname
);
}
15. Optional
• You can work with Optionals in the same way you work with
streams
Optional<String> firstname = getFirstname()
.filter(fn -> fn.length() > 3)
.filter(fn -> fn.charAt(0) == 'A');
• When the value of an optional is retreived, you can handle every
case
String absoluteFirstname = firstname.getOrDefault("Anonymous");
16. Speedment API
Embrace Java 8 paradigm with stream(), filter(), sort(), limit() etc.
Forget about SQL, JQL
Focus on Business Logic
Focus on what is fun
Develop a Database app within hours
No Query API
Stick to Java 8
23. Configuration
• Groovy script like Gradle
• Config file is placed relative to the POM
dbms {
schema {
table {
name = ”hare";
column {
name = "name";
}
}
}
}
24. Ease of use:
• Single Maven dependency in POM
• Maven plugin for code generation/GUI
• Maven targets
• Works with Gradle
• GUI to automatically derive the groovy config file from existing
databases
25. Conclusions
• Code generation, No boiler plate code
• Focus on the problem at hand!
• Forget about the database
• Use Java 8