Class MutablePreferenceImpl
- java.lang.Object
-
- io.github.oliviercailloux.j_voting.preferences.classes.MutablePreferenceImpl
-
- All Implemented Interfaces:
MutablePreference
,Preference
public class MutablePreferenceImpl extends Object implements MutablePreference
Implements MutablePreference interface. The structure of a MutablePreference is a MutableGraph in which an edge represents the relation "at least as good as".- See Also:
Preference
,MutablePreference
-
-
Field Summary
Fields Modifier and Type Field Description protected Set<Alternative>
alternatives
protected MutableGraph<Alternative>
graph
protected Voter
voter
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAlternative(Alternative alternative)
Adds an alternative to the Preference.void
addEquivalence(Alternative a1, Alternative a2)
Adds an edge from an alternative a1 to an alternative a2 and from a2 to a1.ImmutableGraph<Alternative>
asGraph()
In the graph : a relation from a to b means "a is at least as good as b".MutableGraph<Alternative>
asMutableGraph()
This graph is not necessarily transitive and not necessarily reflexive.boolean
equals(Object obj)
Set<Alternative>
getAlternatives()
The returned set reads through this object: if this object is mutable, any modification to this object modifies the returned set, and conversely.Voter
getVoter()
static MutablePreferenceImpl
given(Preference pref)
Factory method making new MutablePreference from an other Preference.static MutablePreferenceImpl
given(Voter voter)
static MutablePreference
given(Voter voter, MutableGraph<Alternative> pref)
void
setAsLeastAsGood(Alternative a1, Alternative a2)
Adds an edge from an alternative a1 to an alternative a2, so that a1 is as least as good as a2 (a1 >= a2).String
toString()
-
-
-
Field Detail
-
voter
protected Voter voter
-
graph
protected MutableGraph<Alternative> graph
-
alternatives
protected Set<Alternative> alternatives
-
-
Method Detail
-
given
public static MutablePreference given(Voter voter, MutableGraph<Alternative> pref)
- Parameters:
pref
- is a mutable graph of alternatives representing the preference. Each node of the graph contains a set, if this set contains several alternatives, it means that those alternatives are ex-aequovoter
- is the Voter associated to the Preference.- Returns:
- the mutable preference
- See Also:
Voter
-
given
public static MutablePreferenceImpl given(Voter voter)
- Parameters:
voter
- is the Voter associated to the Preference.- Returns:
- a MutablePreference with the same voter and an empty graph
-
given
public static MutablePreferenceImpl given(Preference pref)
Factory method making new MutablePreference from an other Preference. It creates a new similar graph instance (mutable). The voter instance of the created preference is the same as the copied preference.- Parameters:
pref
- a Preference- Returns:
- a copy of this preference as a MutablePreference, with the same voter.
-
asGraph
public ImmutableGraph<Alternative> asGraph()
Description copied from interface:Preference
In the graph : a relation from a to b means "a is at least as good as b". This graph can’t be modified, but is not necessarily immutable. Reflexive and transitive.- Specified by:
asGraph
in interfacePreference
- Returns:
- the Graph corresponding to the Preference
-
asMutableGraph
public MutableGraph<Alternative> asMutableGraph()
Description copied from interface:MutablePreference
This graph is not necessarily transitive and not necessarily reflexive.- Specified by:
asMutableGraph
in interfaceMutablePreference
-
addAlternative
public void addAlternative(Alternative alternative)
Description copied from interface:MutablePreference
Adds an alternative to the Preference. This alternative is not preferred to any other of the preference, it is being added isolated.- Specified by:
addAlternative
in interfaceMutablePreference
- Parameters:
alternative
- to add to the preference.
-
addEquivalence
public void addEquivalence(Alternative a1, Alternative a2)
Description copied from interface:MutablePreference
Adds an edge from an alternative a1 to an alternative a2 and from a2 to a1. If one of them is not in the graph, they are added. a1 and a2 are ex-aequo.- Specified by:
addEquivalence
in interfaceMutablePreference
- Parameters:
a1
- first alternativea2
- second alternative
-
setAsLeastAsGood
public void setAsLeastAsGood(Alternative a1, Alternative a2)
Description copied from interface:MutablePreference
Adds an edge from an alternative a1 to an alternative a2, so that a1 is as least as good as a2 (a1 >= a2). If one of them is not in the graph, they are added.- Specified by:
setAsLeastAsGood
in interfaceMutablePreference
- Parameters:
a1
- preferred alternative to a2a2
- "lower" alternative
-
getAlternatives
public Set<Alternative> getAlternatives()
Description copied from interface:Preference
The returned set reads through this object: if this object is mutable, any modification to this object modifies the returned set, and conversely.- Specified by:
getAlternatives
in interfacePreference
- Returns:
- an immutable set of all alternatives of the preference
-
getVoter
public Voter getVoter()
- Specified by:
getVoter
in interfacePreference
- Returns:
Voter
instance of the preference, Returns the voter 0 if no specific voter is associated to this preference
-
-