Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

23 September 2008

ActiveObjects: An Easier Java ORM

In my little experience with Hibernate, I had to port code from C# to Java.
While doing so I got rid of dreadful data sets and introduced the use of Hibernate.
Having to import an existing database scherma, I used the Hibernate Tools for Eclipse to generate the required XML configuration files.

In my view choosing Hibernate over data sets has improved a lot the existing code by getting rid of tons of SQL that was scattered all over the place.
Plus it has inforced good decoupling of business and data access logic so that it is now trivial to switch over to a different database vendor.

Apart from some intricacies with native and composite keys, my experience with Hibernate has been pleasant,
The only issue I have with the ORM is its slow startup and the fact that it relies on a ton of external Jars (the same reason why it's quite hard to package Hibernate as an OSGi bundle).

For a future project of the same (small) size, I will definitely look at a new, smaller ORM.
Here are some links:

ActiveObjects: An Easier Java ORM
https://activeobjects.dev.java.net/

13 June 2008

Enum types - Nice Trick!

Effective Java Reloaded (Google I/O Session Videos and Slides)

public enum Ensemble {
SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5),
SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8),
NONET(9), DECTET(10), TRIPLE_QUARTET(12);
private final int numberOfMusicians;
Ensemble(int size) {
numberOfMusicians = size;
}
public int numberOfMusicians() {
return numberOfMusicians;
}
}

23 January 2008

Serial Port and flow control with javax.comm

Sun doesn't provide a javax.comm implementation for Windows and they suggest to use a freely available implementation of their API called RxTx.

But I was having trouble getting the RTS/CTS hardware flow control going, like someone else.
It seems like some CTS events are missed.

At the end I succeeded by adding a wait loop for the CTS to go high before writing each character:

import gnu.io.*;
private SerialPort sPort;

private void osWrite( int val ) throws IOException
{
while( !sPort.isCTS() );

os.write( val );
os.flush();
}


One would expect this to happen in the background, but it doesn't with this particular implementation of javax.comm.

See also: http://en.wikibooks.org/wiki/Serial_Programming:Serial_Java#JavaComm

09 December 2007

Understanding dependency injection and Guice

crazybob.org: Introduction to Guice Video (Redux)

I've just seen the first 20 minutes, but it's sufficient for me to clarify:
1 - the usefulness of dependency injection
2 - how this new framework, designed by Google's Bob Lee, can help reducing the amount of code one needs to write, and more

From now on I will look at factories with suspicion...

19 November 2007

Open Inheritance versus Designed Inheritance

Final means different things depending on where it’s found. If you put it in front of variables, that means ‘constant’. Final variables can just be assigned a value once.

It’s different if it’s put in front of methods and classes. In that case it means that it closes them from being overridden through inheritance. A final class can’t be extended, and a final method can’t be overridden by methods with the same signature in derived classes.

I’ve just discovered that closing or not a class to inheritance is behind two different school of thoughts: Open Inheritance and Designed Inheritance.

Here’s a little introduction to these two different approaches:

http://martinfowler.com/bliki/DesignedInheritance.html

DSP... in Java

Interesting applications, entirely written in Java:

Sphinx-4 A speech recognizer
FreeTTS 1.2 - A speech synthesizer

And something else, that might come useful sometimes :-)
jMATLAB - an interpreter for the matlab language and a plugin for Eclipse

16 November 2007

java.util.zip

I've just discovered that this useful API has been present in Java since version 1.1.
That's good because I don't have to add any additional library to perform the task of zipping, that would steal some additional precious embedded storage room.
XML files are notoriously redundant so the idea of gzipping an XML file before saving it onto flash can lead to significant savings.

The only problem, I had to learn a bit more about the difference in Java between byte-based and character-based streams.
As you probably already know, these two types, byte and char, are different.
Byte is an 8 bit signed type, while char is a 16 bit unsigned byte. The reason for char is to allow support for internalization and localization by carrying character set information.
Some classes use byte based streams, some others char based ones.
The java.util.zip library uses byte based streams, so in order to interface it with character based classes (like an XML parser) I needed to use some sort of bridge classes.


Examples. To Gzip a String:

String str = ...;
ByteArrayOutputStream ba = new ByteArrayOutputStream();
GZIPOutputStream gz = new GZIPOutputStream( ba );
DataOutputStream os = new DataOutputStream( gz );

os.writeBytes( str );
os.close();

byte result[] = ba.toByteArray()



To UnGzip from an InputStream:

InputStream is = ...;
GZIPInputStream igz = new GZIPInputStream( is );
InputStreamReader isr = new InputStreamReader( igz, "ISO-8859-1" );
StringBuilder strb = new StringBuilder();
int c;
while(( c = isr.read() ) != -1 )
{
strb.append( (char)c );
}
String result = strb.toString();

09 November 2007

Java Swing Survival Guide - Software Reality

Java Swing Survival Guide - Software Reality

This is an old but spot-on article that highlights the limitations of Java as a language for desktop applications. It also finds some qualities in it.
We'll see whether the latest development (SwingLabs, JavaFX) can give some hopes to Java as a language for desktop applications.

How to compare a Byte[] and a byte[] ?

I love collections in Java, they're so handy and I too think arrays should be deprecated.
But a lot of functions return just plain arrays.

So I happen to compare an array of objects Byte (returned by an ArrayList) and an array of plain bytes, returned by a ByteArrayOutputStream.
I know an array of Bytes is an array of p... ehm, references so its nature is totally different from the one of an array that contains primitive types.
But I wish there was a method to compare the two, so clever to do boxing/unboxing automatically.