Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished Table-Table tutorial

  • Loading branch information...
commit 35cbc5f71a997ef7a4d8d00dbdd54e9effe057ba 1 parent b30d949
Brett L. Schuchert authored
View
90 DVR/src/com/om/example/dvr/domain/Program.java
@@ -6,43 +6,53 @@
public class Program {
- public final String programName;
- public final String episodeName;
- public final TimeSlot timeSlot;
-
- public Program(String programName, String episodeName, TimeSlot timeSlot) {
- this.programName = programName;
- this.episodeName = episodeName;
- this.timeSlot = timeSlot;
- }
-
- public String getId() {
- return String.format("(%s:%d)", programName, timeSlot.channel);
- }
-
- public String getProgramName() {
- return programName;
- }
-
- public String getEpisodeName() {
- return episodeName;
- }
-
- public TimeSlot getTimeSlot() {
- return timeSlot;
- }
-
- public boolean sameEpisodeAs(Program program) {
- return timeSlot.channel == program.timeSlot.channel
- && programName.equals(program.programName)
- && episodeName.equals(program.episodeName);
- }
-
- public boolean isOn(Date date) {
- return DateUtil.instance().isSameDate(timeSlot.startDateTime, date);
- }
-
- public boolean hasTimeConflictWith(Program other) {
- return timeSlot.conflictsInTimeWith(other.timeSlot);
- }
-}
+ public final String programName;
+ public final String episodeName;
+ public final TimeSlot timeSlot;
+
+ public Program(String programName, String episodeName, TimeSlot timeSlot) {
+ this.programName = programName;
+ this.episodeName = episodeName;
+ this.timeSlot = timeSlot;
+ }
+
+ public String getId() {
+ return String.format("(%s:%d)", programName, timeSlot.channel);
+ }
+
+ public String getProgramName() {
+ return programName;
+ }
+
+ public String getEpisodeName() {
+ return episodeName;
+ }
+
+ public TimeSlot getTimeSlot() {
+ return timeSlot;
+ }
+
+ public boolean sameEpisodeAs(Program program) {
+ return timeSlot.channel == program.timeSlot.channel
+ && programName.equals(program.programName)
+ && episodeName.equals(program.episodeName);
+ }
+
+ public boolean isOn(Date date) {
+ return DateUtil.instance().isSameDate(timeSlot.startDateTime, date);
+ }
+
+ public boolean hasTimeConflictWith(Program other) {
+ return timeSlot.conflictsInTimeWith(other.timeSlot);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof Program))
+ return false;
+
+ Program rhs = getClass().cast(other);
+ return programName.equals(rhs.programName) && episodeName.equals(rhs.episodeName)
+ && timeSlot.equals(rhs.timeSlot);
+ }
+}
View
6 DVR/src/com/om/example/dvr/domain/Schedule.java
@@ -51,4 +51,10 @@ private boolean conflictsWithOtherTimeSlots(TimeSlot timeSlot) {
public void clear() {
scheduledPrograms.clear();
}
+
+ public void addProgram(Program program) {
+ if (conflictsWithOtherTimeSlots(program.timeSlot))
+ throw new ConflictingProgramException();
+ scheduledPrograms.add(program);
+ }
}
View
52 DVR/src/com/om/example/dvr/domain/TimeSlot.java
@@ -6,26 +6,34 @@
public class TimeSlot {
- public final int channel;
- public final Date startDateTime;
- public final int durationInMinutes;
-
- public TimeSlot(int channel, Date startDateTime, int durationInMinutes) {
- this.channel = channel;
- this.startDateTime = startDateTime;
- this.durationInMinutes = durationInMinutes;
- }
-
- public boolean conflictsWith(TimeSlot other) {
- if (channel == other.channel
- && startDateTime.equals(other.startDateTime))
- return true;
- return false;
- }
-
- public boolean conflictsInTimeWith(TimeSlot other) {
- return DateUtil.instance()
- .segmentsConflict(startDateTime, durationInMinutes,
- other.startDateTime, other.durationInMinutes);
- }
+ public final int channel;
+ public final Date startDateTime;
+ public final int durationInMinutes;
+
+ public TimeSlot(int channel, Date startDateTime, int durationInMinutes) {
+ this.channel = channel;
+ this.startDateTime = startDateTime;
+ this.durationInMinutes = durationInMinutes;
+ }
+
+ public boolean conflictsWith(TimeSlot other) {
+ if (channel == other.channel && startDateTime.equals(other.startDateTime))
+ return true;
+ return false;
+ }
+
+ public boolean conflictsInTimeWith(TimeSlot other) {
+ return DateUtil.instance().segmentsConflict(startDateTime, durationInMinutes,
+ other.startDateTime, other.durationInMinutes);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof TimeSlot))
+ return false;
+
+ TimeSlot rhs = getClass().cast(other);
+ return channel == rhs.channel && durationInMinutes == rhs.durationInMinutes
+ && startDateTime.equals(rhs.startDateTime);
+ }
}
View
31 DVR/src/com/om/example/dvr/fixtures/CreateOneDayProgramGuide.java
@@ -0,0 +1,31 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+import java.util.Collections;
+import java.util.List;
+
+import com.om.example.dvr.domain.Program;
+
+public class CreateOneDayProgramGuide {
+ private ProgramGuideRowParser parser;
+
+ public CreateOneDayProgramGuide(String startTime, String date) throws ParseException {
+ parser = new ProgramGuideRowParser(date, startTime);
+ }
+
+ public List<?> doTable(List<List<String>> table) {
+ table.remove(0);
+
+ for (List<String> row : table)
+ handleOneRow(row);
+
+ return Collections.emptyList();
+ }
+
+ private void handleOneRow(List<String> row) {
+ List<Program> programs = parser.parse(row);
+
+ for (Program program : programs)
+ AddProgramsToSchedule.getSchedule().addProgram(program);
+ }
+}
View
193 DVR/src/com/om/example/dvr/fixtures/DvrRecording.java
@@ -6,101 +6,100 @@
import com.om.example.dvr.domain.Program;
public class DvrRecording {
- public void givenDvrCanRecord(int number) {
- CreateSeasonPassFor.getSeasonPassManager().setNumberOfRecorders(number);
- }
-
- public void whenICreateSeasonPasses(String listOfSeasonPasses) {
- String[] individualSeasonPasses = listOfSeasonPasses.split(",");
-
- for (String programNameChannel : individualSeasonPasses)
- addOneSeasonPass(programNameChannel);
- }
-
- private void addOneSeasonPass(String programNameChannel) {
- String[] parts = programNameChannel.split(":");
-
- String programName = parts[0];
- int channel = Integer.parseInt(parts[1]);
-
- new CreateSeasonPassFor(programName, channel);
- }
-
- public boolean thenTheToDoListShouldContain(String listOfEpisodes) {
- List<Program> toDoList = CreateSeasonPassFor.getSeasonPassManager()
- .toDoListContentsFor("");
-
- String[] episodesSets = listOfEpisodes.split(",");
-
- for (String episodeSet : episodesSets)
- if (!removeAllFrom(episodeSet, toDoList))
- return false;
-
- return toDoList.size() == 0;
- }
-
- private boolean removeAllFrom(String episodeSet, List<Program> toDoList) {
- String programName = extractProgramNameFrom(episodeSet);
- String baseEpisodeName = extractBaseNameFrom(episodeSet);
- int lowerRange = extractLowerRangeFrom(episodeSet);
- int upperRange = extractUpperRangeFrom(episodeSet);
-
- for (int episodeNumber = lowerRange; episodeNumber <= upperRange; ++episodeNumber)
- if (!remove(programName, baseEpisodeName, episodeNumber, toDoList))
- return false;
-
- return true;
- }
-
- private String extractProgramNameFrom(String episodeSet) {
- return episodeSet.split(":")[0];
- }
-
- private String extractBaseNameFrom(String episodeSet) {
- String[] values = episodeSet.split(":");
-
- String result = "";
- if (values.length > 1)
- result = values[1];
-
- return result;
- }
-
- private int extractLowerRangeFrom(String episodeSet) {
- String[] values = episodeSet.split(":");
- if (values.length > 2) {
- String lowRange = episodeSet.split(":")[2].split("-")[0];
- return Integer.parseInt(lowRange);
- }
- return 0;
- }
-
- private int extractUpperRangeFrom(String episodeSet) {
- String[] values = episodeSet.split(":");
- if (values.length > 2) {
- String highRange = episodeSet.split(":")[2].split("-")[1];
- return Integer.parseInt(highRange);
- }
- return -1;
- }
-
- private boolean remove(String programName, String baseEpisodeName,
- int episodeNumber, List<Program> toDoList) {
- String episodeName = String.format("%s%d", baseEpisodeName,
- episodeNumber);
-
- for (Iterator<Program> iter = toDoList.iterator(); iter.hasNext();) {
- if (matches(iter.next(), programName, episodeName)) {
- iter.remove();
- return true;
- }
- }
- return false;
- }
-
- private boolean matches(Program current, String programName,
- String episodeName) {
- return programName.equals(current.programName)
- && episodeName.equals(current.episodeName);
- }
+ public void givenDvrCanRecord(int number) {
+ CreateSeasonPassFor.getSeasonPassManager().setNumberOfRecorders(number);
+ }
+
+ public void whenICreateSeasonPasses(String listOfSeasonPasses) {
+ String[] individualSeasonPasses = listOfSeasonPasses.split(",");
+
+ for (String programNameChannel : individualSeasonPasses)
+ addOneSeasonPass(programNameChannel);
+ }
+
+ private void addOneSeasonPass(String programNameChannel) {
+ String[] parts = programNameChannel.split(":");
+
+ String programName = parts[0];
+ int channel = Integer.parseInt(parts[1]);
+
+ new CreateSeasonPassFor(programName, channel);
+ }
+
+ public boolean thenTheToDoListShouldContain(String listOfEpisodes) {
+ List<Program> toDoList = CreateSeasonPassFor.getSeasonPassManager()
+ .toDoListContentsFor("");
+
+ String[] episodesSets = listOfEpisodes.split(",");
+
+ for (String episodeSet : episodesSets)
+ if (!removeAllFrom(episodeSet, toDoList))
+ return false;
+
+ return toDoList.size() == 0;
+ }
+
+ private boolean removeAllFrom(String episodeSet, List<Program> toDoList) {
+ String programName = extractProgramNameFrom(episodeSet);
+ String baseEpisodeName = extractBaseNameFrom(episodeSet);
+ int lowerRange = extractLowerRangeFrom(episodeSet);
+ int upperRange = extractUpperRangeFrom(episodeSet);
+
+ for (int episodeNumber = lowerRange; episodeNumber <= upperRange; ++episodeNumber)
+ if (!remove(programName, baseEpisodeName, episodeNumber, toDoList))
+ return false;
+
+ return true;
+ }
+
+ private String extractProgramNameFrom(String episodeSet) {
+ return episodeSet.split(":")[0];
+ }
+
+ private String extractBaseNameFrom(String episodeSet) {
+ String[] values = episodeSet.split(":");
+
+ String result = "";
+ if (values.length > 1)
+ result = values[1];
+
+ return result;
+ }
+
+ private int extractLowerRangeFrom(String episodeSet) {
+ String[] values = episodeSet.split(":");
+ if (values.length > 2) {
+ String lowRange = episodeSet.split(":")[2].split("-")[0];
+ return Integer.parseInt(lowRange);
+ }
+ return Integer.MAX_VALUE;
+ }
+
+ private int extractUpperRangeFrom(String episodeSet) {
+ String[] values = episodeSet.split(":");
+ if (values.length > 2 && values[2].indexOf('-') > 0) {
+ String highRange = episodeSet.split(":")[2].split("-")[1];
+ return Integer.parseInt(highRange);
+ }
+
+ return 1;
+ }
+
+ private boolean remove(String programName, String baseEpisodeName, int episodeNumber,
+ List<Program> toDoList) {
+ String episodeName = String.format("%s%d", baseEpisodeName, episodeNumber);
+
+ for (Iterator<Program> iter = toDoList.iterator(); iter.hasNext();) {
+ if (matches(iter.next(), programName, episodeName)) {
+ iter.remove();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean matches(Program current, String programName, String episodeName) {
+ return programName.equals(current.programName)
+ && episodeName.equals(current.episodeName);
+ }
}
View
17 DVR/src/com/om/example/dvr/fixtures/ProgramBuilderUtil.java
@@ -0,0 +1,17 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+import java.util.Date;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.dvr.domain.TimeSlot;
+import com.om.example.util.DateUtil;
+
+public class ProgramBuilderUtil {
+ public static Program buildProgram(String date, String time, String name, int channel,
+ int duration) throws ParseException {
+ Date expectedStartDateTime = DateUtil.instance().buildDate(date, time);
+ return new Program(name, "E1", new TimeSlot(channel, expectedStartDateTime,
+ duration));
+ }
+}
View
82 DVR/src/com/om/example/dvr/fixtures/ProgramGuideProgramCellsParser.java
@@ -0,0 +1,82 @@
+package com.om.example.dvr.fixtures;
+
+import java.util.Date;
+import java.util.LinkedList;
+import java.util.List;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.dvr.domain.TimeSlot;
+import com.om.example.util.DateUtil;
+
+public class ProgramGuideProgramCellsParser {
+
+ private static final int MINS_PER_CHAR = 15;
+ private final Date buildDate;
+ private int channel;
+
+ public ProgramGuideProgramCellsParser(Date buildDate) {
+ this.buildDate = buildDate;
+ }
+
+ public List<Program> parse(String programsInCells) {
+ List<Program> result = new LinkedList<Program>();
+
+ String programs = buildSingleStringFromCells(programsInCells);
+
+ int index = 0;
+ while ((index = startIndexOfNextProgram(index, programs)) < programs.length()) {
+ String nextProgramName = calculateNextProgramName(index, programs);
+ index += addNextProgram(nextProgramName, index, result);
+ }
+
+ return result;
+ }
+
+ public void setChannel(int channel) {
+ this.channel = channel;
+ }
+
+ private String buildSingleStringFromCells(String programsInCells) {
+ String programs = programsInCells.replaceAll("\\|\\|", "| |");
+ programs = programs.replaceAll("\\|", "");
+ return programs;
+ }
+
+ private Program buildProgram(int index, int duration, String name) {
+ Date nextDate = calculateNextDate(buildDate, index * MINS_PER_CHAR);
+ TimeSlot timeSlot = new TimeSlot(channel, nextDate, duration);
+ return new Program(name, "E1", timeSlot);
+ }
+
+ private int addNextProgram(String programName, int index, List<Program> result) {
+ if (programName != null) {
+ int length = programName.length();
+ result.add(buildProgram(index, length * MINS_PER_CHAR, programName));
+ return length;
+ }
+ return 0;
+ }
+
+ private int startIndexOfNextProgram(int index, String string) {
+ while (index < string.length() && !Character.isLetter(string.charAt(index)))
+ ++index;
+ return index;
+ }
+
+ private String calculateNextProgramName(int index, String string) {
+ int lastIndex = index;
+
+ while (lastIndex < string.length() && charactesSameAt(string, index, lastIndex))
+ ++lastIndex;
+
+ return string.substring(index, lastIndex);
+ }
+
+ private boolean charactesSameAt(String string, int startIndex, int endIndex) {
+ return string.charAt(endIndex) == string.charAt(startIndex);
+ }
+
+ private Date calculateNextDate(Date fromDate, int lengthInMinutes) {
+ return DateUtil.instance().addMinutesTo(fromDate, lengthInMinutes);
+ }
+}
View
96 DVR/src/com/om/example/dvr/fixtures/ProgramGuideProgramCellsParserTest.java
@@ -0,0 +1,96 @@
+package com.om.example.dvr.fixtures;
+
+import static org.junit.Assert.assertEquals;
+
+import java.text.ParseException;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.util.DateUtil;
+
+public class ProgramGuideProgramCellsParserTest {
+ private ProgramGuideProgramCellsParser parser;
+
+ @Before
+ public void init() throws ParseException {
+ parser = new ProgramGuideProgramCellsParser(DateUtil.instance().buildDate(
+ "4/8/2008", "1:00"));
+ parser.setChannel(204);
+ }
+
+ @Test
+ public void emptyRowGeneratesNoPrograms() {
+ List<Program> result = parser.parse("");
+ assertEquals(0, result.size());
+ }
+
+ @Test
+ public void oneOneHourProgram() throws ParseException {
+ List<Program> result = parser.parse("|aaaa|");
+
+ assertEquals(1, result.size());
+
+ Program expected = buildProgram("4/8/2008", "1:00", "aaaa", 204, 60);
+ assertEquals(expected, result.get(0));
+ }
+
+ @Test
+ public void oneThirtyMinuteProgram() throws ParseException {
+ List<Program> result = parser.parse("|aa|");
+
+ assertEquals(1, result.size());
+
+ Program expected = buildProgram("4/8/2008", "1:00", "aa", 204, 30);
+ assertEquals(expected, result.get(0));
+ }
+
+ @Test
+ public void twoThirtyMinuteProgramsInSameCell() throws ParseException {
+ List<Program> result = parser.parse("|aabb|");
+
+ assertEquals(2, result.size());
+
+ Program expected0 = buildProgram("4/8/2008", "1:00", "aa", 204, 30);
+ assertEquals(expected0, result.get(0));
+
+ Program expected1 = buildProgram("4/8/2008", "1:30", "bb", 204, 30);
+ assertEquals(expected1, result.get(1));
+ }
+
+ @Test
+ public void underscoredIgnored() throws ParseException {
+ List<Program> result = parser.parse("|__aa|");
+
+ assertEquals(1, result.size());
+
+ Program expected = buildProgram("4/8/2008", "1:30", "aa", 204, 30);
+ assertEquals(expected, result.get(0));
+ }
+
+ @Test
+ public void emptyCellsHandeledCorrectly() throws ParseException {
+ List<Program> result = parser.parse("||__aa|");
+
+ assertEquals(1, result.size());
+
+ Program expected = buildProgram("4/8/2008", "2:30", "aa", 204, 30);
+ assertEquals(expected, result.get(0));
+ }
+
+ @Test
+ public void verifyComplexParse() throws ParseException {
+ List<Program> result = parser.parse("||__aa|BBcc||FFgg|__ |");
+ assertEquals(5, result.size());
+
+ Program expectedLastProgram = buildProgram("4/8/2008", "5:30", "gg", 204, 30);
+ assertEquals(expectedLastProgram, result.get(4));
+ }
+
+ private Program buildProgram(String date, String time, String name, int channel,
+ int duration) throws ParseException {
+ return ProgramBuilderUtil.buildProgram(date, time, name, channel, duration);
+ }
+}
View
34 DVR/src/com/om/example/dvr/fixtures/ProgramGuideRowParser.java
@@ -0,0 +1,34 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.util.DateUtil;
+
+public class ProgramGuideRowParser {
+ ProgramGuideProgramCellsParser parser;
+
+ public ProgramGuideRowParser(String date, String startTime) throws ParseException {
+ Date startDateTime = DateUtil.instance().buildDate(date, startTime);
+ parser = new ProgramGuideProgramCellsParser(startDateTime);
+ }
+
+ public List<Program> parse(List<String> cells) {
+ int channel = Integer.parseInt(cells.get(0));
+ cells.remove(0);
+ parser.setChannel(channel);
+ return parser.parse(rowAsString(cells));
+ }
+
+ private String rowAsString(List<String> row) {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append("|");
+ for (String current : row)
+ buffer.append(String.format("%s|", current));
+
+ return buffer.toString();
+ }
+}
View
33 DVR/src/com/om/example/dvr/fixtures/ProgramGuideRowParserTest.java
@@ -0,0 +1,33 @@
+package com.om.example.dvr.fixtures;
+
+import static org.junit.Assert.assertEquals;
+
+import java.text.ParseException;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.om.example.dvr.domain.Program;
+
+public class ProgramGuideRowParserTest {
+ @Test
+ public void verifyChannelSetCorrectly() throws ParseException {
+ ProgramGuideRowParser parser = new ProgramGuideRowParser("4/5/2008", "9:00");
+ List<Program> programs = parser.parse(buildList("123", "aaaa"));
+
+ assertEquals(1, programs.size());
+ Program expected = ProgramBuilderUtil.buildProgram("4/5/2008", "9:00", "aaaa", 123,
+ 60);
+ assertEquals(expected, programs.get(0));
+ }
+
+ private List<String> buildList(String... strings) {
+ List<String> result = new LinkedList<String>();
+
+ for (String current : strings)
+ result.add(current);
+
+ return result;
+ }
+}
View
151 DVR/src/com/om/example/util/DateUtil.java
@@ -6,78 +6,81 @@
import java.util.Date;
public class DateUtil {
- private static DateUtil INSTANCE = new DateUtil();
-
- private DateUtil() {
- }
-
- public static DateUtil instance() {
- return INSTANCE;
- }
-
- public static final SimpleDateFormat dateFormat = new SimpleDateFormat(
- "M/d/yyyy");
-
- public Date formatDate(String date) throws ParseException {
- return dateFormat.parse(date);
- }
-
- public String formatDate(Date startDateTime) {
- return dateFormat.format(startDateTime);
- }
-
- static SimpleDateFormat timeFormat = new SimpleDateFormat("H:mm");
-
- public String formatTime(Date startDateTime) {
- return timeFormat.format(startDateTime);
- }
-
- public String addDaysTo(int days, String nextStartDate)
- throws ParseException {
- Calendar calendar = Calendar.getInstance();
- calendar.clear();
- Date startingDate = dateFormat.parse(nextStartDate);
- calendar.setTime(startingDate);
- calendar.add(Calendar.DATE, days);
- return dateFormat.format(calendar.getTime());
- }
-
- public static final SimpleDateFormat dateTimeMergedFormat = new SimpleDateFormat(
- "M/d/yyyy|h:mm");
-
- public Date buildDate(String date, String startTime) throws ParseException {
- String dateTime = String.format("%s|%s", date, startTime);
- return dateTimeMergedFormat.parse(dateTime);
- }
-
- public boolean isSameDate(Date startDateTime, Date date) {
- return formatDate(startDateTime).equals(formatDate(date));
- }
-
- public Date createEndDate(Date startDateTime, int durationInMinutes) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(startDateTime);
- calendar.add(Calendar.MINUTE, durationInMinutes);
- return calendar.getTime();
- }
-
- public boolean segmentsConflict(Date lhs, int lhsDurationMins, Date rhs,
- int rhsDurationMins) {
- Date lhsEnd = createEndDate(lhs, lhsDurationMins);
- Date rhsEnd = createEndDate(rhs, rhsDurationMins);
-
- return isOnToJustBefore(lhs, rhs, rhsEnd)
- || isStrictlyWithin(lhsEnd, rhs, rhsEnd)
- || isOnToJustBefore(rhs, lhs, lhsEnd)
- || isStrictlyWithin(rhsEnd, lhs, lhsEnd);
- }
-
- private boolean isOnToJustBefore(Date date, Date rangeBegin, Date rangeEnd) {
- return date.equals(rangeBegin)
- || (date.after(rangeBegin) && date.before(rangeEnd));
- }
-
- private boolean isStrictlyWithin(Date date, Date rangeBegin, Date rangeEnd) {
- return date.after(rangeBegin) && date.before(rangeEnd);
- }
+ private static DateUtil INSTANCE = new DateUtil();
+
+ private DateUtil() {
+ }
+
+ public static DateUtil instance() {
+ return INSTANCE;
+ }
+
+ public static final SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
+
+ public Date formatDate(String date) throws ParseException {
+ return dateFormat.parse(date);
+ }
+
+ public String formatDate(Date startDateTime) {
+ return dateFormat.format(startDateTime);
+ }
+
+ static SimpleDateFormat timeFormat = new SimpleDateFormat("H:mm");
+
+ public String formatTime(Date startDateTime) {
+ return timeFormat.format(startDateTime);
+ }
+
+ public String addDaysTo(int days, String nextStartDate) throws ParseException {
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ Date startingDate = dateFormat.parse(nextStartDate);
+ calendar.setTime(startingDate);
+ calendar.add(Calendar.DATE, days);
+ return dateFormat.format(calendar.getTime());
+ }
+
+ public static final SimpleDateFormat dateTimeMergedFormat = new SimpleDateFormat(
+ "M/d/yyyy|h:mm");
+
+ public Date buildDate(String date, String startTime) throws ParseException {
+ String dateTime = String.format("%s|%s", date, startTime);
+ return dateTimeMergedFormat.parse(dateTime);
+ }
+
+ public boolean isSameDate(Date startDateTime, Date date) {
+ return formatDate(startDateTime).equals(formatDate(date));
+ }
+
+ public Date createEndDate(Date startDateTime, int durationInMinutes) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(startDateTime);
+ calendar.add(Calendar.MINUTE, durationInMinutes);
+ return calendar.getTime();
+ }
+
+ public boolean segmentsConflict(Date lhs, int lhsDurationMins, Date rhs,
+ int rhsDurationMins) {
+ Date lhsEnd = createEndDate(lhs, lhsDurationMins);
+ Date rhsEnd = createEndDate(rhs, rhsDurationMins);
+
+ return isOnToJustBefore(lhs, rhs, rhsEnd) || isStrictlyWithin(lhsEnd, rhs, rhsEnd)
+ || isOnToJustBefore(rhs, lhs, lhsEnd)
+ || isStrictlyWithin(rhsEnd, lhs, lhsEnd);
+ }
+
+ private boolean isOnToJustBefore(Date date, Date rangeBegin, Date rangeEnd) {
+ return date.equals(rangeBegin) || (date.after(rangeBegin) && date.before(rangeEnd));
+ }
+
+ private boolean isStrictlyWithin(Date date, Date rangeBegin, Date rangeEnd) {
+ return date.after(rangeBegin) && date.before(rangeEnd);
+ }
+
+ public Date addMinutesTo(Date fromDate, int minutes) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(fromDate);
+ calendar.add(Calendar.MINUTE, minutes);
+ return calendar.getTime();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.