Best way to fetch data from database-style table with given row/column/table number - Java

I have several hypothetical 2 - dimensional tables, from which I am to fetch data. I need to make a method that will take in the table id and the "coordinates" of the desired item, and return the item. So far, I have tried making it with multi-layered switches, but I am wondering whether there is any better way to go about this, as the switch code seems too long to be the optimal solution. Any help would be greatly appreciated.

An idea of what my code looks like:

switch(tableId) {
    case "table 1":
        switch(top) {
            case "whatever":
                switch(side) {
                    // et cetera
    case "table 2":
        // etc


Use polymorphism.

Create an interface SearchableTable:

public interface SearchableTable<T> {
    T getItem(int x, int y);

If these tables are under your control, let them implement this interface. Otherwise, wrap the tables with your own wrapper-classes like so:

public class SearchableTableWrapper implements SearchableTable<MyItemType> {

    private final Table wrappedThirdPartyTable;

    public SearchableTableWrapper(Table wrappedThirdPartyTable) {
        this.wrappedThirdPartyTable = wrappedThirdPartyTable;

    public MyItemType getItem(int x, int y) {

Now, in the general class where you want to implement a general method that accepts a table id and indices of the item, accept the table itself and invoke its getItem method, like so:

public class TableUtils {
    public static <T> T getItem(SearchableTable<T> table, int x, int y) {
        return table.getItem(x, y);

If you have to get table id instead of table, just keep a Map from table id to the relevant SearchableTable, like so:

public class TableUtils {

    private static Map<Long, SearchableTable> tableIdToSearchableTable;

    public static <T> T getItem(SearchableTable<T> table, int x, int y) {
        return table.getItem(x, y);

This map can be loaded with the actual SearchableTables in several ways, either via static initializer block or static addTable method or you could turn TableUtils to be non-static at all, whatever fits you best.

The main thing here is to use polymorphism.


You don't need an enum. Your Table1 from your comment should look like this:

public class Table1 implements SearchableTable<String> {
    public String getItem(int x, int y) {
        // use x and y to fetch the item friom the 2-dimensional data structure

You have to rewrite everything in a more object oriented way, one smart way to do it in Java cold be the use of some 'tuned' enums :

enum activity { WHATEVER, SOMETHINGELSE } //Use the same principle as in the enum below ...

enum tables {

  private activity activity;

  tables(activity activity) {
    this.activity = activity;

  public activity activity() {
   return this.activity;

After creating all the needed enum for each needed level, you can use the following "trick" to avoid the long and multilevel switch conditional statement :

String tableId = ...
//Load the table 
tables table = tables.valueOf(tableId);
//Call the related attached activity ...

Of course the enum element must have the same name as the variable name that you want to intercept(the same name that you would have put into the check condition of the if or switch statement). Another similar result can be achieved using a map instead of an enum ... Take a look at the Command Pattern, for further information .

Need Your Help

Testing strategy for large application with few public methods?

unit-testing testing

I'm working on a project which I'm really not sure how to unit test. It's an unobtrusive tag based framework for wiring up events between models, views and delegates in a GUI system.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.