Tuesday, September 25, 2012

Better Carpools with Compossible Cars

Todays random thought is about how to make car pooling better.

The Trouble with Car Pools

This week I have to drive 60 miles to a client down in San Marcos. This is a long drive, I would appreciate both the gas advantages of a car pool, but also the time savings of being able to be productive while driving. However, to do this I need someone near by, who will drive to my house (or vice versa) then drive to my work, and then pick me up. This is a bunch of extra’s and lot’s of little things that if they go wrong will ruin my day. It’s just too much trouble.

 

but what if it didn’t have to be this way?

Image if our 2 cars could become 1, for a period, then split apart again. Then we could both drive to a common meet up, join up, ride together until we covered the common portion of our trip. spilt apart and not have to worry about getting stuck if one of us needs to stay late at work tonight.

Here’s a diagram of the idea:

image

The nice thing about this is not only do you get to share the burden of driving, but also gas mileage only goes up slightly with weight. From my short search, doubling your weight (2 cars) would only remove about 10% to your gas mileage. So a 34MPG Smart Car should get about 30 MPG combined with a second car.

Saturday, September 1, 2012

Generic Type Information at Runtime in Java

I’ve been playing around with Hadoop lately, and it seems that this is a situation when the lack of runtime information about generics really hurts.
The bad news: java isn’t going to be changing that anytime soon.
The good news: you don’t need to wait for them to do it.
       Here’s how.

1. Define Useful Generic Base

The secret here is to add a method per generic type to expose it’s type. Here’s an example

public interface UsefulMap<In1, Out1>
{
  public Class<In1> getIn1Type();
  public Class<Out1> getOut1Type();
}

2. Implement

Now when you implement, you will have to provide the runtime information.
public class MyMap implements UsefulMap<Double, String>
{
  @Override
  public Class<Double> getIn1Type()
  {
    return Double.class;
  }
  @Override
  public Class<String> getOut1Type()
  {
    return String.class;
  }
}

The nice part is, if you forget or change it, it won’t compile.

  public Class<Double> getIn1Type()
  {
    return Integer.class; <- Type mismatch: cannot convert from 
                             Class<Integer> to Class<Double>
  }


Once you do this, there are a bunch of convenience functions you can write that will make your setup much easier & safer.

For example, you can now write

HadoopJob.Create()         
         .WithMapper(new MyMapper())
         .AndReducer(new MyReducer())
         .Start();