Triplet class

I wrote a Triplet class, it seems general purpose enough to share, so here it is:

package harsch.sandbox;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 
 * @author Tim Harsch
 * 
 * @param <L>
 * @param <M>
 * @param <R>
 */
public class Triplet<L, M, R> implements Comparable<Object> {
 private final L left;
 private final M mid;
 private final R right;

 public R getRight() {
  return right;
 } // end getter

 public M getMid() {
  return mid;
 } // end getter

 public L getLeft() {
  return left;
 } // end getter

 public Triplet(final L left, final M mid, final R right) {
  this.left = left;
  this.mid = mid;
  this.right = right;
 } // end constructor

 public static <A, B, C> Triplet<A, B, C> create(A left, B mid, C right) {
  return new Triplet<A, B, C>(left, mid, right);
 } // end factory method

 @Override
 public final boolean equals(Object o) {
  if (!(o instanceof Triplet<?, ?, ?>))
   return false;

  final Triplet<?, ?, ?> other = (Triplet<?, ?, ?>) o;
  return equal(getLeft(), other.getLeft())
    && equal(getMid(), other.getMid())
    && equal(getRight(), other.getRight());
 } // end method

 public static final boolean equal(Object o1, Object o2) {
  if (o1 == null) {
   return o2 == null;
  }
  return o1.equals(o2);
 } // end method

 @Override
 public int hashCode() {
  return new HashCodeBuilder(17, 13).append(this.left).append(this.mid)
    .append(this.right).toHashCode();
 } // end method

 @Override
 public int compareTo(Object o) {
  Triplet<?, ?, ?> other = (Triplet<?, ?, ?>) o;
  return new CompareToBuilder().append(this.left, other.left).append(
    this.mid, other.mid).append(this.right, other.right)
    .toComparison();
 } // end method

 @Override
 public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append('<');
  if (left == null) {
   sb.append("null");
  } else {
   sb.append(left.toString());
  } // end if
  sb.append(',');
  if (mid == null) {
   sb.append("null");
  } else {
   sb.append(mid.toString());
  } // end if
  sb.append(',');
  if (right == null) {
   sb.append("null");
  } else {
   sb.append(right.toString());
  } // end if
  sb.append('>');
  return sb.toString();
 } // end method
} // end class

UPromise

I use a service called UPromise, which I think any parent planning for a college education should use. You can simply sign up and then register a credit card that you use with them. From that point forward, any purchases that you make with any of their affiliated sites will get you a percentage kick back, which goes into your Account and can be transfered to a 529 college account. The nice part is it costs you nothing and you would be making the purchase anyway, so why not get the extra points. Oh, and it doesn't matter if your credit card already earns you miles, points or cash back. You get the college savings anyway!

Java Maps Inline Initialization

I found a blog sometime back at
http://nileshbansal.blogspot.com/2009/04/initializing-java-maps-inline.html

It shows how to inline initialize a Java map, like so:
Map map = new HashMap<String, String>() {{
put("Harry", "Potter");
put("Ron", "Weasley");
put("Hermione", "Granger");
}};

I find myself forgetting the exact syntax if I haven't used in awhile so I thought I would blog it :-)

An alternate way is using Google collections:
Map<String, String> map = ImmutableMap.of( "x", "y" );

More Shocker

Work on the shocker continues. Check this out!

Stack Overflow

Awhile back a friend and co-worker showed my Stack Overflow. I never really got into at the time, but lately I have had need to ask some questions. Mostly, I really love the way they have organized the workflow and ratings of question/answer. It goes way beyond the email list thread idea. On top of that it is something of a social site as well. Your user gains reputation from posts/answers and voting. I was amazed at how quick I got answers but more than that how thorough people are to provide a clear and concise answer. The reputation aspect really encourages others to answer well.

Also, they are a service which uses Gravatars (see previous post).

Gravatar

Check out Gravatar! A very interesting concept of creating a persistent avatar that can be used with other web services:

In my early days at LLNL my co-workers called me "alpha geek" cause I am fairly knowledgable about a lot of software development and technical topics. I used to do ontology modeling with Stanford's Protege project and have always enjoyed the protege nerd that is their mascot. I "borrowed" him (I'm hoping he is under some sort of commons license) and twiddled him to match an idea I had to create the alpha geek. Mostly I wanted to make him look a little more like me so I shortened his glasses, put him in a T-shirt and put the greek alpha letter prominently in the middle. I then thought I don't claim to be a great teacher so I removed the pointer stick and replaced it with a thumbs up!

Here he is as my Gravatar. What do you think?

Are kids natural born physicists?

While walking to the park my daughter, walking ahead, looked down, pointed and said "Mommy, Daddy, look! My shadow!" Lori, then quizzed, "Mason, what's the difference between a shadow and your reflection". Mason, gave a barely a moment's thought, and said "There's no light."
Of course, when you think about it, it's a perfect text book answer. It may be possible that she was taught the definition at day care, and not really some miracle child, but in any case an answer like that coming from a three year old was enough to blow us away.

Check out the new gadget at the top of this page. It is a play list of mine from LaLa. www.lala.com is probably the coolest thing I've found all year!

More Electric Dirt Bikes...

... oh so cool. Check out the canti-levered suspension.

This will be proved...

This will be proved to Ed that I can call in and Jott messages to Blogger. Check it out at www.jott.com. You can see that it transcribes my voice directly.
listen

Powered by Jott

OK... The street version was cool. I knew about the dirt version and after digging on Jay Leno's site I found the older review.

This is pretty cool. The bike they feature here is a new release from the same company that makes electric dirt bikes. Awesome stuff..

I recently discovered...

I recently discovered that you can Jott messages to Blogger. Check it out at www.jott.com.
listen

Powered by Jott

Using PropertyPlaceHolderConfigurer to Create a WebApp Configuration File

The problem: when developing my properties files are available on the classpath. I have many layered projects so the props files end up in a jar which gets embedded in a JAR inside the WAR. This doesn't make a very friendly config file for someone deploying the app. Essentially I want my WAR to read a config file outside the classpath, in a location determined by an environement variable. I realized I could extend Spring's PropertyPlaceHolderConfigurer to do the trick. This class and config file are below, as well as some test files.

package harschware;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.google.common.collect.Lists;

public class HomePropertyPlaceholderCongfigurer extends
PropertyPlaceholderConfigurer {
private String homePathEnv;
private String[] homePathLocations;

public HomePropertyPlaceholderCongfigurer(String homePathEnv,
String[] homePathLocations ) {
super();
this.homePathEnv = System.getenv(homePathEnv);
this.homePathLocations = homePathLocations;
} // end method

public void setLocations(Resource[] locations) {
List<Resource> locList = Lists.newArrayList(locations);

for (String homePathLocation : homePathLocations) {
if( homePathEnv != null ) {
Resource propFileRespource = new FileSystemResource(homePathEnv
+ File.separator + homePathLocation);
if( propFileRespource.exists() ) locList.add(propFileRespource);
} // end if
} // end for
locations = locList.toArray(locations);
super.setLocations(locations);
} // end method

} // end class



The Spring configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean class="harschware.HomePropertyPlaceholderCongfigurer">
<constructor-arg value="MY_HOME" />
<constructor-arg>
<list>
<value>home.properties</value>
</list>
</constructor-arg>
<property name="locations">
<value>classpath:test.properties</value>
</property>
</bean>

<bean id="testStr" class="java.lang.String">
<constructor-arg value="${t.x}" />
</bean>
</beans>


A Simple Test Driver:
package harschware;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMe {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:app.xml");
System.out.println((String)ctx.getBean("testStr"));
}

}

The properties file "test.properties" (existing in the project's classpath):
t.x=Not Overridden!


The properties file "home.properties" (existing at %MY_HOME%/home.properties):
t.x=It was overidden


Running the test app would produce the output "It was overidden".

That's it!

Kids can't do acronyms (and neither can adults)

Hello and welcome to my first blog. Given that I am a software engineer, currently working at NASA Ames Research Center, you'd think that my posts are going to be about booring old technical stuff like "A trick to make writing SPARQL easier" (SPARQL is a semantic web query language)... well, you'd be right! Unfortunately I'm not all that creative; all that often and I usually have my head wrapped in some technical problem or other even when I'm doing other things I love like dirt biking for example. However, in the hopes of giving my future booring posts a more entertaining inauguration I thought I would relate to you a story about my kids... well, actually its more about the funny things that adults do...

So we're in the car coming back from church or somewhere and my belly begins to give me early signals that it's time to put something in it. Knowing I get freaky when I'm hungry I decide to deal with it right then and there:

me: "Hey mom? (what I call my wife since she is the mother of my two children, makes sense huh? )"
wife: "Yes, dear?"
me: "What should we have to eat when we get home? ( this is sometimes a cleverly disguised way of saying: "What are you fixing for lunch?" )
wife: "I don't know what do you want?"
me: "Mmmmm, not sure what are you in the mood for?"

me: "Mmmmm, well how about sandwhiches?"
wife: "OK, how about PB&J" (note standard acronym usage: you know this one don't you? I now think this standard acronym will next lead my wife into speaking in child safe encrypted language where we either spell our intended subjects or use acronyms...)
me: "Nah, what else?"
wife: "How about TFS?" ( I was right... )
... wait a minute what's TFS? I pride myself on being able to discern the meaning of an acronym quickly just by noodling hard enough on the letters and trying out a few common words for some of them. TFS? Turkey, Sandwhich, what could be 'F'? ...
me: "TFS?"
wife: "Yeah you know: TFS."
( hmmm... being prodded I think some more. As my jaw begins to slack, and a little drool begins to appear at the corner of mouth, I realize I can't think through the sound of these blasted crickets... I give up )
me: "TFS?"
wife: "Yeah, you know: Tee Eff Ess?"
frustrated me: "What do you mean TFS?" ( Is she smiling? )
wife: "Yeah, you know: Teeeee Effffff Esssss?" ( Thank God we're speaking in lingo that the kids can't crack. )
me: "Just tell me damn it!"
wife waving hands and speaking louder: "Yeah, you know: Teeeeeeeee Efffffffffff Essssssssss?" Watching her wave hands in a circle to emphasize each syllable of what is supposed to be an acronym now has me giggling a bit. I realize she is doing something most of us do at one time or another... it's like what you do when trying to give directions to someone who barely speaks English you make sure you tell em slow and loud!
me: "Alright already I give what is it?"
wife: "You know T, T-u F-i S-i. no wait! S-a"
I get passed her original erroneous attempt at spelling and I think for a moment. As I begin to notice the sound of crickets creaping in I finally get it.
me: "Tuna Fish Sandwhich?"
wife: "Tuna Fish Salad."
Resisting the urge to discuss the semantic differerences between the two knowing it will likely be a 20 minute conversation I choose instead to interrogate her as to:
me: "Why didn't you just say so?"
Insert 10 minute dialog bordering on the ludicrous, and you'll realize, as do I, how much fun we have and why I love her.

Thanks Lori.