Class MutableLinearPreferenceImpl
- java.lang.Object
-
- io.github.oliviercailloux.j_voting.preferences.classes.MutableLinearPreferenceImpl
-
- All Implemented Interfaces:
MutableLinearPreference
,Preference
public class MutableLinearPreferenceImpl extends Object implements MutableLinearPreference
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MutableLinearPreferenceImpl.MutableLinearIteratorDecorator
static class
MutableLinearPreferenceImpl.MutableLinearSetDecorator
In the future, ideally, sets returned via this decorator should be protected from alteration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addAlternative(Alternative a)
Adds the specified alternative at the last rank of this preference if it is not already present.Graph<Alternative>
asGraph()
In the graph : a relation from a to b means "a is at least as good as b".boolean
changeOrder(Alternative alternative, int rank)
Moves an existing alternative to the desired rank in the preference.boolean
equals(Object o2)
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 MutableLinearPreference
given(Voter voter, List<Alternative> list)
int
hashCode()
boolean
removeAlternative(Alternative a)
Removes the specified alternative from this preference if it is present.boolean
swap(Alternative alternative1, Alternative alternative2)
This method enables to swap 2 alternatives of the preference.String
toString()
-
-
-
Method Detail
-
given
public static MutableLinearPreference given(Voter voter, List<Alternative> list)
- Parameters:
list
- is a List of alternatives representing the preference.voter
- is the Voter associated to the Preference.- Returns:
- the mutable linear preference
-
changeOrder
public boolean changeOrder(Alternative alternative, int rank)
Description copied from interface:MutableLinearPreference
Moves an existing alternative to the desired rank in the preference. All the intermediate alternatives shift. More precisely, the given alternative is swapped with its neighbor until it reaches the given rank.- Specified by:
changeOrder
in interfaceMutableLinearPreference
- Parameters:
alternative
- that we're going to move in the preferencerank
- is the new rank where the alternative will be. The first alternative is at the rank 1. The rank must be less than 1 or greater than the rank of the last alternative.- Returns:
- true if the preference has changed after this call. In other words, if the alternative was not already at this rank.
-
removeAlternative
public boolean removeAlternative(Alternative a)
Description copied from interface:MutableLinearPreference
Removes the specified alternative from this preference if it is present.- Specified by:
removeAlternative
in interfaceMutableLinearPreference
- Parameters:
a
- to be removed from this preference, if present- Returns:
- true if this set contained the specified alternative
-
addAlternative
public boolean addAlternative(Alternative a)
Description copied from interface:MutableLinearPreference
Adds the specified alternative at the last rank of this preference if it is not already present.- Specified by:
addAlternative
in interfaceMutableLinearPreference
- Parameters:
a
- to be added to this preference- Returns:
- true if this preference did not already contain the specified 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:
- alternatives's set
-
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
-
asGraph
public Graph<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
-
swap
public boolean swap(Alternative alternative1, Alternative alternative2)
Description copied from interface:MutableLinearPreference
This method enables to swap 2 alternatives of the preference. (If the specified alternatives are equal, invoking this method leaves the preference unchanged.)- Specified by:
swap
in interfaceMutableLinearPreference
- Parameters:
alternative1
- that will change places with alternative2alternative2
- that will change places with alternative1- Returns:
- true if the preference has changed after this call (and, thus, the alternatives are contained in this preference). Or equivalently, false if the specified alternatives are equal and contained in this preference.
-
-