Error codes lookup - I

23 May 2008

There are sometimes situations, when you need some look up mechanism. I will present few example in the next few posts.

Let me present an scenario. We have an application which reads and writes to files on a file system. We have defined a error generating and tracking mechanism as well. Each error is assigned an error code and that code is thrown in case an error is encountered. To Interpret the code, we need to have some lookup mechanism.

I defined a final class with the error code mapping in a HashMap.

public final class ErrorCodes {
 
	private  HashMap hashMap = new HashMap();
 
	public ErrorCodes() {
 
		hashMap.put("00", "No error.");
		hashMap.put("10", "File locked.");
		hashMap.put("20","File does not exist.");
		hashMap.put("30","Folder does not exist.");
		hashMap.put("40","File is Read Only.");
		hashMap.put("30","File read error");
		hashMap.put("50","Unknown error");
 
	}
 
	public  HashMap getHashMap() {
		return hashMap;
	}
}

Now we need to lookup through the HashMap to get the error details.

ErrorCodes codes = new ErrorCodes();
FileApplication fileApp = new FileApplication();
int code = fileApp.read(arrayList,"filename.txt");
System.out.println("Code retrieved: " + codes.getHashMap().get(code));

Its a simple of of retrieving code details from the map.

del.icio.us:Error codes lookup - I  digg:Error codes lookup - I  spurl:Error codes lookup - I  wists:Error codes lookup - I  simpy:Error codes lookup - I  newsvine:Error codes lookup - I  blinklist:Error codes lookup - I  furl:Error codes lookup - I  reddit:Error codes lookup - I  fark:Error codes lookup - I  blogmarks:Error codes lookup - I  Y!:Error codes lookup - I  smarking:Error codes lookup - I  magnolia:Error codes lookup - I  segnalo:Error codes lookup - I  gifttagging:Error codes lookup - I

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Error codes lookup - I

One Response to “Error codes lookup - I”

  1. Mumbly Joe Says:

    Problems:
    1) Returning HashMap instead of Map
    2) Making class final, why?

    Bugs:
    1) Accessing Map with an int key.

Leave a Reply