The Spike covers the following Java 7 features:
- strings in switch statements
- underscores in numeric literals
- catching multiple exceptions
- rethrow exceptions with improved type checking
- type inference for generic instance creation (Diamond)
- try-with-resources statement
Java 7 Spike:
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
public class Java7Spike {
@Test
public void testStringsInSwitch() {
assertEquals("right direction", decideDirection("right"));
assertEquals("left direction", decideDirection("left"));
assertEquals("no direction", decideDirection("no"));
}
private String decideDirection(final String decision) {
switch(decision) {
case "right":
return "right direction";
case "left":
return "left direction";
default:
return "no direction";
}
}
@Test
public void testUnderscoreInNumericLiterals() {
int numericLiteral = 12_30;
numericLiteral++;
assertEquals(1231, numericLiteral);
}
@Test
public void testMultiCatch() {
try {
final String fileText = readFile("src/org/ccd/java/features/testNio.txt");
parseFile(fileText);
}
catch (IOException | ParseException ex) {
throw new IllegalStateException("Error during file handling occured!", ex);
}
}
private String readFile(final String filename) throws IOException {return null;}
private void parseFile(final String content) throws ParseException {}
@Test(expected=FirstException.class)
public void testRethrowExceptionWithTypeChecking()
throws FirstException, SecondException {
try {
simulateFailure();
}
catch (Exception ex) {
throw ex;
}
}
private void simulateFailure() throws FirstException {
throw new FirstException();
}
private class FirstException extends Exception {
private static final long serialVersionUID = -1872200400984813924L;
}
private class SecondException extends Exception {
private static final long serialVersionUID = 7453507163657978623L;
}
@Test
public void testDiamond() {
final List<String> list = new LinkedList<>();
list.add("anEntry");
assertEquals(1, list.size());
}
@Test
public void testTryWithResources() throws IOException {
final Path file = Paths.get("src/org/ccd/java/features/testNio.txt");
try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(file)) {
final ByteBuffer buffer = ByteBuffer.allocate(100_000);
channel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
public void completed(Integer result, ByteBuffer attachment) {
assertEquals(19, result.intValue());
}
public void failed(Throwable exception, ByteBuffer attachment) {
exception.printStackTrace();
}
});
}
}
}
I am looking forward to Java SE 8 which may include further language enhancements, summarized in the Coin Project and additional features like the integration of Closures (JSR 335 / Lambda Project).
Der Rechtshinweis des Java Blog für Clean Code Developer ist bei der Verwendung und Weiterentwicklung des Quellcodes des Blogeintrages zu beachten.
Der Rechtshinweis des Java Blog für Clean Code Developer ist bei der Verwendung und Weiterentwicklung des Quellcodes des Blogeintrages zu beachten.