Java SE 7 Preview: Alex Miller
Java SE 7 Preview: Alex Miller
Alex Miller
Blog: http://tech.puredanger.com
Twitter: http://twitter.com/puredanger
History
Nov, 2008
Feb, 2002 Sept, 2004 Dec, 2006 Java SE 6 ??
J2SE 1.4 J2SE 5.0 Java SE 6 Update 10 Java SE 7
Danny Coward
// org/foo/api/FooFighters.java:
org.foo.api
module org.foo;
package org.foo.api;
org.foo.domain
public class FooFighters {
public static FooFighters newBand() {
org.foo.impl
return new org.foo.impl.FooFightersImpl();
}
}
org.foo.util
Module Development
org.foo
// org/foo/impl/FooFightersImpl.java:
org.foo.api
module org.foo;
package org.foo.impl;
org.foo.domain
module class FooFightersImpl
implements FooFighters {
org.foo.impl
// etc
}
org.foo.util
Module Development
// org/foo/module-info.java:
org.foo
@Version(“2.3”)
@MainClass(“org.foo.Foosball”)
@ImportModules {
org.foo.api
@ImportModule(name=”java.se.core”,
version=”1.7+”)
@ImportModule(name=”org.bar”,
org.foo.domain
version=”1.0”, reexport=”true”)
}
@ExportResources({“org/foo/icons/**})
org.foo.impl
module org.foo;
org.foo.util
Packaging
javac ... // as usual
Extension ESOM
Module B Module C
(2.0) (4.1)
Global Classpath
Module C
(3.2)
OSGi Maven URL
Project Jigsaw
File
WatchService Watchable AttributeView
BasicFile Named
FileRef
AttributeView AttributeView
Directory
FileSystems FileSystem Path Stream
Files
FileStore Paths
FileVisitor
Directory Directory
StreamFilters Stream.Filter
FileVisit
Result
Using Path
import java.nio.file.*;
OutputStream stream =
journal.newOutputStream(CREATE, APPEND);
try {
writeEntry(stream); // normal stuff
} finally {
stream.close();
}
Copying and Moving
import java.nio.file.*;
// Steal secrets
secrets.copyTo(home.resolve("stolenSecrets.txt"));
// Hide secrets
secrets.moveTo(Paths.get("/Users/dvader/secrets.txt"));
Walking Directories
Path music = Paths.get("/Users/amiller/files/music");
// External iterator
DirectoryStream<Path> mp3s =
music.newDirectoryStream(“*.mp3”);
try {
for(Path entry : mp3s)
System.out.println(entry.getName());
} finally {
mp3s.close();
}
// Internal iterator
Files.withDirectory(music, “*.mp3”, new FileAction<Path>() {
public void invoke(Path entry) {
System.out.println(entry.getName());
}
});
Recursive Walk
Path itunes =
Paths.get("/Users/amiller/Music/iTunes/iTunes Music");
System.out.println(root.relativize(file));
}
}
System.out.format("%s %s %s",
PosixFilePermission.toString(perms),
attrs.owner(),
attrs.group());
for(;;) {
key = watcher.take(); // blocks, also can poll
for(WatchEvent<?> ev : key.pollEvents()) {
switch(ev.kind()) {
case ENTRY_CREATE:
Path file = (Path)ev.getContext(); //relative to deploy
// deploy new stuff
case ENTRY_MODIFY: ...
case ENTRY_DELETE: ...
}
}
key.reset(); // reset after processing
}
What else?
• NetworkChannel
• Multicasting
• AsynchronousDatagramChannel (new)
Is this right?
A simple example
Date xmasEve = new Date(2008, 12, 25, 23, 59, 59);
Is this right?
NO!
Correction:
int year = 2008 - 1900;
int month = 12 - 1;
Date xmasEve = new Date(year, month, 25, 23, 59, 59);
Other Problems
• Date, Calendar, SimpleDateFormatter are mutable
+
Local*, Offset*, Zoned*
// Local human-scale (not tied to TimeZone or Instant)
LocalDate myBirthday =
LocalDate.date(1974, MonthOfYear.May, 1);
LocalTime quittingTime = LocalTime.time(17, 0);
LocalDateTime start2008 = LocalDateTime.dateMidnight(
2008, MonthOfYear.JANUARY, 1);
// Create instant
Instant now = clock.instant();
// Filter
Ops.Predicate<Order> isLate = new Ops.Predicate<Order>() {
public boolean op(Order o) {
return o.due() < new Date();
}
};
// Map
Ops.Predicate<Order> daysOverdue =
new Ops.ObjectToInt<Order>() {
public int op(Order o) {
return daysOverdue(o.due());
}
};
ParallelArray
SummaryStatistics<Integer> summary =
orders.withFilter(isLate)
.withMapping(daysOverdue)
.summary();
System.out.println(“overdue: “ + summary.size());
System.out.println(“avg by: “ + summary.average());
Swing App Framework
Session
Storage
Application
Context
Local
Storage SwingWorker
Task Task
Task
Monitor Service
Beans Validation
@Length(max=30)
private String addressline2;
private String zipCode;
private String city;
@Length(max=30) @NotNull
public String getCity() { ... }
• Use annotations
• Project Coin
Strings in switch
static boolean isStooge(String stooge) {
switch(input) {
case “Moe”:
case “Curly”:
case “Larry”:
case “Shemp”:
return true;
default:
return false;
}
}
Enum Comparisons
enum Rank {
LIEUTENANT, CAPTAIN, MAJOR, COLONEL, GENERAL
}
Enum Comparisons
enum Rank {
LIEUTENANT, CAPTAIN, MAJOR, COLONEL, GENERAL
}
try {
...
try {
...
} catch(final Throwable e) {
LOGGER.info(e.getMessage(), e);
throw e;
}
}
Null Handling
Car car = ...
Integer tilt = null;
if(car != null) {
Sunroof sunroof = car.getSunroof()(;
if(sunroof != null) {
tilt = sunroof.getTilt();
}
}
• http://tech.puredanger.com/java7
• http://java7.tumblr.com
Find Me...
• http://tech.puredanger.com
• Twitter: @puredanger