This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Public methods and automatic SUN testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Public methods and automatic SUN testing" Watch "Public methods and automatic SUN testing" New topic
Author

Public methods and automatic SUN testing

Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi,

I'm just working on my locking scheme and would like to use the CRUD-DB methods in a specific way (pseudo-code):


But what about the public methods update, delete and read? Some weirdo could come up and call these methods
without following my documentation (in choices and javaDoc). Is it enough if I provide these documentation or can't I use
this approach at all?
Maybe one more comment about JavaDoc. Can I change the comments provided by Sun in the interface or can I at least add
some comments (here about the calling scheme) to the method comments?
Oh, there is another question. I don't want to reuse deleted records for creating new records, because of the KISS (keep it simple stupid)
principle. But the comments for the create method say, "possibly reusing a deleted entry". Should I explain why I didn't reuse them
or does it violated the heart of my assessor?

For the people still reading
Thanks for answering then
cheers
Bob

SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5121
    
  12

Hi Bob,

first of all: no need for deleteRoom, createRoom, deleteRoom,... only findRooms and bookRoom are required by the assignment

Simple and easy answers to your questions:
- my data class should be first initialised, so javadoc of my init-method says "must be called before any other method". So if you try to read before call to init, you will get an IllegalStateException (as also documented in the javadoc of the read-method). So wrong usage of your api (according to the javadoc) is not your problem, but problem of the one who is abusing your api.
- I decided to leave sun's interface as is and provide all javadoc in my own interface (extending sun's interface). I just added a small javadoc comment at class level and that was no problem, so I think you are just fine as long as you don't change method signatures (or add/remove methods)
- if you don't decide to reuse deleted entries, just document it in your choices.txt and you'll be fine too (and the kiss-principle is a good argumentation). I reused deleted entries and that was 8 lines extra code, but each statement was still easy and simple.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi Roel,

that was fast. Thanks for answering.

Roel De Nijs wrote:
first of all: no need for deleteRoom, createRoom, deleteRoom,... only findRooms and bookRoom are required by the assignment

Yeah, I know. I just wanted to show how my scheme for using these methods work.

Roel De Nijs wrote:
Simple and easy answers to your questions:
- my data class should be first initialised, so javadoc of my init-method says "must be called before any other method". So if you try to read before call to init, you will get an IllegalStateException (as also documented in the javadoc of the read-method). So wrong usage of your api (according to the javadoc) is not your problem, but problem of the one who is abusing your api.

So how does your read-method knows that another method should be called first to throw the IllegalStateException? Do you have some boolean variable like (lockMethodCalled) or something. Did I miss something?

Roel De Nijs wrote:
- I decided to leave sun's interface as is and provide all javadoc in my own interface (extending sun's interface). I just added a small javadoc comment at class level and that was no problem, so I think you are just fine as long as you don't change method signatures (or add/remove methods)

You have your own interface. Mmmh, would be also a nice solution. Have to check this for my assignment. I "must implement the" Sun interface in a class called Data.java. I think that would allow an own interface: Data.java implements myOwnInteface.java extends SunsInteface.java.
But I guess I'll just add some more comments at my Data.java file.

Roel De Nijs wrote:
- if you don't decide to reuse deleted entries, just document it in your choices.txt and you'll be fine too (and the kiss-principle is a good argumentation). I reused deleted entries and that was 8 lines extra code, but each statement was still easy and simple.

Did you use a map for the deleted entries or did you just loop through the record cach (I guess you used a record cache) searching for the first "missing"
entry?

Thanks for the help.
Bob
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, y'all.

Just one little thought here. Even though the assignment only requires the update and find methods, you still have to implement all the other methods. And they should do what is described in the comments provided in the interface provided by Sun. That's why there's no risk to call your methods and something unexpected happening.

Just like my good buddy Roel, my Data class also needs to be initialized, and if you call a method before initializing it, you also get an IllegalStateException.

Bob Wheeler wrote:So how does your read-method knows that another method should be called first to throw the IllegalStateException? Do you have some boolean variable like (lockMethodCalled) or something. Did I miss something?


In my case, if the cache is empty, it means it hasn't been initialized.

Bob Wheeler wrote:You have your own interface. Mmmh, would be also a nice solution. Have to check this for my assignment. I "must implement the" Sun interface in a class called Data.java. I think that would allow an own interface: Data.java implements myOwnInteface.java extends SunsInteface.java.
But I guess I'll just add some more comments at my Data.java file.


Again, Roel and I had the same approach. Even though my Data class implements my own interface, there's no problem, since it extends Sun's interface.

Bob Wheeler wrote:Did you use a map for the deleted entries or did you just loop through the record cach (I guess you used a record cache) searching for the first "missing" entry?


In my case, I have a List<Integer> that keeps the numbers of the deleted records, so when creating a record, I see if there's any entry there; if so, this is the key of the new record in the cache of records; if not, the new record is saved at the end of the file.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5121
    
  12

Hi Bob,

All my design decisions are according this 2 big principles:
  • don't do anything that's not required
  • keep everything simple, easy and understandable for a junior programmer


  • So I used a record cache together with Data being a singleton (so that's the easiest and simplest approach as far as I know). It's easy to see when it's not initialized: like Roberto said if record cache is not initialized or (my solution) if your database-path is not initialized (because you have to store it, so you'll be able to write everything back at the end )

    My map with records has 2 different possible: it's a record number with a string array or a record number with null. The rest of the implementation is food for your brain because like I said at the start: easy, simple and understandable for a junior programmer.

    Kind regards,
    Roel
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2258
        
        3

    Roel De Nijs wrote:My map with records has 2 different possible: it's a record number with a string array or a record number with null.


    Same thing here (great minds, remember? ). But in my case, I have a Map<Integer, Room>.
    Bob Wheeler
    Ranch Hand

    Joined: Apr 24, 2009
    Posts: 317
    Hi Bob,

    [EDIT: wrote this before the last two comment due to slow e-mail ???]
    also thanks for answering here. Beer for all !!!

    Roberto Perillo wrote:Howdy, y'all.

    Just one little thought here. Even though the assignment only requires the update and find methods, you still have to implement all the other methods. And they should do what is described in the comments provided in the interface provided by Sun. That's why there's no risk to call your methods and something unexpected happening.

    So writing something in the JavaDoc is not enough, the comments from the interface should also be followed?

    Roberto Perillo wrote:
    Just like my good buddy Roel, my Data class also needs to be initialized, and if you call a method before initializing it, you also get an IllegalStateException.

    Ok, you throw that IllegalStateException if the database file hasn't been read. And if the database file hasn't been read the record cache is empty. Yeah, easy to check.

    Roberto Perillo wrote:
    Bob Wheeler wrote:So how does your read-method knows that another method should be called first to throw the IllegalStateException? Do you have some boolean variable like (lockMethodCalled) or something. Did I miss something?

    In my case, if the cache is empty, it means it hasn't been initialized.

    Do you talk now about the lock cache (I keep the current locks in a cache, so I could check there if the current record number is locked)?
    If the method read is called w/o calling the lock method first, the record number entry is missing in the locked cache. So I'll check this and throw
    a RuntimeException. Do I follow your here or is it cold My interface comments would be satisfied.

    Roberto Perillo wrote:
    Bob Wheeler wrote:Did you use a map for the deleted entries or did you just loop through the record cach (I guess you used a record cache) searching for the first "missing" entry?

    In my case, I have a List<Integer> that keeps the numbers of the deleted records, so when creating a record, I see if there's any entry there; if so, this is the key of the new record in the cache of records; if not, the new record is saved at the end of the file.


    Sounds easy, smooth and funky.

    Thanks and cheers
    Bob
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2258
        
        3

    Howdy, Bob! There you go!

    Bob Wheeler wrote:So writing something in the JavaDoc is not enough, the comments from the interface should also be followed?


    Yes. But this should be pretty simple. One thing I think is hidden there is related to compatibility with other systems. Let's say that somewhere there's a method (and this is not explicity in the assignment, it's just a thought of mine) that expects the interface provided by Sun. Let's say that someone wants to use your implementation of this interface; the behavior should be predictable. So this should be important regarding compatibility and reusability.

    Bob Wheeler wrote:Do you talk now about the lock cache


    Oh, no. I have two caches. One for the database records (that's the one), and another one for the locked records.

    Bob Wheeler wrote:Sounds easy, smooth and funky.


    Okey Dokey!
    Bob Wheeler
    Ranch Hand

    Joined: Apr 24, 2009
    Posts: 317


    All my design decisions are according this 2 big principles:
    # don't do anything that's not required
    # keep everything simple, easy and understandable for a junior programmer

    That's my man, that's Roel


    Roberto Perillo wrote:

    Yes. But this should be pretty simple. One thing I think is hidden there is related to compatibility with other systems. Let's say that somewhere there's a method (and this is not explicity in the assignment, it's just a thought of mine) that expects the interface provided by Sun. Let's say that someone wants to use your implementation of this interface; the behavior should be predictable. So this should be important regarding compatibility and reusability.

    Ok. I got it. Thanks.

    Roberto Perillo wrote:
    Bob Wheeler wrote:Do you talk now about the lock cache

    Oh, no. I have two caches. One for the database records (that's the one), and another one for the locked records.

    What I think I have to do for the update method (look below) in case of using the update method (pseudo-code).
    What do you think?


    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5121
        
      12

    Hi Bob,


    I would think the bracket at the end of this line is not needed, otherwise your complete update method does nothing, because it only gets executed when room number is not in cache. Makes no sense at all in my opinion...

    My update-method does similar tests as yours, so I think you're fine with that implementation.

    Kind regards,
    Roel
    Bob Wheeler
    Ranch Hand

    Joined: Apr 24, 2009
    Posts: 317
    Roel De Nijs wrote:

    I would think the bracket at the end of this line is not needed, otherwise your complete update method does nothing, because it only gets executed when room number is not in cache. Makes no sense at all in my opinion...

    You are right. I will delete that, so other user don't get annoyed. Thanks. Was only Pseudo-Code anyway.

    Roel De Nijs wrote:
    My update-method does similar tests as yours, so I think you're fine with that implementation.

    Thanks for your help. I will drink some German beers on you and Bob tonight

    cheers
    Bob

    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5121
        
      12

    Bob Wheeler wrote:Was only Pseudo-Code anyway.

    Your pseudo-code is looking a lot like Java code ;)

    An example of my pseudo-code (on a very high level) would be:


    Bob Wheeler wrote:I will drink some German beers on you and Bob tonight

    Prost! Cheers! But I would advice to ignore the German beers and try some Belgian ones

    Kind regards,
    Roel
    Bob Wheeler
    Ranch Hand

    Joined: Apr 24, 2009
    Posts: 317
    Roel De Nijs wrote:
    Bob Wheeler wrote:I will drink some German beers on you and Bob tonight

    Prost! Cheers! But I would advice to ignore the German beers and try some Belgian ones

    You have misunderstood me. I thanked you for your advices, so I wanted to do something good for you.
    So no Belgian beers Only good German ones

    cheers
    Bob
    Ps. The creator of a new pseudo code, which looks like Java. lol
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Public methods and automatic SUN testing
     
    Similar Threads
    recNo choice
    Passed URLyBird 1.3.2 (400/400)
    java doc for private members
    NX: URLyBird questions about database
    assistance with javadoc