在Java 8下更好地利用枚舉
在我們的云使用分析API中,返回了格式化過的分析數(shù)據(jù)(這里指生成分析圖)。最近,我們添加了一個特性,允許用戶選擇時間段(最開始只可以按天選擇)。問題是,代碼中每天中的時間段部分高度耦合了……
例如,下面這段代碼:
- private static List<DataPoint> createListWithZerosForTimeInterval(DateTime from,
- DateTime to,
- ImmutableSet<Metric<? extends Number>> metrics) {
- List<DataPoint> points = new ArrayList<>();
- for (int i = 0; i <= Days.daysBetween(from, to).getDays(); i++) {
- points.add(new DataPoint().withDatas(createDatasWithZeroValues(metrics))
- .withDayOfYear(from.withZone(DateTimeZone.UTC)
- .plusDays(i)
- .withTimeAtStartOfDay()));
- }
- return points;
- }
注意:Days、Minutes、Hours、Weeks 和Months一樣出現(xiàn)在代碼的后面部分。這些代碼來自Joda-Time Java時間和日期API。甚至方法的名字都沒有反應(yīng)出(各自的功能)。這些名字牢牢的綁定到了days的概念上。
我也嘗試過使用不同時間段方式(比如月、周、小時)。但我看到了糟糕的switch/case鬼鬼祟祟地隱藏在代碼里。
你需要知道,switch/case=罪惡 已經(jīng)深入我心了。在我大學(xué)期間的兩段實習(xí)經(jīng)歷中就已經(jīng)這么認為了。因此,我會不惜任何代價避免使用switch/case。這主要是因為它們違反了開放閉合原則。我深深地相信,遵循這個原則是寫出面向?qū)ο蟠a的***實踐。我不是唯一一個這樣想的,Robert C. Martin曾經(jīng)說:
在很多方面,開放閉合原則是面向?qū)ο笤O(shè)計的核心。遵循這個原則會從面向?qū)ο蠹夹g(shù)中收獲巨大的好處,比如可重用性和可維護性。(http://www.objectmentor.com/resources/articles/ocp.pdf )
我告訴自己:“我們使用Java8或許可以發(fā)現(xiàn)一些新的特性來避免swtich/case的危險場面出現(xiàn)”。使用Java8的新 functions(不是那么新,不過你知道我的意思)。我決定使用枚舉代表不同的可得到時間段。
- public enum TimePeriod
- {
- MINUTE(Dimension.MINUTE,
- (from,
- to) -> Minutes.minutesBetween(from, to).getMinutes() + 1,
- Minutes::minutes,
- from -> from.withZone(DateTimeZone.UTC)
- .withSecondOfMinute(0)
- .withMillisOfSecond(0)),
- HOUR(Dimension.HOUR,
- (from,
- to) -> Hours.hoursBetween(from, to).getHours() + 1,
- Hours::hours,
- from -> from.withZone(DateTimeZone.UTC)
- .withMinuteOfHour(0)
- .withSecondOfMinute(0)
- .withMillisOfSecond(0)),
- DAY(Dimension.DAY,
- (from,
- to) -> Days.daysBetween(from, to).getDays() + 1,
- Days::days,
- from -> from.withZone(DateTimeZone.UTC)
- .withTimeAtStartOfDay()),
- WEEK(Dimension.WEEK,
- (from,
- to) -> Weeks.weeksBetween(from, to).getWeeks() + 1,
- Weeks::weeks,
- from -> from.withZone(DateTimeZone.UTC)
- .withDayOfWeek(1)
- .withTimeAtStartOfDay()),
- MONTH(Dimension.MONTH,
- (from,
- to) -> Months.monthsBetween(from, to).getMonths() + 1,
- Months::months,
- from -> from.withZone(DateTimeZone.UTC)
- .withDayOfMonth(1)
- .withTimeAtStartOfDay());
- private Dimension<Timestamp> dimension;
- private BiFunction<DateTime, DateTime, Integer> getNumberOfPoints;
- private Function<Integer, ReadablePeriod> getPeriodFromNbOfInterval;
- private Function<DateTime, DateTime> getStartOfInterval;
- private TimePeriod(Dimension<Timestamp> dimension,
- BiFunction<DateTime, DateTime, Integer> getNumberOfPoints,
- Function<Integer, ReadablePeriod> getPeriodFromNbOfInterval,
- Function<DateTime, DateTime> getStartOfInterval)
- {
- this.dimension = dimension;
- this.getNumberOfPoints = getNumberOfPoints;
- this.getPeriodFromNbOfInterval = getPeriodFromNbOfInterval;
- this.getStartOfInterval = getStartOfInterval;
- }
- public Dimension<Timestamp> getDimension()
- {
- return dimension;
- }
- public int getNumberOfPoints(DateTime from,
- DateTime to)
- {
- return getNumberOfPoints.apply(from, to);
- }
- public ReadablePeriod getPeriodFromNbOfInterval(int nbOfInterval)
- {
- return getPeriodFromNbOfInterval.apply(nbOfInterval);
- }
- public DateTime getStartOfInterval(DateTime from)
- {
- return getStartOfInterval.apply(from);
- }
- }
通過枚舉,我就能夠很容易地修改代碼,允許用戶給圖表數(shù)據(jù)點指定時間段。
原來是這樣調(diào)用:
- for (int i = 0; i <= Days.daysBetween(from, to).getDays(); i++)
變成這樣調(diào)用:
- for (int i = 0; i < timePeriod.getNumberOfPoints(from, to); i++)
支持getGraphDataPoints調(diào)用的Usage Analytics服務(wù)代碼已經(jīng)完成了,并且支持時間段。值得一提的是,它考慮了我之前說過的開放閉合原則。
原文鏈接: coveo 翻譯: ImportNew.com - zer0Black
譯文鏈接: http://www.importnew.com/14040.html