aspose file tools*
The moose likes Beginning Java and the fly likes beginner question about efficency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner question about efficency" Watch "beginner question about efficency" New topic
Author

beginner question about efficency

leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
hi all!

i have a question about efficency.
i have a program that has to read a lot of data from different files.
to begin with, i have an abstract class called CodedData.
CodedData gets a String[] parametr, and String DataCoded and String DataName variables. like:


now i have couple of data types i extends from CodedData. each data Type should be stored in a different "DataBase" class , contains a HashMap<String, "DataType">.
in the hashmap the string is for the "dataCode" from before, and the dataType is the certain datatype(extendings of the codedData class).
any time i need a certain data i get it from the appropirate database, with its get method, where the keys are the dataCodes.
now, in each database i have to read the right txtfile, and transfer the list of String[] i get (each String[] is a line from the text) to the right dataType

my question is how to do it in the most efficient way, so i need your advice experts.
i mean, should i have an abstract class "CodedDataBase" and extend it each time with the datatype like:

i thought of using factory methods, but i got lost.
any help would be appreciated!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

What kind of "efficiency" did you have in mind? Fewest lines of code? Fastest production of source code? Some kind of run-time optimizations?
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
sorry that i didnt make myself clear.
i want to stick to oop as much as i can, and not have to much code and classes that ill get lost.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11408
    
  16

The best approach it is to write the cleanest, simplest code you can to do what is needed. Only worry about efficiency later if it proves to be a problem.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
this is what i try to achieve...
maybe ill describe my problem better.
i want to read data from couple of files.
each file contains different type of data type.
each data type extends the "CodedData" class i mentioned above, but one data type can read a String[] with 3 variables, and others read String[] with 20 variables.
for example, i have TileData class:


when i create a new Tile for example, i use the TileData, or when i create an Animation i use an AnimationData datatype.
but i dont need TileData\animationData instances.
i want to have a static method inside TileData that gets a List<String[]> and returns a HashMap<String, TileData> (the String is the dataCode).
i want each dataType to have such method.
AnimationData will return HashMap<String, AnimationData>.

my question, is how to write that method only once inside CodedData (if it's possible) so i want have to write the method again and again.

Thanks whoever tries to help, i'm really lost..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
That is a simple inheritance question. You need to write a method which takes a String[] puts the data into a Map (call it Map, not HashMap) and returns it, in the CodedData class, and all its subclasses will inherit it.

I can see potential problems that your String[] might not be in the format you require, and might not contain 4 elements when it is passed to the TileData constructor.
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Thanks Campbell for your answer
the method you suggested does return the hashmap, but it returns it as Map<String, String[]> type.
i need it Map<String, TileData>, or Map<String, AnimationData>, or so depands on the dataType which call the method.

the problem you mentioned with the String[] i read, it is a problem, but what can i do other than catch exception?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
By catching an exception, you probably mean throwing an exception.
You would have to perform some sort of validation
  • Does the array contain 4 elements?
  • Does each element match the format I require?
  • Can I produce a regular expression to test the format of each element?
  • If you cannot validate the array, throw an IllegalArgumentException.

    Do you really need the data to be passed as Strings? That sounds as though you are taking data in a specific format (eg int) and converting them to a generalised format (ie String) and trying to convert them back. It looks complicated; that suggests there must be a simpler way to do it.

    You would probably want to return a Map<String, CodedData>.
    leroy tsruya
    Ranch Hand

    Joined: Sep 24, 2009
    Posts: 57
    Thank you very much for the validating check you provided!
    i dont think it's complicated...
    i read the data from a file. so i can only get the data as String.
    i read the file in that format:

    string,string,string,string
    string,string,string,string

    each line is String[].
    so my dataTypes just each of the String[] fiealds, and store them in variables, so it would be easier to use them later.
    for example, the TileData file is somrthing like that:
    String datcode,String dataname,String isblocked,String imgcode
    then the TileData class just pass the Strings into variables.

    also, if i get the Map<String CodedData> i will have 2 problems:
    1.CodedData is now abstract, i cant create an instance.
    2.i wont have the methods of the subclass which are important for the program. (TileData has getImgCode method and isBlocked method in addition to the codedData methods).

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    As I said before, this is a basic inheritance thing. If CodedData is abstract class, you can still declare it. You can also add those methods as abstract methods.
    If you can't add abstract methods, then you have your inheritance wrong and you ought not to declare TileData extends CodedData.
    leroy tsruya
    Ranch Hand

    Joined: Sep 24, 2009
    Posts: 57
    first, Thank you Campbell for all this help.
    what i try to get, is having a Map<String, TileData> or Map<String, AnimationData> without having to create new instances of these classes.
    to have a static method to return such a map. called
    Map<String, (datatype)> getMap(List<String[] dataList>{}
    i cant use an abstract static method.

    now in each class (tiledata, animationdata) ill have to write the same code for the getMap method, and the only difference would be the class i create.

    the code would be:


    so for each class i have to write the same code and just change the dataType accordingly.
    that was the problem, and i wanted to know if i can do it somehow better.
    Thanks again!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    You can try a generic method, but I still think there is something wrong with your inheritance if you can't return a Map<String, CodedData>.
    leroy tsruya
    Ranch Hand

    Joined: Sep 24, 2009
    Posts: 57
    yeah... something is definetely wrong, however i cant figure out what it is..
    CodedData just supplies a "skeleton" for every datatype i have.
    all data types should have dataCode (the code for retrieving) and dataName (the name of the data).
    then each new datatype which extends CodedData only inherits these 2 variables and 2 getter methods(getDataCode(),getDataName).

    or i should think of another way to achieve it?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: beginner question about efficency