Thread-safe list and the proxy design pattern in Java
2012-10-23 11:57:22
To make a List (or another collection) thread-safe in Java, the code would be something like:

 
ArrayList notSafe = new ArrayList();
List threadSafe = Collections.synchronizedList(notSafe);
 


"threadSafe" is a proxy to the "notSafe" list, it controls all access made to this object, one can assume that it uses "synchronized" to do so.
Equals, hashcode, hashmap in Java
2012-10-18 16:54:12
I had an argument in an interview recently on how the equals function behave and how it ensures that compared object are "equals".

I said in the interview : "equals is based hashcode comparison function", which look shocking for the techs experts.

I don't think that I was so wrong :
By default on object, equals behave like '=' that is making comparison reference, and by default hashcode is based on object references.
On 32 bit system, any object has a 32 bits reference, so as hashcode() return a 32 bit value from an object, one can assume that there is a direct mapping between object reference and hashcode().

I'm talking about default behavior of hashcode when directly inherited from object:
" The default hashCode() method uses the 32-bit internal JVM (Java Virtual Machine) address of the Object as its hashCode. "
So...yes by transitivity AND by default : equals() uses reference and hashcode uses reference, so equals uses hashcode based on reference...

On the other hand, hashcodes's contract claims that two equals objects MUST have the same hashcode, but different objects can have the same hashcode too.
Therefore the interviewer bring the subject to hashmap behavior : hashmap are using the hashcode value to store an object and equals to resolve collision (that is in case where multiple objects are stored with the same hashcode).
If references are used in a 32 bit system, there's no possible collision. So this point is still valid.

An interesting point is that not to break hashmap behvior, one can also override hashcode to return 0 so that hashmap will degenerate in successive "equals" comparison for any object to retrieve.

I also have to mention a famous booby trap : BigDecimal, can you use equals to compare BigDecimals : you can't because, equals() check the "equality" and 2.00 and 2.0 are on different scale therefore, are not considered as equal objects. To compare two BigDecimal, use the compareTo() method. (=0)
Fastest way to copy an array in Java ?
2012-10-14 11:20:39
To copy an array, one would think about about a simple "for" loop, but this is sub -optimal as dedicated methods in the java framework usually call natives(ie:faster) implementations.

 
>java -cp . -Xint ArrayCopier performance 250000
Using clone: 93 ms
Using System.arraycopy: 110 ms
Using Arrays.copyOf: 187 ms
Using for loop: 422 ms
 


According to this site:
http://www.javapractices.com/topic/TopicAction.do?Id=3
using System.arraycopy is about 4 times faster.
Looking for a lightweight caching framework in Java
2012-10-13 12:41:34
I am thinking about a recurrent problem : caching. A cache if often used to hide access latency when fenching data.
A good caching framework would be able to quickly access its cache lookup, and if data is not avalaible, fetch data (from a web ressource, disk, or even an expensive on-board computation), and put it in its own store (memory or hard drive).

It should be :
* lightweight to fit in an Android app
* Highly configurable (for instance cached data lifetime, data sources...)
* Non-blocking (multi-threaded)
* Handle data update (write-through mechanism)
* Support access concurrency

Whirlycache is a good example :
http://code.google.com/p/whirlycache/

Spring implements some cache features too:
http://blog.springsource.com/2011/02/23/spring-3-1-m1-caching/
Lightweight configuration file loader on Android
2012-10-11 22:42:53
Loading a simple property file in Android is easy, knowing that the java world if full of server side configuration library.
For instance, the first stop would be http://commons.apache.org/configuration/ great for web applications...but a 9 meg jar is kind of too much for an Android app. Let's pull back to java.util.properties.

 
public class Sd3dConfig {
 
private static Properties config;
private static Object lockObj = new Object();
public static String getString(String key)
{
synchronized(lockObj)
{
if (config == null)
{
config = new Properties();    
try
{
config.load(Sd3dRessourceManager.getManager().getRessource("config/sd3d.properties"));
Log.d("sd3d","config/sd3d.properties LOADED "+config.toString());
}
catch (IOException ex)
{
Log.e("sd3d","error while loading sd3d configuration file : "+ex.toString());
Log.e("sd3d","A config/sd3d.properties should be placed in the asset directory");
}
}
}
return config.getProperty(key);
}
}
 
 
Programming workout schedule
2012-10-11 10:50:39
In professionnal programming, speed matters a lot, one can reach a reasonable productivity on any particular tool or language, but the good programmers must be able to deliver quickly on any of the most common needs :

* building a skeleton hello world project
* building a database dao : send request and fetch result mechanisms
* file access
* configuration access (read)
* setup webservice project (server and client)
* setup mvc framework

In addition, I should put my interest into :
C++
* STL
* Boost
* CUDA

C#
* WCF

Java
* Spring IOC/MVC/Security/AOP
* Guice
* Guava
* GWT
* Swing
* Play! framework
* Hibernate

Being able to answer most common interview questions:
* SQL request involving GROUP_BY clause (inner / outer join)
* Difference between collection and list in Java
* Common design pattern knowledge
sd3d released on GitHub !
2012-10-07 00:11:58


Sd3d (for Speedroid3D) is the 3D engine used in all my Android applications BallJump3D, Chessmind3D and other wallpapers projects. It's now released open-source on GitHub.
https://github.com/kobr4/sd3d-android
Tired of Web Languages ? Try Node.js !
2012-10-03 23:59:34

http://nodejs.org

Video: http://www.youtube.com/watch?v=jo_B4LTHi3I

node.js is library that enables building javascript server-side application. Server-side js ?! Well this idea is not as absurd at it sounds. After all, with latest developpement like the Google V8 JIT compilier, javascript run compiled on a virtual machine just as fast as would...let say Java server side application.

Another point is that node.js emphasizes in "non-blocking IO" which is just another fancy word equivalent to "event-based". In other word, node.js applications are build like a set of callbacks which are called upon receiving certain events : for instance, client connection/disconnection, receiving data etc...
Why javascript for this usage ? because javascript is already event oriented in web browser (onClick(), onMouseOver() ...)

node.js developpers are claiming that event-based is scalable and designed for data intensive real-time application.
=> I'm not quite sure of this assertion because, eventually events are polled by a worker loop which is single threaded (could be multi but then time scheduling wouldn't be respected).
=> event-based leads an application not to have a single execution flow but scatter functionnal code accross multiple events and spaghetti style code (if we are in this state, but not in this state then if we receive this event ...etc...). So code would be a pain to maintain.

At least node.js offer an interesting approach to build web applications. And also it claims being used by major IT companies like Microsoft or Google, I'm still waiting to see a real-world large-scale application that will use node.js.
Chrome Web Audio API
2012-08-12 00:29:00
It's was already fairly easy to play audio from any web browser for years. But Google is providing an interesting Web Audio API that allows real-time processing and enables all sort of web based audio processing tool : audio sequencer, vu meter.

http://www.youtube.com/watch?v=ZV_BIGmpfF8
Big Bang Live Wallpaper : another Android wallpaper
2012-05-31 00:02:15


https://play.google.com/store/apps/details?id=com.nicolasmy.bigbangwallpaper

Matter would spark from the void.

Thanks to hardware acceleration, a simple Samsung Galaxy S can handle animating up to 10 000 particles at 60 frames per second.
See 10 older news | Back to the top