-1
\$\begingroup\$

What do you think about this read only table design, regarding performance?

package jsql

Main.java

package jsql;

public class Main {
    public static void main(String[] args) {
        Record head = new Record("given name", "family name", "street", "postal code", "city");
        Record jimmy = new Record("James", "Blunt", "Rochesterfield 1", "1234", "Bloomville");
        Record joey = new Record("John", "Smith", "Green Way 5", "5678", "Harrisburg");
        
        Table friends = new Table(head, jimmy, joey);
        
        if(friends.isValid) {
            System.out.println(friends.toString());
        }
        else {
            System.err.println("Error: Invalid table with hash code: " + friends.hashCode());
        }
    }
}

Record.java

package jsql;

class Record {
    final String entry[];
    
    private static final String SEPARATOR = "\t";
    
    public String toString() {
        String temp = this.entry[0];
        for(int i = 1; i < this.entry.length; i++) {
            temp = temp.concat(SEPARATOR);
            temp = temp.concat(this.entry[i]);
        }
        return temp;
    }
    
    Record(String...entry) {
        this.entry = entry;
    }
}

Table.java

package jsql;

class Table {
    final Record record[];
    final boolean isValid;
    
    private static final String NEWLINE = "\n";
    
    public String toString() {
        String temp = this.record[0].toString();
        for(int i = 1; i < this.record.length; i++) {
            temp = temp.concat(NEWLINE);
            temp = temp.concat(this.record[i].toString());
        }
        return temp;
    }
    
    static boolean validate(Table t) {
        for(int i = 1; i < t.record.length; i++) {
            if(t.record[0].entry.length != t.record[i].entry.length) {
                return false;
            }
        }
        return true;
    }
    
    Table(Record...record) {
        this.record = record;
        this.isValid = validate(this);
    }
}
\$\endgroup\$
0
\$\begingroup\$

You should use exceptions to indicate that something fails, rather than just printing something somewhere that might very easily get ignored, while the program goes on with invalid data. Classes should have the responsibility of ensuring valid state (not additional methods or auxiliary attributes).

Basically do the validate in the constructor, and then:

try {
  Table friends = new Table(head, jimmy, joey);
} catch(InvalidRecordLength e) {
  // Do something. This allows you to just print if you don't care about validity too much, but will stop other users from unknowingly continuing with invalid state
}

The header should be stored ideally in a different class (you might want to store constraints on columns, like types), and more importantly in a different place than records[0]. This is because the header is not a record. This will save you from a lot of confusing logic like the one you have in validate(), which would get repeated for every method reading the table.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.