16장 SerialDate 리팩터링(363~369)

논리적 의존성 제거하기

public Day getDayOfWeek(){
	Day startingDay = getDayOfWeekForOrdinalZero();
	int startingOffset = startingDay.index - Day.SUNDAY.index;
	return Day.make((getOrdinalDay() + startingOffset) % 7 + 1);
}

if 문 연쇄를 enum으로 옮겨 연쇄 없애기

public enum DateInterval{
	Open {
		public boolean isIn(int d, int left, int right) {
			return d > left && d < right;
		}
	},
	CLOSED_LEFT {
		public boolean isIn(int d, int left, int right) {
			return d >= left && d < right;
		}
	},
	CLOSED_RIGHT {
		public boolean isIn(int d, int left, int right) {
			return d > left && d <= right;
		}
	},
	CLOSED {
		public boolean isIn(int d, int left, int right) {
			return d >= left && d <= right;
		}
	};

	public abstract boolean isIn(int d, int left, int right);
}

public boolean isInRange(DayDate d1, DayDate d2, DateInterval interval) {
	int left = Math.min(d1.getOrdinalDay(), d2.getOrdinalDay());
	int right = Math.max(d1.getOrdinalDay(), d2.getOrdinalDay());
	return interval.isIn(getOrdinalDay(), left, right);
}

결론!