Joda Time: Find the amount of time between two dates

Posted by Chad | Posted in Groovy, Java | Posted on 04-03-2010-05-2008

0

Joda Time is a Date/Time API proposed for Java SE 7. Below, I have a Groovy script that uses the 1.6 Joda Time library to find the difference between two dates.

 import org.joda.time.*
    ...
 def i = new Interval(startDate.getTime(),endDate.getTime())
 def p = i.toPeriod()

 println "Time Difference: " + p.getSeconds() + "." + p.getMillis() + " Seconds"



In addition to Seconds and Milliseconds, you can get the number of Days, Hours, Minutes, Months, Weeks, and Years.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati

Basic MSI Project: Setup.exe Custom Command-Line Arguments

Posted by Chad | Posted in InstallShield | Posted on 11-02-2010-05-2008

0

This one eluded me for way too long considering how straight-forward it is.  So, in an effort to help others who may have the same question, I thought I’d write it down..

Problem: For a Basic MSI Project in InstallShield 2009, I need to pass in 1 or more custom command-line arguments to use during installation/un-installation/etc.



Step 1 – Accessing your custom command-line arguments

First, accessing these values are as easy as accessing other variables in InstallShield.  All you need to do is use the format [MYARGUMENTNAME].  So, for example, if you want to use one of the arguments when invoking an executable from a custom action, it may look like this image below.  My custom action is invoking an executable called myapp.exe and I am accessing a custom command line argument named FILENAME to use as part of the invocation of my executable.

example1



Step 2 – Passing in your custom command-line arguments

It’s really this simple.. each argument needs to be preceeded by a /v and wrapped in quotes.  Continuing the example above, here is how I pass in my custom command line argument named FILENAME:

     setup.exe /v"FILENAME=\"myfile.txt\""

This would result in my custom action invoking my executable as follows:   myapp.exe -f myfile.txt

To be clear on multiple custom command-line arguments, the following example adds a second argument to my example above:

     setup.exe /v"FILENAME=\"myfile.txt\"" /vSECONDARGUMENT=\"value2\""


Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati

My 10 Favorite Android Apps

Posted by Chad | Posted in Android | Posted on 25-01-2010-05-2008

0

I’ve had my new phone for a week.. a Motorola Droid. Without including any games, here are my top 10 favorite apps so far.  I’m curious how this list will change over the coming months.

1. Google Maps Navigation – This is the free app from Google that transforms your phone into a mobile GPS Navigation unit.

2. Seesmic – Twitter client

3. GMail – Do i have to spell it out?

4. Pandora – Music, like last.fm, but much better.

5. Facebook for Android

6. Flashlight – I have the version that just turns your screen bright white.. not the version that uses the phone’s camera’s LED flash bulbs.

7. Calendar – Syncs to your Google calendar.

8. FourSquare – Location based social application, allowing you to ‘check-in’ at venues.

9. WorldTour – Retrieves images to be used as your wallpaper from all over the world.  Can be set up to randomly change every 30 minutes, or choose a static site.

10. Yammer – A client for Yammer (much like Twitter except for internal corporate use).

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati

Replacing new with a Transparent Factory

Posted by Chad | Posted in Groovy | Posted on 22-12-2009-05-2008

0

A little while back, I decided I would try and come up with some fun things to try and accomplish in Groovy and blog about them. The primary goal being to learn more about the Groovy language.

The adventure I chose for this post was to somehow make the use of a Factory transparent by allowing what appeared to be the instantiation of an Interface or Abstract Class. I looked for ways to override the new keyword so that when new was used with an Interface or Abstract Class, it would call a factory behind the scenes. I figured out that I could not override the new keyword, but ended up finding this example of overriding the getAt method as a way of constructing an object. I thought this was very innovative and it be best to use this approach for the task at hand.

In this code segment, you can see I have an interface Animal, two implementations, Dog and Cat, along with a factory AnimalFactory. Now, the factory here is not very useful. It simply calls the first constructor of the Class passed in as a parameter to its only method, create(). However, the focus in this post is how we can transparently use a factory, not the logic used by the factory itself.

    interface Animal {
     def talk();
    }

    class Dog implements Animal {
     def talk() {"bark"}
    }

    class Cat implements Animal {
     def talk() {"meow"}
    }

    class AnimalFactory {
     static Object create(Class c) {
      c.getConstructors()[0].newInstance()
     }
    }



This following segment is where the magic happens. First, I’m creating a static method on all classes called registerFactory() that allows us to register a factory with an Interface or Abstract class. The second thing i’m doing is what you saw in the example I linked to above by Alex Tkachman, where we override the static getAt method, allowing us to use square brackets for object instantiation.

    Class.metaClass.static.registerFactory = { clazz ->
     delegate.metaClass.static.getClassFactory = { -> clazz}
    }
    Class.metaClass.static.getAt = { c -> delegate.classFactory.create(c)}



Now, you can see an example of using the registerFactory() method and instantiating an object through the getAt method on an interface.

    Animal.registerFactory(AnimalFactory)

    def c = Animal[Cat]
    c.talk()



Finally, the output from this previous segment is shown below.

    Result: meow



I thought about taking this post even further and making the factory more useful by trying out some dynamic constructor invocation based on the arguments passed in through the square brackets, but I think I’ll save that for later. For now, I hope you can see past my useless factory implementation.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati

Groovy Applied: findAll

Posted by Chad | Posted in Groovy | Posted on 18-12-2009-05-2008

1

Use findAll to match items in separate lists.

Problem: Given a list of employee names and a list of employee objects, find the id of each employee whose name is in the list.

Provided:

def namesList = ["Joe","Jane"]
class Student {def name; def id}
def students = [
    new Student([name:"Joe",id:101]),
    new Student([name:"Fred",id:102]),
    new Student([name:"Jane",id:104])
    ]


Solution:

students.findAll {
    namesList.contains(it.name)
}.id


Output:

[101,104]



Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati

Problem Solvers

Posted by Chad | Posted in People | Posted on 17-11-2009-05-2008

0

Over the past 5 years, I’ve had the opportunity to work with many people who were very knowledgeable in many different areas.  However, one thing I’ve come to realize all too often is that when it comes to solving difficult problems, knowledge is, at most, half the battle.  Time and time again I have seen people struggle at solving a problem they did not immediately know the answer to.  Without a suite of problem solving skills, the value of their knowledge of the problem domain quickly diminishes.  These are the times when the Problem Solvers step in and get the job done.

So, who are the real problem solvers?

Visionaries – The greatest Problem Solvers can very quickly identify a clear path to success.  From this vision they are able to generate a plan of attack that will strategically expose the root problem and find the correct solution(s).

Leaders – As a leader, the Problem Solver is able to coordinate the effort.  They break the the plan of attack down into individual tasks that can be executed in parallel by the resources at hand.

Enablers – Possibly the most important skill a Problem Solver has is the ability to remove roadblocks or find alternative paths.  The key to solving a problem quickly is to never stop.  The Problem Solver recognizes this and focuses on getting himself/herself and others whatever it is they need to identify and fix the issue.

Communicators – Solving problems requires that the right people have the right information.  The Problem Solver is able to recognize important data and ensure that the information is shared.  Furthermore, they identify all available communication channels and use them effectively.

Adaptive - The Problem Solver knows that any new discovery can change everything.  At a moment’s notice, they are able to shift resources, open new communication channels, and devise an entirely new plan of attack.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati

Welcome

Posted by Chad | Posted in Misc | Posted on 02-11-2009-05-2008

1

About a year ago, I stopped blogging publicly.  It really just came down to a change in priorities for a while.  However, I’m back and excited to get going again.  This site is very likely to change over the next little while.  I just picked the first decent theme I could find to get things up going.

Anyway… welcome and hopefully you find something I write to be educational, thought provoking or entertaining.

-Chad

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • FriendFeed
  • Ping.fm
  • StumbleUpon
  • Technorati