Solved

Redesign Java Logic to limit String literal comparisons

Posted on 2012-04-06
5
221 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

829 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