137 lines
4.7 KiB
Java
137 lines
4.7 KiB
Java
package src.fr.impl;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
import java.util.Objects;
|
|
import src.fr.api.PartType;
|
|
|
|
public class CompatibilityManagerImpl implements src.fr.api.CompatibilityManager {
|
|
|
|
private HashMap<PartType, Set<PartType>> incompatibilities;
|
|
private HashMap<PartType, Set<PartType>> requirements;
|
|
|
|
public CompatibilityManagerImpl() {
|
|
this.incompatibilities = new HashMap<>();
|
|
this.requirements = new HashMap<>();
|
|
}
|
|
|
|
@Override
|
|
public void addIncompatibilities(PartType reference, Set<PartType> target){
|
|
//Précondition
|
|
Objects.requireNonNull(reference);
|
|
Objects.requireNonNull(target);
|
|
|
|
|
|
Set<PartType> incompa = this.getIncompatibilities(reference);
|
|
Set<PartType> require = this.getRequirements(reference);
|
|
|
|
if(incompa.isEmpty()){
|
|
incompatibilities.put(reference, target);
|
|
}else {
|
|
for(PartType x: target){
|
|
if (incompa.contains(x)){
|
|
System.out.println("We have it already");
|
|
}else {
|
|
if(require.contains(x)){
|
|
System.out.println("Cannot remove");
|
|
}else{
|
|
//Ajout ref à x
|
|
incompa.add(x);
|
|
System.out.println("Add "+reference.getName() +" incompatible with "+x.getName());
|
|
|
|
//Ajoute x à ref
|
|
this.getIncompatibilities(x).add(reference);
|
|
System.out.println("Add "+x.getName() +" incompatible with "+reference.getName());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void removeIncompatibility(PartType reference, PartType target){
|
|
//Précondition
|
|
Objects.requireNonNull(reference);
|
|
Objects.requireNonNull(target);
|
|
|
|
Set<PartType> incompa = this.getIncompatibilities(reference);
|
|
if(incompa.isEmpty()){
|
|
System.out.println("No part incompatible");
|
|
}else{
|
|
if(incompa.contains(target)){
|
|
incompa.remove(target);
|
|
//Remove target -> ref
|
|
this.getIncompatibilities(target).remove(reference);
|
|
}else {
|
|
System.out.println("This part doesn't exist in incompatibilities list");
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void addRequirements(PartType reference, Set<PartType> target){
|
|
//Précondition
|
|
Objects.requireNonNull(reference);
|
|
Objects.requireNonNull(target);
|
|
|
|
Set<PartType> incompa = this.getIncompatibilities(reference);
|
|
Set<PartType> require = this.getRequirements(reference);
|
|
if(require.isEmpty()){
|
|
requirements.put(reference, target);
|
|
}else {
|
|
for(PartType x: target){
|
|
if (require.contains(x)){
|
|
System.out.println("We have it already");
|
|
}else {
|
|
if(incompa.contains(x)){
|
|
System.out.println("Cannot remove");
|
|
}else{
|
|
//Ajout ref à x
|
|
require.add(x);
|
|
System.out.println("Add "+reference.getName() +" compatible with "+x.getName());
|
|
|
|
//Ajoute x à ref
|
|
this.getRequirements(x).add(reference);
|
|
System.out.println("Add "+x.getName() +" compatible with "+reference.getName());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void removeRequirement(PartType reference, PartType target){
|
|
//Précondition
|
|
Objects.requireNonNull(reference);
|
|
Objects.requireNonNull(target);
|
|
|
|
Set<PartType> require = this.getRequirements(reference);
|
|
if(require.isEmpty()){
|
|
System.out.println("No part compatible");
|
|
}else{
|
|
if(require.contains(target)){
|
|
require.remove(target);
|
|
//Remove target -> ref
|
|
this.getRequirements(target).remove(reference);
|
|
}else {
|
|
System.out.println("This part doesn't exist in compatibilities list");
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Set<PartType> getIncompatibilities(PartType reference) {
|
|
// Il faut verifier ref est existé dans Hashmap
|
|
//return this.incompatibilities.get(reference);
|
|
return this.incompatibilities.getOrDefault(reference, new HashSet<PartType>());
|
|
}
|
|
|
|
@Override
|
|
public Set<PartType> getRequirements(PartType reference) {
|
|
//return this.requirements.get(reference);
|
|
return this.requirements.getOrDefault(reference, new HashSet<PartType>());
|
|
}
|
|
|
|
} |