Solved

Redesign Java Logic to limit String literal comparisons

Posted on 2012-04-06
5
206 Views
Last Modified: 2012-06-21
Hello,

I'm trying to redesign the classes below so that the DeviceRowHelper.getColumnValue method doesn't have to use all the String literal comparisons in order to call the correct JNI method (asString, asInteger or asMember).  In an ideal world I would redesign the schema to include a member table and use the Attribute.type to determine which JNI method to call, but I can't do that. Is there any way around using multiple String literal ifs?

Schema .sql file that gets read and parsed into Java objects and attributes:

   
CREATE TABLE DEVICE (
        Id                    VARCHAR(256),
        Unit                  INT,
        MemberTime            VARCHAR(256),
        MemberLevel           VARCHAR(10),
        MemberImportance      VARCHAR(10), 
     );

Open in new window

class that is created by the parser to hold the schema info (not complete code):

   
 public class Schema{
        private final Map<String, CEWTable> tables;
      
        public Attribute getAttribute(String tableName, String colName) {
           Table table = tables.get(tableName.toUpperCase());
           if (table == null) {
              return null;
           }
        
           return table.getColumns().get(colName.toUpperCase());
        }
    }

Open in new window

Class that is created by parser for each column in schema above:

   
 public class Attribute{
        private final String name; //contains values such as Id, Units, memberTime..etc
        private final String type; //contains values such as varchar, int
    
        public Attribute(String name, String type) {
            this.name = name;
            this.type = type;
        }
         
        public String getName(){
            return name;
        }

        public String getType(){
            return type;
        }
    }

Open in new window


   Schema columns that are returned as object from JNI:
   
 public class Member{
        private String time;
        private String level;
        private String importance;
        
       //getters and setters
    }

Open in new window


class that processes sql statement Result Set Row:

   
public class DeviceRowHelper{
    
       private final Schema schema;
       private final ResultSetRow row;
       private final Member member;
       
       public DeviceRowHelper(Schema schema, ResultSetRow row){
           this.schema = schema;
           this.row = row;
       }
     
       public String getColumnValue(columnName){
            if(columnName.equalsIgnoreCase("Id")){
                //calls C function via jni to get value from row as a String
                return JNIClass.getRowValueAsString(columnName, row); 
            }else if(columnName.equalsIgnoreCase("Unit")){
                //calls C function via jni to get value from row as a Integer
                return JNIClass.getRowValueAsInteger(columnName, row).toString(); 
            }else if(columnName.equalsIgnoreCase("MemberTime")){
                if(member == null){
                    initializeMember();
                }
                return member.getTime();
            }else if(columnName.equalsIgnoreCase("MemberLevel")){
                if(member == null){
                    initializeMember();
                }
                return member.getLevel();
            }else if(columnName.equalsIgnoreCase("MemberImportance")){
                if(member == null){
                    initializeMember();
                }
               return member.getImportance();
            }
       }
      
       private void initializeMember(){
           member = JNIClass.getRowValueAsMember();
       }
    
    }

Open in new window

0
Comment
Question by:cgray1223
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 37817188
You could look at using the Factory pattern to avoid a long series of ifs. But you don't have that many at the moment. Are you thinking of having more?
0
 

Author Comment

by:cgray1223
ID: 37817549
@CEHJ, I have more columns and thusly more IFs than what I showed in the example above.  I've thought about using an enum instead.  Grouping all the String columns, Integer columns and then member columns and just loop through each enum...am I really gaining much by getting rid of those literals IFs?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37817815
Well certainly the most elegant and extensible way to do it would be to use a Factory pattern, but you need to make that method homogeneous to do so - at the moment it does (at least) two different things
0
 

Author Comment

by:cgray1223
ID: 37829221
I've requested that this question be deleted for the following reason:

crappy question
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 37829222
Well i don't agree that it's a "crappy question". It's actually a common design problem to have the need to eliminate a long series of if statements, and i have given a common textbook solution, so this would be useful to future visitors to this site and question.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now