Commit cbec81fc authored by Gerson Sunyé's avatar Gerson Sunyé
Browse files

design patterns

parent 698a904a
<?xml version="1.0" encoding="UTF-8"?>
<uml:Model xmi:version="20131001" xmlns:xmi="http://www.omg.org/spec/XMI/20131001" xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML" xmi:id="_LPz7kBCCEei8I66bYraWpA" name="NewModel"/>
<?xml version="1.0" encoding="UTF-8"?>
<viewpoint:DAnalysis xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:viewpoint="http://www.eclipse.org/sirius/1.1.0" xsi:schemaLocation="http://www.eclipse.org/sirius/description/1.1.0 http://www.eclipse.org/sirius/1.1.0#//description" xmi:id="_LNY5sBCCEei8I66bYraWpA" selectedViews="_LSCwMBCCEei8I66bYraWpA _LSIPwBCCEei8I66bYraWpA _LSKE8BCCEei8I66bYraWpA _LSMhMBCCEei8I66bYraWpA _LSOWYBCCEei8I66bYraWpA" version="11.1.0.201608251200">
<semanticResources>.idea/compiler.xml</semanticResources>
<semanticResources>.idea/gradle.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__com_google_code_findbugs_jsr305_3_0_2.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_apache_commons_commons_lang3_3_7.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_0_0.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_0_3.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_0_3.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_params_5_0_3.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_0_3.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_0_3.xml</semanticResources>
<semanticResources>.idea/libraries/Gradle__org_opentest4j_opentest4j_1_0_0.xml</semanticResources>
<semanticResources>.idea/misc.xml</semanticResources>
<semanticResources>.idea/modules.xml</semanticResources>
<semanticResources>.idea/vcs.xml</semanticResources>
<semanticResources>.idea/workspace.xml</semanticResources>
<semanticResources>agenda.uml</semanticResources>
<ownedViews xmi:type="viewpoint:DView" xmi:id="_LSCwMBCCEei8I66bYraWpA">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.obeonetwork.dsl.uml2.design/description/uml2.odesign#//@ownedViewpoints[name='Reused']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" xmi:id="_LSIPwBCCEei8I66bYraWpA">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.obeonetwork.dsl.uml2.design/description/uml2.odesign#//@ownedViewpoints[name='Capture']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" xmi:id="_LSKE8BCCEei8I66bYraWpA">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.obeonetwork.dsl.uml2.design/description/uml2.odesign#//@ownedViewpoints[name='Design']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" xmi:id="_LSMhMBCCEei8I66bYraWpA">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.obeonetwork.dsl.uml2.design/description/uml2.odesign#//@ownedViewpoints[name='Review']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" xmi:id="_LSOWYBCCEei8I66bYraWpA">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.obeonetwork.dsl.uml2.design/description/uml2.odesign#//@ownedViewpoints[name='Extend']"/>
</ownedViews>
</viewpoint:DAnalysis>
package fr.unantes.agenda;
/**
* Created on 20/02/2018.
*
* @author sunye.
*/
public class AbstractEvent {
protected String id;
protected TimeSlot slot;
public AbstractEvent(String id, TimeSlot slot) {
this.id = id;
this.slot = slot;
}
public TimeSlot slot() {
return slot;
}
}
package fr.unantes.agenda;
/**
* Created on 20/02/2018.
*
* @author sunye.
*/
public interface Event {
boolean conflictsWith(Event other);
boolean conflictsWithSingleEvent(SingleEvent other);
boolean conflictsWithRecurrentEvent(RecurrentEvent other);
}
package fr.unantes.agenda;
import java.time.temporal.ChronoUnit;
/**
* Created on 20/02/2018.
*
* @author sunye.
*/
public enum Frequency {
DAILY(ChronoUnit.DAYS),
WEEKLY(ChronoUnit.WEEKS),
MONTHLY(ChronoUnit.MONTHS),
YEARL (ChronoUnit.YEARS);
private ChronoUnit unit;
Frequency(ChronoUnit unit) {
this.unit = unit;
}
public ChronoUnit unit() {
return unit;
}
}
......@@ -15,8 +15,20 @@ public class Interval<T extends Comparable> {
this.end = end;
}
public T begin() {
return begin;
}
public T end() {
return end;
}
public boolean includes(T value) {
return value.compareTo(begin) >= 0 && value.compareTo(end) <= 0;
}
public boolean overlapsWith(Interval<T> other) {
return false;
}
}
package fr.unantes.agenda;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;
/**
* Created on 20/02/2018.
*
* @author sunye.
*/
public class RecurrentEvent extends AbstractEvent implements Event {
private Frequency frequency;
private LocalDate reccurrenceEnd;
public RecurrentEvent(String id, TimeSlot slot, Frequency frequency, LocalDate reccurrenceEnd) {
super(id, slot);
this.frequency = frequency;
this.reccurrenceEnd = reccurrenceEnd;
}
public boolean conflictsWith(Event other) {
return other.conflictsWithRecurrentEvent(this);
}
@Override
public boolean conflictsWithSingleEvent(SingleEvent other) {
return false;
}
@Override
public boolean conflictsWithRecurrentEvent(RecurrentEvent other) {
return false;
}
public Collection<TimeSlot> occurrences() {
Collection<TimeSlot> calculatedOccurrences = new LinkedList<>();
for (TimeSlot i = this.slot(); i.isBeforeDate(reccurrenceEnd) ; i = i.add(1, frequency.unit())) {
calculatedOccurrences.add(i);
}
return calculatedOccurrences;
}
public static void main(String[] args) {
LocalDateTime begin = LocalDateTime.of(2015, Month.JULY, 29, 19, 30, 40);
LocalDate end = LocalDate.now();
RecurrentEvent re = new RecurrentEvent("id1", new TimeSlot(begin, Duration.ofHours(1)), Frequency.DAILY, end);
re.occurrences().stream().forEach(System.out::println);
}
}
package fr.unantes.agenda;
/**
* Created on 20/02/2018.
*
* @author sunye.
*/
public class SingleEvent extends AbstractEvent implements Event {
public SingleEvent(String id, TimeSlot slot) {
super(id, slot);
}
@Override
public boolean conflictsWith(Event other) {
return other.conflictsWithSingleEvent(this);
}
@Override
public boolean conflictsWithSingleEvent(SingleEvent other) {
return this.slot.overlapsWith(other.slot);
}
@Override
public boolean conflictsWithRecurrentEvent(RecurrentEvent other) {
return other.conflictsWithSingleEvent(this);
}
}
package fr.unantes.agenda;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* Created on 21/02/2018.
*
* @author sunye.
*/
public class TimeSlot {
private LocalDateTime start;
private Duration duration;
public TimeSlot(LocalDateTime start, Duration duration) {
this.start = start;
this.duration = duration;
}
public TimeSlot(LocalDateTime begin, LocalDateTime end) {
this.start = begin;
this.duration = Duration.between(begin, end);
}
public LocalDateTime start() {
return start;
}
public LocalDateTime end() {
return start.plus(duration);
}
public Duration duration() {
return duration;
}
public boolean includes(LocalDateTime dateTime) {
return dateTime.isAfter(this.start()) && dateTime.isBefore(this.end());
}
public boolean overlapsWith(TimeSlot other) {
return this.includes(other.start()) ||
this.includes(other.end()) ||
other.includes(other.start());
}
public TimeSlot add(long amountToAdd, ChronoUnit unit) {
return new TimeSlot(start.plus(amountToAdd,unit), duration);
}
public boolean isAfter(TimeSlot other) {
return this.start().isAfter(other.end());
}
public boolean isBefore(TimeSlot other) {
return other.isAfter(this);
}
public boolean isBeforeDate(LocalDate other) {
return this.start().toLocalDate().isBefore(other);
}
@Override
public String toString() {
return "TimeSlot{" +
"start=" + start +
", duration=" + duration +
'}';
}
}
package fr.unantes.event;
import java.util.Optional;
/**
* Created on 02/02/2018.
*
* @author sunye.
*/
public class Main {
public static void main(String[] args) {
Optional<String> value = Optional.empty();
if(value.isPresent())
value.orElse("rien");
Optional.of("ttt");
System.out.println(Optional.ofNullable(null).get());
}
}
package fr.unantes.utils;
import fr.unantes.agenda.TimeSlot;
import java.util.Comparator;
/**
* Created on 22/02/2018.
*
* @author sunye.
*/
public class OverlapComparator implements Comparator<TimeSlot> {
@Override
public int compare(TimeSlot one, TimeSlot other) {
if (one.overlapsWith(other)) {
return 0;
} else {
return one.start().compareTo(other.start());
}
}
}
\ No newline at end of file
package fr.unantes.agenda;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class IntervalTest {
private Interval<Integer> interval_1_10;
@BeforeEach
void setup() {
interval_1_10 = new Interval<>(0, 10);
}
@ParameterizedTest
@ValueSource(ints = {1, 5, 10})
void testIncludes(int value) {
assertTrue(interval_1_10.includes(value));
}
@ParameterizedTest
@ValueSource(ints = {Integer.MIN_VALUE, -1, 11, Integer.MAX_VALUE})
void testNotIncludes(int value) {
assertFalse(interval_1_10.includes(value));
}
}
\ No newline at end of file
package fr.unantes.agenda;
/**
* Created on 22/02/2018.
*
* @author sunye.
*/
public class TestEventIntersection {
}
// Modified from markdown.js from Hakim to handle external html files
(function () {
/*jslint loopfunc: true, browser: true*/
/*globals alert*/
'use strict';
var querySlidingHtml = function () {
var sections = document.querySelectorAll('[data-html]'),
section, j, jlen;
for (j = 0, jlen = sections.length; j < jlen; j++) {
section = sections[j];
if (section.getAttribute('data-html').length) {
var xhr = new XMLHttpRequest(),
url = section.getAttribute('data-html'),
cb = function () {
if (xhr.readyState === 4) {
if (
(xhr.status >= 200 && xhr.status < 300) ||
xhr.status === 0 // file protocol yields status code 0 (useful for local debug, mobile applications etc.)
) {
section.innerHTML = xhr.responseText;
} else {
section.outerHTML = '<section data-state="alert">ERROR: The attempt to fetch ' + url + ' failed with the HTTP status ' + xhr.status + '. Check your browser\'s JavaScript console for more details.</p></section>';
}
}
};
xhr.onreadystatechange = cb;
xhr.open('GET', url, false);
try {
xhr.send();
} catch (e) {
alert('Failed to get file' + url + '.' + e);
}
}
}
};
querySlidingHtml();
})();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment