Jan Sterk

Ranch Hand
+ Follow
since Jun 06, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jan Sterk

Page 146:

book wrote:A natural ordering that uses compareTo() is said to be consistent with equals if, and only if, x.equals(y) is true whenever x.compareTo(y) equals 0.



The API doc of Comparable says

API doc wrote:The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C.



That's different. According to the book's rule, a class which equals() method always returns true, is always consistent, even if its compareTo() method does not return 0. According to the API, equals() must return false if compareTo() doesn't return 0 (and true if it does).

Then follows this:


You might be sorting Product objects by name, but names are not unique. Therefore, the return value of compareTo() might not be 0 when comparing two equal Product objects,
so this compareTo() method is not consistent with equals.


(This refers to the case where two Product objects have the same id values, and different name values.)
Its inconsistency does not follow from the rule in the book's quote, but from the API doc's quote.
(It is also inconsistent because of the following case. Say you have two Product objects with different id values, and with equal name values. Then namecompareTo() returns 0, but equals() returns false. This is because of both the book's rule and the API's rule.)

[edit] Reading it over again, I think the only typo in the book's rule is that the phrase if, and only if has to be in two places. It should be:
A natural ordering that uses compareTo() is said to be consistent with equals if, and only if: x.equals(y) is true if, and only if x.compareTo(y) equals 0.
Page 136, at the end:

Shouldn't

When talking about LIFO (stack), people say push/poll/peek.

be

When talking
about LIFO (stack), people say push/pop/peek.

?
I hope I'm not breaking any rules by waking up this old thread.

When synchronizing on the list as in Rob Spoor's message, how is the list protected when another thread modifies the list (e.g. adds an object to it), in a piece of code that is not synchronized on the list. E.g.:




If thread 1 is at point B, and thread 2 takes over then, and modifies the list at point A, doesn't that mean trouble?

The source code of SynchronizedList shows that its methods synchronize on a separate object (called mutex), and not on the list itself.
4 years ago
What's not clear for the beginner (that's the way I experienced it by reading HFSJ), is that all 3 belong to the same category: actions.
Why is something called 'transparent' in OO (e.g. 'that process is completely transparent for other objects') when you don't see what's going on?
Yes this is very confusing stuff. Sun made a mess of the terminology.

There are
* standard actions
* custum tags
* standard tag library

In fact, they all belong to the category 'actions'. In the latest specs, custom tags are indeed called custom actions. This makes more sense, because the only difference between the two is that s.a. are actions that are defined in the specs, and that they are taken care of by the container without the need of importing anything.

The standard tag library consist of actions that do need importation. The difference with custom actions are that they are supplied by Sun. (And also that mentioned in the specs, but that I'm not sure). A more logical name for them would be 'standard custom actions', but that would just bring more cunfusion right now
The solution is to not use one side of the mapping

The program processes an xml file, which contains nodes with a 'type' attribute. One part of the program (the model) passes these types to another part (the view).

To keep things typesafe, I created an enum for the known types. This is were the mapping comes in. Each type from the xml (a String) maps to an enum.

The program reads the types, stores them in a List, and at a later point creates new nodes with those types.

My first approach was to store the enum that belongs to the type xml-String, and store that enum in the List. But then when a new node has to be created, the enum has to be translated back to the xml-String. That's where I thought a bi-directional mapping was needed.

One solution is not to store the enums, but the xml-Strings as they're read from the xml. When they have to be passed to the view-part, they can be translated to the enums at that moment. It has another advantage - it seperates the type that is used for reading/writing the xml, from the type that is used by the view, which is a good thing. So actually, the String-enum mapping is not one-to-one: several Strings can potentially map to the same enum.

Now that I think of it, I may be able to get rid of the xmlString-to-enum hashmap. Maybe I can store the String in the enum class.. That would be better OO - the String and enum relationship is then encapsulated in one class. I'm not an enum-expert, I'll have to check it out.
[ October 20, 2008: Message edited by: Jan Sterk ]
11 years ago
Thanks for your replies.

Originally posted by Jade Jadaaih:
...
Could you please let me know what exactly is your requirement?



While I was trying for an hour or so to write down what the exact problem was, I found the solution
11 years ago
With a Hashmap you can get a value for a given key. But you can't get a key that belongs to a (unique) value.

Does such a class exist? E.g. if you want to associate an English name with its French translation.
11 years ago
I've build a GUI that has several different JComponents: JLists, JPanels, JTextAreas etc. Now I'm looking for a good way to set a few properties (e.g. Border, Back/Foreground color, Font, Layout) to the same value. E.g. all components have to have the same colors, and some of them a particular border, etc.

What is the most elegant way to do this?

A few considerations:
* avoid duplicate code
* preferable instantiation in 1 line
* avoid an exponential grow of classes

Any thoughts about this? Seems to me that this problem is very common.
Yes, good explanation.

Still one more question.. Will unbound() also be called if the session is destroyed?
Congrats Eurig!

Have a nice celebration
You're right.

Check the errata of The Book. It's mentioned there.
[ September 10, 2008: Message edited by: Jan Sterk ]

Originally posted by Bala'J'i Rags:
COngrats dude..what only a beer!!!


Oh, I get it. In my country, the word 'beer' is also a plural form, e.g. two beer.

I corrected the subject.

Take as many as you can, another shipment will arrive soon..

Passed the exam

Books: HFSJ and Charles Lyons' SCWCDSC. Very good combination. HFSJ to start, the other if you want to get deeper in some topic, and as a reference.

Also read a few pages of the specs.

Mocks: HFSJ 1 and 2 (both 80%), 8x Enthuware non-free (75% - 90%), and the one of SCWCDSC (73%). All these are recommended very much.

This forum helped me getting there also very much, both in a technical as in a psychological way. Thanks!
[ September 10, 2008: Message edited by: Jan Sterk ]