Skip to content

Commit

Permalink
First open source commit for BNY Mellon Code Katas
Browse files Browse the repository at this point in the history
  • Loading branch information
donraab committed Sep 28, 2017
0 parents commit 565de4a
Show file tree
Hide file tree
Showing 78 changed files with 7,951 additions and 0 deletions.
21 changes: 21 additions & 0 deletions CalendarKata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# **What is the Calendar Kata?**

The Calendar Kata is an advanced kata which can help developers
become familiar with the [Java 8 Date/Time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html)
and [ThreeTen-Extra](http://www.threeten.org/threeten-extra/) libraries.

The domain for the Calendar kata is an object representation of an Outlook Calendar.
There are several domain classes that are shared by all of the exercises. These are
[`MyCalendar`](./src/main/java/bnymellon/codekatas/calendarkata/MyCalendar.java),
[`Meeting`](./src/main/java/bnymellon/codekatas/calendarkata/Meeting.java),
[`WorkWeek`](./src/main/java/bnymellon/codekatas/calendarkata/WorkWeek.java),
[`FullWeek`](./src/main/java/bnymellon/codekatas/calendarkata/FullWeek.java),
[`FullMonth`](./src/main/java/bnymellon/codekatas/calendarkata/FullMonth.java) and

![Diagram](mycalendar.png)
</p>

### How to get started

* There are failing tests in [`MyCalendarTest`](./src/test/java/bnymellon/codekatas/calendarkata/MyCalendarTest.java)
* Make the tests pass by following and completing the TODOs in MyCalendar, WorkWeek, FullMonth and FullWeek.
Binary file added CalendarKata/mycalendar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions CalendarKata/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017 BNY Mellon.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>bnymellon.codekatas</groupId>
<artifactId>calendarkata</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<eclipse-collections.version>9.0.0</eclipse-collections.version>
<threeten-extra.version>1.2</threeten-extra.version>
<junit.version>4.12</junit.version>
</properties>

<dependencies>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
<version>${eclipse-collections.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>${eclipse-collections.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-testutils</artifactId>
<version>${eclipse-collections.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.threeten</groupId>
<artifactId>threeten-extra</artifactId>
<version>${threeten-extra.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017 BNY Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bnymellon.codekatas.calendarkata;

import java.time.LocalDate;

import org.eclipse.collections.api.multimap.Multimap;
import org.threeten.extra.LocalDateRange;

public class CalendarWindow
{
LocalDateRange range;
Multimap<LocalDate, Meeting> meetings;

public LocalDate getStart()
{
return this.range.getStart();
}

public LocalDate getEnd()
{
return this.range.getEnd().minusDays(1);
}

public int getNumberOfMeetings()
{
return this.meetings.size();
}

protected String iterateMeetings()
{
StringBuilder builder = new StringBuilder();
this.range.stream().forEach(date -> {
builder.append("Date=" + date);
builder.append(" {Meetings= ");
builder.append(this.meetings.get(date));
builder.append("} ");
});
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2017 BNY Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bnymellon.codekatas.calendarkata;

import java.time.LocalDate;

import org.eclipse.collections.api.multimap.sortedset.SortedSetMultimap;
import org.threeten.extra.LocalDateRange;

public class FullMonth extends CalendarWindow
{
/**
* TODO Calculate the start, end and range of dates for the full month including the specified date.
*
* Hint: Look at {@link LocalDate#withDayOfMonth(int)}
* Hint: Look at {@link LocalDate#lengthOfMonth()}
* Hint: Look at {@link LocalDateRange#of(LocalDate, LocalDate)}
* Hint: The end date is exclusive in LocalDateRange
*/
public FullMonth(LocalDate forDate, SortedSetMultimap<LocalDate, Meeting> calendarMeetings)
{
LocalDate start = null;
LocalDate end = null;
this.range = null;
this.meetings = calendarMeetings.selectKeysValues(
(date, meeting) ->
date.getMonth().equals(start.getMonth()) &&
date.getYear() == start.getYear());
}

@Override
public String toString()
{
return "FullMonth(" +
"start=" + this.getStart() +
", end=" + this.getEnd() +
", meetings=" + this.iterateMeetings() +
')';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2017 BNY Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bnymellon.codekatas.calendarkata;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.eclipse.collections.api.multimap.sortedset.SortedSetMultimap;
import org.threeten.extra.LocalDateRange;

public class FullWeek extends CalendarWindow
{
/**
* TODO Calculate the start date and range of dates for the full seven day week from Sunday to Saturday.
*
* Hint: Look at {@link LocalDate#with(TemporalAdjuster)}
* Hint: Look at {@link TemporalAdjusters#previousOrSame(DayOfWeek)}
* Hint: Look at {@link LocalDate#plusDays(long)}
* Hint: Look at {@link LocalDateRange#of(LocalDate, LocalDate)}
* Hint: The end date is exclusive in LocalDateRange
*/
public FullWeek(LocalDate forDate, SortedSetMultimap<LocalDate, Meeting> calendarMeetings)
{
LocalDate start = null;
this.range = null;
this.meetings = calendarMeetings.selectKeysValues((date, meeting) -> this.range.contains(date));
}

@Override
public String toString()
{
return "FullWeek(" +
"start=" + this.getStart() +
", end=" + this.getEnd() +
", meetings=" + this.iterateMeetings() +
')';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2017 BNY Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bnymellon.codekatas.calendarkata;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Comparator;
import java.util.Objects;

import org.threeten.extra.Interval;

public class Meeting implements Comparable<Meeting>
{
public static final Comparator<Meeting> COMPARATOR = Comparator.comparing(Meeting::getDate).thenComparing(Meeting::getStartTime);
private String subject;
private LocalDate date;
private LocalTime startTime;
private Duration duration;
private ZoneId zoneId;

public Meeting(String subject, LocalDate date, LocalTime startTime, Duration duration, ZoneId zoneId)
{
this.subject = subject;
this.date = date;
this.startTime = startTime;
this.duration = duration;
this.zoneId = zoneId;
}

public String getSubject()
{
return this.subject;
}

public LocalDate getDate()
{
return this.date;
}

public LocalTime getStartTime()
{
return this.startTime;
}

public Duration getDuration()
{
return this.duration;
}

public ZoneId getZoneId()
{
return this.zoneId;
}

public LocalTime getEndTime()
{
return this.getStartTime().plus(this.getDuration());
}

public Interval getInterval()
{
return Interval.of(
LocalDateTime.of(this.date,this.startTime)
.atZone(this.zoneId)
.toInstant(),
this.duration);
}

@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || this.getClass() != o.getClass())
{
return false;
}
Meeting meeting = (Meeting) o;
return Objects.equals(this.getSubject(), meeting.getSubject()) &&
Objects.equals(this.getDate(), meeting.getDate()) &&
Objects.equals(this.getStartTime(), meeting.getStartTime()) &&
Objects.equals(this.getDuration(), meeting.getDuration()) &&
Objects.equals(this.getZoneId(), meeting.getZoneId());
}

@Override
public int hashCode()
{
int result = this.getSubject().hashCode();
result = 31 * result + this.getDate().hashCode();
result = 31 * result + this.getStartTime().hashCode();
result = 31 * result + this.getDuration().hashCode();
result = 31 * result + this.getZoneId().hashCode();
return result;
}

@Override
public String toString()
{
return "Meeting(" +
"subject='" + this.getSubject() + '\'' +
", date=" + this.getDate() +
", startTime=" + this.getStartTime() +
", duration=" + this.getDuration() +
", endTime=" + this.getEndTime() +
')';
}

@Override
public int compareTo(Meeting that)
{
return COMPARATOR.compare(this, that);
}
}
Loading

0 comments on commit 565de4a

Please sign in to comment.