Joda Time: Find the amount of time between two dates

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

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

Replacing new with a Transparent Factory

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

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

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