title: Optimize many Intervals in Joda Time
body: <p>I have a bunch of Joda time <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/Interval.html" rel="nofollow">Interval</a> objects stored in a List. All of these Intervals have valid start and end instants. These Intervals can have any overlaps, abuts or even gaps among themselves.</p>
  
  <p>I want to flatten (optimize) these Intervals to the maximum possible. I mean, I have to generate other Interval objets that will represent the very same from - to information, but without redundancy.</p>
  
  <p>For example:</p>
  
  <p>I1: 2012-01-12T05:00:00.000/2013-03-18T14:00:00.000</p>
  
  <p>I2: 2012-04-12T04:00:00.000/2013-02-10T06:00:00.000</p>
  
  <p>I3: 2015-12-12T04:00:00.000/2016-12-12T06:00:00.000</p>
  
  <p>Should produce:</p>
  
  <p>I1_o: 2012-01-12T05:00:00.000/2013-03-18T14:00:00.000</p>
  
  <p>I2_o: 2015-12-12T04:00:00.000/2016-12-12T06:00:00.000</p>
  
  <p>(I2 is absolutely within I1, so can be ignored, and the resulted two has a gap).</p>
  
  <p>I know the three methods in Interval class that can help me, but I guess I need a more generic algorithm that can search overlaps between some kind of intervals, like ordinary numbers, etc.
  Thanks in advance!</p>