Created
September 29, 2017 09:35
-
-
Save DBarthe/0c87b9967f716eb2ec9c16e2e9ffdef8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ressources; | |
import java.util.Collection; | |
import java.util.HashSet;; | |
import java.util.stream.Collectors; | |
public class LicenceManager { | |
private HashSet<Licence> licences = new HashSet<>(); | |
/** | |
* Getter à utiliser pour toute opération add ou remove, etc... | |
* @return la collection de licence | |
*/ | |
public Collection<Licence> getLicences() { | |
return licences; | |
} | |
/** | |
* Liste les logiciels utilisables par une machine | |
* @param machineId | |
* @return | |
*/ | |
public Collection<Logiciel> getByMachine(String machineId) { | |
return getLicences() | |
.stream() | |
.filter(l -> l.getMachine().getId().equals(machineId)) | |
.map(l -> l.getLogiciel()) | |
.collect(Collectors.toList()); | |
} | |
/** | |
* Liste les machines pouvant utilisé un logiciel | |
* @param nomLogiciel | |
* @return | |
*/ | |
public Collection<Machine> getByLogiciel(String nomLogiciel) { | |
return getLicences() | |
.stream() | |
.filter(l -> l.getLogiciel().getNom().equals(nomLogiciel)).map(l -> l.getMachine()) | |
.collect(Collectors.toList()); | |
} | |
/** | |
* Attribue une licence de logiciel à une machine | |
* @param nomLogiciel | |
* @param machine | |
* @return true si une licence a bien été attribuée, false si la machine possède déjà la licence | |
* ou si aucune licence n'est disponible | |
*/ | |
public boolean attribue(String nomLogiciel, Machine machine) { | |
// test si la machine n'a pas dejà cette licence | |
return getLicences() | |
.stream() | |
.noneMatch(l ->l.getLogiciel().getNom().equals(nomLogiciel) && l.getMachine() != null && l.getMachine().getId().equals((machine.getId()))) | |
// test si il y a une licence dispo, et le cas échéant attribue cette licence | |
&& getLicences() | |
.stream() | |
.filter(l -> l.getLogiciel().getNom().equals(nomLogiciel) && l.getMachine() == null) | |
.findFirst().map(l -> { | |
l.setMachine(machine); | |
machine.getLicences().add(l); | |
return l; // pas vraiment un map, juste pour le one-statement | |
}) | |
.isPresent(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment