• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beginner question about efficency

 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49796
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49796
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic