@Embeddable
@Embedded
@EmbeddedId
http://www.thejavageek.com/2014/05/01/jpa-embeddedid-example/
Tuesday, June 16, 2015
Monday, June 8, 2015
Effective Java - Chapter 07
Item 38: Check parameters for validity
Item 39: Defensive copy
- Detect errors as soon as possible
- For public methods, use the Javadoc @throws tag to document the exception that will be thrown if a restriction on parameter values is violated
- Non-public methods should generally check their parameters using assertions.
In summary, if a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components. If the cost of the copy would be prohibitive andthe class trusts its clients not to modify the components inappropriately, then the defensive copy may be replaced by documentation outlining the client’s responsibility not to modify the affected components.
- You must program defensively, with the assumption that clients of your class will do their best to destroy its invariant.
- experienced programmers often use the primitive long returned by Date.getTime() as an internal time representation instead of using a Date reference. They do this primarily because Date is mutable.
- Defensive copying can have a performance penalty associated with it and isn’t always justified.
Sunday, May 31, 2015
Effective Java - Chapter 3
Item 11: Clone
Item 12: Comparable
- if a class implements Cloneable, Object’s clone method returns a field-by-field copy of the object; otherwise it throws CloneNotSupportedException.
- all classes that implement Cloneableshould override clonewith a public method whose return type is the class itself.
- You can increase the scope of access modifiers of an overridden method
- Calling method clone won't call the constructor
- It's OK if using super.clone() to clone immutable object. If object is mutable, we have to manually clone its properties which are mutable (like array, set, list, mutable class, ...).
- When working with thread-safe, Object’s clonemethod is not synchronized, you may have to write a synchronized clone method that invokes super.clone().
- it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override the clonemethod and never to invoke it except, perhaps, to copy arrays.
Item 12: Comparable
- override compareTo method
- sometimes, compareTo method is not consistent with equals method
- Collection, Set, Map use equals method to check for existence, while sorted collections like TreeSet, TreeMap use compareTo method.
Subscribe to:
Comments (Atom)