Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:26:22+00:00 2026-05-30T14:26:22+00:00

Problem details. I need to create a framework to perform various checks, like: –

  • 0

Problem details. I need to create a framework to perform various checks, like:
– is Date A between dates B and C?
– is Integer A greater than Integer B and smaller than Integer C?
etc.
So far, i am thinking of two possible implementations, detailed bellow.

Impl1 – using a single class to perform the checks, based on the check type.

import java.sql.Time;
import java.util.Date;

public class SearchManager {

    public final static int SEARCH_TYPE_DATE = 0;
    public final static int SEARCH_TYPE_INT = 1;
    public final static int SEARCH_TYPE_STRING = 2;
    public final static int SEARCH_TYPE_TIME = 3;

    private final int searchType;

    public SearchManager(int searchType) {
        this.searchType = searchType;
    }

    public final boolean doCompare(Object minValue, Object maxValue, Object toBeCompared) {
        switch (this.searchType) {
            case SEARCH_TYPE_DATE: {
                return compareDates((Date) minValue, (Date) maxValue, (Date) toBeCompared);
            }
            case SEARCH_TYPE_INT: {
                return compareIntegers((Integer) minValue, (Integer) maxValue, (Integer) toBeCompared);
            }
            case SEARCH_TYPE_STRING: {
                return compareStrings(String.valueOf(minValue), String.valueOf(maxValue), String.valueOf(toBeCompared));
            }
            case SEARCH_TYPE_TIME: {
                return compareTimes((Time) minValue, (Time) maxValue, (Time) toBeCompared);
            }
            default:
                return false;
        }
    }

    private boolean compareDates(Date min, Date max, Date toBeCompared) {
        boolean result = false;
        // actual comparison
        return result;
    }

    private boolean compareIntegers(Integer min, Integer max, Integer toBeCompared) {
        boolean result = false;
        // actual comparison
        return result;
    }

    private boolean compareStrings(String min, String max, String toBeCompared) {
        boolean result = false;
        // actual comparison
        return result;
    }

    private boolean compareTimes(Time min, Time max, Time toBeComparedDate) {
        boolean result = false;
        // actual comparison
        return result;
    }
}

Impl2 – Using an abstract class or interface, and having an implementation of the comparison method for each search type.

public abstract class AbstractSearch {

public final static int SEARCH_TYPE_DATE = 0;
public final static int SEARCH_TYPE_INT = 1;
public final static int SEARCH_TYPE_STRING = 2;
public final static int SEARCH_TYPE_TIME = 3;

public AbstractSearch() {
    super(); //just for fun
}

protected abstract boolean doCompare(Object minValue, Object maxValue, Object toBeComparedValue);

}

Now, in this example, for X different search types, as you can imagine, X implementations of the AbstractSearch will be created.

Just imagine yourself that the class AbstractSearch from the 2nd implementation will need to perform additional tasks, other than the method doCompare(..) and that is why an interface is not my 1st candidate for this solution, and to write something like

public abstract class AbstractSearch implements Searcheable

would not help me a lot, since AbstractSearch or SearchManager will handle ALL the comparisons, and, if a new comparison type should be needed, an additional type/subclass implementation will be declared for corresponding super classes from Impl1 or Impl2.

My question is about which implementation is faster? And this is very important, since the comparison process will be called in loops containing thousands of elements.
Thank you for reading/answering my question.

EDIT1: Also, please have in mind the fact that minValue and maxValue will be extracted from the classes that extends the AbstractSearch, for the second example, or classes extending SearchManager, as for the 1st example. These implementation will actually be graphical components allowing the user to enter a minimum and a maximum value, and then, these value will be compared in a loop with some bean property, of objects displayed in a table.

EDIT2: I am doing some benchmarks, with dummy implementations (i just want to compare the method call time vs switch execution time). The results are..surprising:

  • Using AbstractSearch (500k loops): -0.047 seconds
  • Using SearchManager (500k loops): -0.422 seconds

Having these results, it is safe to assume that using inheritance is much faster than using a switch (or even worse an if-else test) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:26:24+00:00Added an answer on May 30, 2026 at 2:26 pm

    If you want to make this code as fast as possible, also try using overloaded methods like this:

    public final static boolean doCompare(Date min, Date max, Date toCompare) {
      // ...
    }
    public final static boolean doCompare(int min, int max, int toCompare) {
      // ...
    }
    // ...and so on
    

    At compile time, the compiler will generate a direct call to the appropriate method, based on the types which you pass. (If you are passing Object references which might point to an instance of any of the 4 types, this won’t work.)

    If the values which you are comparing are ints, passing them to a method which takes Object arguments will require boxing and unboxing, which adds overhead.

    If performance is really important, I recommend you use static methods, since they are a bit faster in many Java implementations.

    Also, rather than using compareTo, you can probably squeeze out a bit more performance by using your own inline code for the comparisons.

    EDIT: You said in the edited question that min and max will actually be passed in by a subclass of SearchManager. In that case I would make SearchManager abstract, and put different implementations of doCompare in each subclass of SearchManager. What I said about static methods won’t work in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I get into the details of this problem, I'd like to make the
So I've come across the following problem. I need to create a dinamic menu
This problem has been solved thanks to your suggestions. See the bottom for details.
I have recently stumbled upon a problem with selecting relationship details from a 1
I'm currently working on my own PHP Framework, and I need some help figuring
I'm having a problem to create a generic View to represent NotFound pages. The
I need to create a crystal report that shows the address label of the
I am trying to create WCF Data service project using Entity Framework. ( I
I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |
I won't go into the boring details of why I need this, it's part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.