Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, December 16, 2015

12/16/2015

Learned
-about using joda time's DateTime to parse a UTC date.
-about using Apache Commons XMLConfiguration to drive strategy
-about one pattern for using strategies in a Java Spring Application:
in beans file add:
    <bean id="byWhichMeansStrategyFactory" class="com.awgtek.byWhichMeansStrategyFactory" factory-method="createStrategy">
<constructor-arg index="0"  ref="myXmlConfig" />
<constructor-arg index="1" type="java.lang.String">
  <value>this_means</value>
</constructor-arg>
    </bean>
   
    <bean id="MyImpl" class="com.awgtek.DoesSomethingImpl">
        <constructor-arg index="0" ref="byWhichMeansStrategyFactory" />
    </bean>
The createStrategy method would instantiate the correct strategy class basd on the "this_means" value. The injected strategy would then be used to e.g. pick a consultant either through "cheapness" defined by some parameters in the strategy or skill, etc. see http://blog.lowendahl.net/design-patterns/strategy-pattern/

-about using Guava, e.g. to import static com.google.common.base.Preconditions.checkNotNull to do checkNotNull statements where needed.

Thursday, March 5, 2015

Useful Custom Exceptions

One useful custom exception to cite as an example of "when would you use a custom exception" interview question is the ObjectNotFoundException triggered when accessing an invalid proxy gotten from the session.load() method. Custom exceptions are useful to for conditional logic that is strongly typed and semantically precise and, moreover, more natural then parsing and identifying error codes or String value properties of more generic exception types.

Saturday, December 27, 2014

Observable: Reason for setChanged()

This is useful because it separates the part where you say that the Observable has changed, from the part where you notify the changes. (E.g. Its useful if you have multiple changes happening and you only want to notify at the end of the process rather than at each small step). This is done through setChanged().
- jbx http://stackoverflow.com/questions/13744450/interview-when-do-we-use-observer-and-observable

 ...setChanged is used like a flag I think. This is to avoid unnecessary updating
- James Poulson http://stackoverflow.com/questions/7271044/observable-in-java

But note possible race condition in Java's implementation of Observable where a second call to notifyObservers may not be completed and thus second parameter (if one is provided) is not sent. See answer by mab at http://stackoverflow.com/questions/4446718/why-observable-snapshot-observer-vector/11122611#11122611.

Saturday, December 13, 2014

How to specify JAXB root element

With no metadata specified we need to supply JAXB with a root element name (and namespace).
http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html
@XmlRootElement
public class Customer {
...

Friday, October 10, 2014

Iteration Syntaxes

http://www.javaworld.com/article/2461744/java-language/java-language-iterating-over-collections-in-java-8.html

Do each syntax on demand.

Monday, September 29, 2014

Java wait() releases synchronization lock

Threads calling wait() release the synchronization lock on the current instance, i.e. the method with synchronized keyword is allowed to be entered by other threads.
http://tutorials.jenkov.com/java-concurrency/starvation-and-fairness.html

Sunday, May 4, 2014

readObject and writeObject

Review the nuances of Java serialization, specifically see http://webcache.googleusercontent.com/search?q=cache:Q1Rpg2MReCIJ:www.javablogging.com/what-are-writeobject-and-readobject-customizing-the-serialization-process/+&cd=4&hl=en&ct=clnk&gl=us spells out how readObject and writeObject are called by ObjectOutputStream if present using getPrivateMethod. These are a way to customize serialization. Can call defaultReadObject/defaultWriteObject on ObjectInputStream/ObjectOutputStream before performing custom setup.