Archive for the ‘Computers’ Category
Like the phoenix, from the ashes of Linux Format (Greek version) the brand-new Linux Inside was born. The first volume will be available at 27/1 (Greece only).
Linux Inside is a 100% Greek magazine that is written by the Linux community for the Linux community. It is hosted in http://www.linuxinside.gr/ (in Greek).
Feel free to visit it, tell us your opinion and freely participate in the forums.
Overloading constructors in Java (not only) is good for the overall design. When used effectively it can result with correct and efficient initialisation of a class and avoidance of many lines of boiler-plate code.
Yesterday, I stumbled on the following problem. I wrote a class like the following,
public class Foo { public Foo(Integer i, Double d) { ... } public Foo(Integer i, Float f) { ... } }
which defines a class Foo with two constructors that accept Integer and Double, and Integer and Float.
For some reason (not important right now) I wanted to initialise the first (Integer,Double) with the second parameter set as null.
This resulted as a compiler error, because the compiler actually could not decide which of the two constructors I wanted to use. Whoops … what could I do?
I could introduce a third constructor, with only one parameter and call this. Try it, you will see, that you will end up using part of the initialisation of the two others, which may complicate things (at least it did in my case).
Fascinating, isn’t it? Maybe we need a Null<Double> or Null<Float> to avoid this mess? Maybe the null value should be a generic Null<T> and use it to ensure type safety and instruct the compiler to correctly solve this ambiguity?
I do not know. My real-world problem solved with a third constructor as a work-around and some duplication of code. C’est la vie.
Browsing some source code (actually my own) and I noticed that long is used. The range for this type is from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,808.
Wow, thats a LOT of expected errors. On the other hand, better safe than sorry
.
1 2 3 4 5 6 7 | public void setErrorCount(long e) { this.errors = e; } public long getErrorCount() { return errors; } |
Recently I began coding the Firefox client of the Pithos application. This release include some small fixes, that permits you to copy local files outside your home directory in windows-based systems. I plan to add some cool features in the near future.
A very close friend of mine, Christos KK Loverdos and Apostolos Syropoulos are close to finish their efforts in completing their book for Scala, titled “Steps in Scala”.
Good luck to both of them, keep up the good work, and I will gladly pre-order my copy
.
These days i’m finalising the FIRE regular expression compiler. To add a feature, the need arise to modify the classpath at runtime. It was a surprise for me when i discovered that there is no official way to modify this at runtime. I even created my own URLClassLoader (java.net), and set it as default classloader for my thread, but nothing.
I searched in google a little and found this post on a forum, which provided a class named ClassPathHacker that could add a path or file (jar) to a classpath at runtime. All this by a programmer named Antony Miguel. Well done
.
The source code of the class follows:
import java.net.*; import java.io.*; import java.lang.reflect.*; public class ClassPathHacker { private static final Class[] parameters = new Class[]{URL.class}; public static void addFile(String s) throws IOException { File f = new File(s); addFile(f); } public static void addFile(File f) throws IOException { addURL(f.toURL()); } public static void addURL(URL u) throws IOException { URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader(); Class sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL",parameters); method.setAccessible(true); method.invoke(sysloader,new Object[]{ u }); } catch (Throwable t) { t.printStackTrace(); throw new IOException("Error, could not add URL to system classloader"); } } }
and an example of usage ..
import java.io.*; public class ClassPathAdder { public static void main(String[] args) { try { ClassPathHacker.add(new File("/home/user/newClassPath")); } catch (IOException e) { System.err.println("Error - " + e.toString()); } } }
Cool!





