Solved

Redesign Java Logic to limit String literal comparisons

Posted on 2012-04-06
5
228 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 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Title # Comments Views Activity
How do I remove an object from a 3 60
String array comparison 4 55
java example issue 5 63
Tomcat 9 + java 8 error while trying to deploy a war file 2 843
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

739 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