Commit 6cb7d95d authored by Saad MAHBOUBI's avatar Saad MAHBOUBI 💬
Browse files

fin ex 10 debut ex 2.3 (event)

parent c006308c
Pipeline #27224 passed with stages
in 1 minute and 16 seconds
...@@ -37,5 +37,11 @@ ...@@ -37,5 +37,11 @@
<version>3.19.0</version> <version>3.19.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.atlanmod.commons:commons-core:1.0.5" level="project" />
<orderEntry type="library" name="Maven: com.github.ben-manes.caffeine:caffeine:2.8.0" level="project" />
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:2.10.0" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.3.3" level="project" />
<orderEntry type="library" name="Maven: net.openhft:zero-allocation-hashing:0.9" level="project" />
<orderEntry type="library" name="Maven: de.ruedigermoeller:fst:2.57" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.8" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" />
<orderEntry type="library" name="Maven: org.objenesis:objenesis:2.5.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.19.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.7.0" level="project" />
</component>
</module>
\ No newline at end of file
package fr.unantes.l3.construction.events;
import java.time.Period;
import static org.atlanmod.commons.Preconditions.checkNotNull;
public abstract class Event {
private String id;
private Period slot;
public String getId() {
return id;
}
public void setId(String id) {
checkNotNull(id);
this.id = id;
}
public void setSlot(Period slot){
checkNotNull(slot);
this.slot = slot;
}
}
package fr.unantes.l3.construction.events;
public enum Frequency {
DAILY,
WEEKLY,
MONTHLY,
YEARLY
}
package fr.unantes.l3.construction.exercice10;
import static org.atlanmod.commons.Preconditions.checkGreaterThan;
public class Interval <T extends Comparable<T>> {
private final T begin;
private final T end;
public Interval(T begin, T end) {
checkGreaterThan(end, begin);
this.begin = begin;
this.end = end;
}
public boolean includes(T value) {
return value.compareTo(begin) >= 0 && value.compareTo(end) <= 0;
}
public boolean overlaps(Interval<T> other) {
return this.includes(other.begin) || this.includes(other.end) || other.includes(begin);
}
}
package fr.unantes.l3.construction.exercice4et5;
public class Field {
}
\ No newline at end of file
package fr.unantes.l3.construction.exercice4et5;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
public class Reference<T> {
private Collection<T> contents;
protected Reference(Collection<T> contents) {
this.contents = contents;
}
public void add(T newElement) {
this.contents.add(newElement);
}
public void remove(T elementToRemove) {
this.contents.remove(elementToRemove);
}
public Iterator<T> iterator() {
return this.contents.iterator();
}
}
class UniqueReference<T> extends Reference<T> {
public UniqueReference() {
super(new HashSet<>());
}
}
class NonuniqueReference<T> extends Reference<T> {
public NonuniqueReference() {
super(new LinkedList<>());
}
}
\ No newline at end of file
package fr.unantes.l3.construction.exercice4et5;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
public class ReferenceToField {
private Collection<Field> fields = new LinkedList<Field>();
public void add(Field newField) {
this.fields.add(newField);
}
public void remove(Field fieldToRemove) {
this.fields.remove(fieldToRemove);
}
public Iterator<Field> iterator() {
return this.fields.iterator();
}
public Collection<Field> copy(Collection<Field> other){
return null;
}
}
\ No newline at end of file
package fr.unantes.l3.construction.exercice4et5;
public class Window {
private final ReferenceToField widgets = new ReferenceToField();
public ReferenceToField getWidgets(){
return widgets;
};
/* ex5
private final Reference<Fields> widgets = new Reference<>(new HashSet<>);
public Reference<Field> getWidgets(){
return widgets;
};
*/
}
\ No newline at end of file
package fr.unantes.l3.construction.exercice6; package fr.unantes.l3.construction.exercice6;
public class Action { public class Action {
private ReferenceToButton button = new ReferenceToButton(this); private ReferenceToButton button = new ReferenceToButton(this);
public ReferenceToButton button() { public ReferenceToButton button() {
return button; return button;
} }
} }
...@@ -31,15 +31,19 @@ public class ReferenceToAction { ...@@ -31,15 +31,19 @@ public class ReferenceToAction {
public void unSet() { public void unSet() {
if (action.isPresent()) { if (action.isPresent()) {
action.get().button().basicUnset(); opposite(action.get()).basicUnset();
this.basicUnset(); this.basicUnset();
} }
} }
private ReferenceToButton opposite(Action action) {
return action.button();
}
public void set(Action action) { public void set(Action action) {
this.basicSet(action); this.basicSet(action);
action.button().basicSet(this.container); opposite(action).basicSet(this.container);
} }
public Action get() { public Action get() {
......
package fr.unantes.l3.construction.exercice7;
public class Action {
private ReferenceToOne<Action, Button> button = new ReferenceToOne<>(this) {
@Override
protected ReferenceToOne<Button, Action> opposite(Button target) {
return target.action();
}
};
public ReferenceToOne<Action, Button> button() {
return button;
}
}
package fr.unantes.l3.construction.exercice7;
public class Button {
private ReferenceToOne<Button, Action> action = new ReferenceToOne<>(this) {
@Override
protected ReferenceToOne<Action, Button> opposite(Action target) {
return target.button();
}
};
public ReferenceToOne<Button, Action> action() {
return action;
}
}
package fr.unantes.l3.construction.exercice7;
import java.util.Optional;
public abstract class ReferenceToOne<C, T> {
private final C container;
private Optional<T> target = Optional.empty();
public ReferenceToOne(C container) {
this.container = container;
}
public boolean isSet() {
return target.isPresent();
}
public void basicSet(T newTarget) {
if (this.isSet()) {
this.unSet();
} else {
this.target = Optional.of(newTarget);
}
}
public void basicUnset() {
if (target.isPresent()) {
target = Optional.empty();
}
}
public void unSet() {
if (target.isPresent()) {
opposite(target.get()).basicUnset();
this.basicUnset();
}
}
protected abstract ReferenceToOne<T, C> opposite(T target);
public void set(T newTarget) {
this.basicSet(newTarget);
opposite(newTarget).basicSet(this.container);
}
public T get() {
return target.get();
}
}
package fr.unantes.l3.construction.exercice8;
public class Field {
private ReferenceToWindow window;
public Field() {
window = new ReferenceToWindow(this);
}
public ReferenceToWindow window() {
return window;
}
}
package fr.unantes.l3.construction.exercice8;
import java.util.ArrayList;
import java.util.Collection;
public class ReferenceToFields {
private final Window container;
private Collection<Field> targets = new ArrayList<>();
public ReferenceToFields(Window container) {
this.container = container;
}
public void add(Field f) {
f.window().unSet();
f.window().basicSet(this.container);
this.basicAdd(f);
}
public void remove(Field f) {
f.window().unSet();
this.basicRemove(f);
}
public void basicRemove(Field f) {
this.targets.remove(f);
}
public void basicAdd(Field f) {
this.targets.add(f);
}
public boolean contains(Field f) {
return this.targets.contains(f);
}
}
package fr.unantes.l3.construction.exercice8;
public class ReferenceToWindow {
private final Field container;
private Window target;
public ReferenceToWindow(Field container) {
this.container = container;
}
public Window get() {
return target;
}
public void set(Window target) {
this.unSet();
this.basicSet(target);
target.widgets().basicAdd(container);
}
public void basicSet(Window target) {
this.target = target;
}
public void unSet() {
if (this.isSet()) {
this.target.widgets().basicRemove(this.container);
target = null;
}
}
public boolean isSet() {
return target != null;
}
}
package fr.unantes.l3.construction.exercice8;
public class Window {
private ReferenceToFields widgets;
public Window() {
widgets = new ReferenceToFields(this);
}
public ReferenceToFields widgets() {
return widgets;
}
}
package fr.unantes.l3.construction.exercice10;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.assertj.core.api.Assertions.*;
class IntervalTest {
private Interval<Integer> interval_1_10;
@BeforeEach
void setup() {
interval_1_10 = new Interval<>(1, 10);
}
@Test
void testConstructWithCorrectValues() {
assertThat(interval_1_10).isNotNull();
}
@Test
void testConstructorWithInvalidValues() {
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> {
Interval<Integer> interval = new Interval<>(10, 1);
});
}
@ParameterizedTest()
@ValueSource(ints = {1, 5, 10})
void testIncludesValueInInterval(int value) {
assertThat(interval_1_10.includes(value)).isTrue();
}
@ParameterizedTest()
@ValueSource(ints = {0, 11, -10, 50})
void testIncludesValueNotInInterval(int value) {
assertThat(interval_1_10.includes(value)).isFalse();
}
@Test
void testOverlapsWhenIntervalContainsBegin() {
Interval<Integer> other = new Interval<>(5, 15);
assertThat(interval_1_10.overlaps(other)).isTrue();
}
@Test
void testOverlapsWhenIntervalContainsEnd() {
Interval<Integer> other = new Interval<>(-5, 5);
assertThat(interval_1_10.overlaps(other)).isTrue();
}
@Test
void testOverlapsWhenOtherIntervalContainsThis() {
Interval<Integer> other = new Interval<>(0, 11);
assertThat(interval_1_10.overlaps(other)).isTrue();
}
@Test
void shouldNotOverlapWhenOtherIntervalIsLower() {
Interval<Integer> other = new Interval<>(-10, 0);
assertThat(interval_1_10.overlaps(other)).isFalse();
}
@Test
void shouldNotOverlapWhenOtherIntervalIsHigher() {
Interval<Integer> other = new Interval<>(11, 50);
assertThat(interval_1_10.overlaps(other)).isFalse();
}
}
\ No newline at end of file
package fr.unantes.l3.construction.exercice8;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class TestReferenceToFields {
@Test
public void testAddField() {
Window window = new Window();
Field f1 = new Field();
window.widgets().add(f1);
assertThat(window.widgets().contains(f1)).isTrue();
}
@Test
public void testAddTwoFields() {
Window window = new Window();
Field f1 = new Field();
Field f2 = new Field();
window.widgets().add(f1);
window.widgets().add(f2);
assertThat(window.widgets().contains(f1)).isTrue();
assertThat(window.widgets().contains(f2)).isTrue();
}
@Test
public void testOppositeRemove() {
Window window = new Window();
Field f1 = new Field();
window.widgets().add(f1);
window.widgets().remove(f1);
assertThat(f1.window().isSet()).isFalse();
}
@Test
public void testOppositeReference() {
Window window = new Window();
Field f1 = new Field();
window.widgets().add(f1);
assertThat(f1.window().get()).isEqualTo(window);
}