Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Redesign Java Logic to limit String literal comparisons

Posted on 2012-04-06
5
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 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