try (Scanner scanner = new Scanner(file).useDelimiter("\\Z")) { fileContent= scanner.next(); }
Wednesday, 31 December 2014
A Scanner quickie
Sometimes I want to read a smallish text file in one gulp without going through all the input stream chaining or using external libraries. This can be done with the Scanner class when the delimiter is set to EOF:
Tuesday, 16 December 2014
Scala 1
Solution to Project Euler Problem 1 in Scala:
Array.range(1, 1000).filter { x => (x % 3 == 0) || (x % 5 == 0) }.sumNice.
Thursday, 11 December 2014
Java type erasure in practice
"Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead."
Yay! No overhead! Lets see how this erasure thingy works in practice. javap will let us peek at the generated code and see what actually is going on. We have:
Case 1
We don't really care about the method body, just the generated method signatures, javap -s produces:
Case 2
It is valid to use a "bounded type parameter" that is, to have an upper bound on the type T can assume. This is expressed by using extends which is sort of combined extends/implements like so:
Case 3
Case 4
and indeed, this time EventListener comes out on top:
Yay! No overhead! Lets see how this erasure thingy works in practice. javap will let us peek at the generated code and see what actually is going on. We have:
>java -version java version "1.8.0_72"
Case 1
public <T> void process(T t) {}
We don't really care about the method body, just the generated method signatures, javap -s produces:
public <T> void process(T); descriptor: (Ljava/lang/Object;)VIn this basic case the parameter T is replaced by Object.
Case 2
It is valid to use a "bounded type parameter" that is, to have an upper bound on the type T can assume. This is expressed by using extends which is sort of combined extends/implements like so:
public <T extends Serializable> void process1(T t) {}Looking at the compiled code we see:
public <T extends java.io.Serializable> void process1(T); descriptor: (Ljava/io/Serializable;)VLooks like the compiler is replacing T with the most specific super class/interface. It should be all good, after all, Java is single inheritance. Except that off course we can implement as many interfaces as we like, which is supported by the generics implementation, and so that we have
Case 3
public <T extends Comparable & EventListener > void process2(T t) {}Now the compiler has to choose, what would that be...
public <T extends java.lang.Comparable & java.util.EventListener> void process2(T); descriptor: (Ljava/lang/Comparable;)VSo Comparable won. Is this because of the ordering?
Case 4
public <T extends EventListener & Comparable> void process3(T t) {}
and indeed, this time EventListener comes out on top:
public <T extends java.util.EventListener & java.lang.Comparable > void process3(T); descriptor: (Ljava/util/EventListener;)VHere is the complete java code:
public class Erasure { public <T> void process(T t) {} public <T extends Serializable> void process1(T t) {} public <T extends Comparable & EventListener> void process2(T t) {} public <T extends EventListener & Comparable> void process3(T t) {} }
Monday, 8 December 2014
GitBash as ConEmu task
The current GitBash installs with the shortcut target:
%USERPROFILE%\AppData\Local\GitHub\GitHub.appref-ms --open-shell
The appref-ms file, is a sort of link variant and unsurprisingly, using it as a parameter to ConEmu task command, doesn't work.
To find out what is actually being invoked when when running Git Shell we use Process Explorer which shows a bash.exe process with the command line :
"%USERPROFILE%\AppData\Local\GitHub\PortableGit_da44d00daa738db527315557813e7b68709ed0a1\bin\bash.exe" --login -i
This looks similar to the standard Cygwin bash invocation, we create a Git task in ConEmu (Win-Alt-P) and use the invocation with additional parameters to ConEmu specifying a new console and a color scheme:
"%USERPROFILE%\AppData\Local\GitHub\PortableGit_da44d00daa738db527315557813e7b68709ed0a1\bin\bash.exe" --login -i -new_console:P:"^<Tomorrow Night Bright>^"
and that's it.
Subscribe to:
Posts (Atom)