Error codes lookup - I
23 May 2008There 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.
Related Posts:
- Error codes lookup - II
- JNDI lookup with a corbaname URL
- JNDI - Fixed qualified names
- Handling ASCII character set in Java (I)
- Java Naming and Directory Interface - II
- Using the hibernate objects
- Handling ASCII character set in Java (II)
- Validating ASCII character set
- Asynchronous method calls - I
- Introduction to Hashtable
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
Problems:
1) Returning HashMap instead of Map
2) Making class final, why?
Bugs:
1) Accessing Map with an int key.