Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement a method returning "week-data" [closed]

Tags:

java

In my application I'm implementing several service methods returning data related to a week period.

So I have several methods similar to getDataByDateInterval(Date from, Date to) but I actually need to always retrieve data for a week: from Monday to Sunday (or from Sunday to Saturday, if you prefer).

In this case, is it more correct to have a method with a single Date argument and then calculate week date span?

I'd like to find an elegant and correct way to implement these methods.

like image 251
davioooh Avatar asked Aug 08 '26 01:08

davioooh


2 Answers

I think the best way to achieve this is to chain your methods:

getDataByDateInterval(Date from, Date to) {
    //...
}

getDataByWeek(Date to) {
    // Calculate 'from' date (minus 7 days)
    return getDataByDateInterval(from, to);
}

This approach keeps your interface clear but prevents you violating the DRY principle.

like image 197
JamesB Avatar answered Aug 10 '26 13:08

JamesB


I have had great success handling date-range queries using Guava's Range class. For a while I tried creating methods like getDataByDateInterval(Date from, Date to) but these are cumbersome to work with, particularly to keep track of whether from and to should be inclusive or exclusive in any particular case.

I use two interfaces very heavily in my project, Dated and DatedLookup:

public interface Dated {
    public LocalDate getDate();
}

public interface DatedLookup<T extends Dated> {
    public List<T> queryRange(Range<LocalDate> range);
}

Range also lets you query for all data, or data unbound on one side (e.g. (-∞..2000-1-1) will query everything before 2000). This introduces some of it's own complexities (you have to check that the range has endpoints before trying to get them) but I think this is a feature more than a burden.

You can also, as an implementation detail, know the bounds of all you data and thereby bound the inputs your user sends, e.g.:

public class MyClass<T> imlements DatedLookup<T> {
  // We know there's no data outside this range
  private static final Range<LocalDate> MAX_RANGE = 
      Range.closed(new LocalDate(2010,1,1), new LocalDate());

  @Override
  public List<T> queryRange(Range<LocalDate> range) {
    // range could be Range.all(), meaning it has no bounds
    range = MAX_RANGE.intersection(range);
    // now we guarenteee range has bounds, and can safely call
    LocalDate start = range.lowerEndpoint();
    ....
  }

  // We can also define useful wrappers, like
  public List<T> queryOneWeekBack(LocalDate lastDay) {
    return queryRange(Range.openClosed(lastDate.minus(Weeks.ONE), lastDay);
  }
}

You'll need Range (and the awesome RangeSet and RangeMap) from Guava, and LocalDate from Joda-Time (or LocalDate in java.time package in Java 8). These two libraries add amazingly useful functionality to almost any project. You're crippling yourself if you aren't using them.

like image 20
dimo414 Avatar answered Aug 10 '26 14:08

dimo414



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!