Solved

Redesign Java Logic to limit String literal comparisons

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unix in java example 9 52
micro services spring boot application error 3 139
Java Loop 6 48
nextBoolean(double p) for Random class 3 35
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

930 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