Package net.sf.freecol.common.model
Class FeatureContainer
- java.lang.Object
-
- net.sf.freecol.common.model.FeatureContainer
-
public final class FeatureContainer extends java.lang.Object
A container to hold abilities and modifiers for some FreeColObject-subclass. - FreeColSpecObjectType, Europe, Player, Settlement are current implementors. - Building delegates some functionality to its type. - Unit fakes it by constructing a FeatureContainer on the fly. - FreeColObject itself implements a null version.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.util.Set<Ability>>
abilities
The abilities in the container.private java.lang.Object
abilitiesLock
Lock variables.private static java.util.logging.Logger
logger
private java.util.Map<java.lang.String,java.util.Set<Modifier>>
modifiers
The modifiers in the container.private java.lang.Object
modifiersLock
-
Constructor Summary
Constructors Constructor Description FeatureContainer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
abilitiesPresent()
Have the abilities map been created?boolean
addAbility(Ability ability)
Add the given ability to a container.void
addFeatures(FreeColObject fco)
Adds all the features in an object to this object.boolean
addModifier(Modifier modifier)
Adds a modifier to a container.static boolean
allAbilities(java.util.stream.Stream<Ability> abilities)
Is the given set of abilities non-empty and contains no false-valued members?static float
applyModifiers(float number, Turn turn, java.util.Collection<Modifier> mods)
Applies a collection of modifiers to the given float value.static float
applyModifiers(float number, Turn turn, java.util.stream.Stream<Modifier> mods)
Applies a stream of modifiers to the given float value.private static float
applyModifiersInternal(float number, Turn turn, java.util.Collection<Modifier> mods)
Implement applyModifiers.void
clear()
Clear this feature container.boolean
containsAbilityKey(java.lang.String key)
Checks if this container contains a given ability key.void
copy(FeatureContainer other)
Copy anther feature container.java.util.stream.Stream<Ability>
getAbilities(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of abilities with the given identifier from a container.protected java.util.Collection<Ability>
getAbilityValues()
java.util.stream.Stream<Modifier>
getModifiers(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of modifiers with the given identifier from this container.protected java.util.Collection<Modifier>
getModifierValues()
boolean
hasAbility(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Is the given set of abilities non-empty and contains no false-valued members?private boolean
modifiersPresent()
Have the modifiers map been created?void
removeAbilities(java.lang.String id)
Remove all abilities with a given identifier.Ability
removeAbility(Ability ability)
Remove the given ability from a container.void
removeFeatures(FreeColObject fco)
Removes all the features in an object from a container.Modifier
removeModifier(Modifier modifier)
Removes a modifier from a container.void
removeModifiers(java.lang.String id)
Removes all modifiers with a given identifier.void
replaceSource(FreeColSpecObjectType oldSource, FreeColSpecObjectType newSource)
Replaces the source field.private void
requireAbilities()
On demand creation of the abilities map.private void
requireModifiers()
On demand creation of the modifiers map.java.lang.String
toString()
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
abilitiesLock
private final java.lang.Object abilitiesLock
Lock variables.
-
modifiersLock
private final java.lang.Object modifiersLock
-
abilities
private java.util.Map<java.lang.String,java.util.Set<Ability>> abilities
The abilities in the container.
-
modifiers
private java.util.Map<java.lang.String,java.util.Set<Modifier>> modifiers
The modifiers in the container.
-
-
Method Detail
-
abilitiesPresent
private boolean abilitiesPresent()
Have the abilities map been created?- Returns:
- True if the abilities are present.
-
requireAbilities
private void requireAbilities()
On demand creation of the abilities map.
-
modifiersPresent
private boolean modifiersPresent()
Have the modifiers map been created?- Returns:
- True if the modifiers are present.
-
requireModifiers
private void requireModifiers()
On demand creation of the modifiers map.
-
getAbilityValues
protected java.util.Collection<Ability> getAbilityValues()
-
getModifierValues
protected java.util.Collection<Modifier> getModifierValues()
-
allAbilities
public static boolean allAbilities(java.util.stream.Stream<Ability> abilities)
Is the given set of abilities non-empty and contains no false-valued members?- Parameters:
abilities
- A stream ofAbility
s to check.- Returns:
- True if the abilities are `satisfied'.
-
hasAbility
public boolean hasAbility(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Is the given set of abilities non-empty and contains no false-valued members?- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the ability applies to.turn
- An optional applicableTurn
.- Returns:
- True if the ability is present.
-
containsAbilityKey
public boolean containsAbilityKey(java.lang.String key)
Checks if this container contains a given ability key.- Parameters:
key
- The key to check.- Returns:
- True if the key is present.
-
getAbilities
public java.util.stream.Stream<Ability> getAbilities(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of abilities with the given identifier from a container.- Parameters:
id
- The object identifier (null matches all).fcgot
- An optionalFreeColSpecObjectType
the ability applies to.turn
- An optional applicableTurn
.- Returns:
- A stream of abilities.
-
addAbility
public boolean addAbility(Ability ability)
Add the given ability to a container.- Parameters:
ability
- AnAbility
to add.- Returns:
- True if the
Ability
was added.
-
removeAbility
public Ability removeAbility(Ability ability)
Remove the given ability from a container.- Parameters:
ability
- AnAbility
to remove.- Returns:
- The ability removed or null on failure.
-
removeAbilities
public void removeAbilities(java.lang.String id)
Remove all abilities with a given identifier.- Parameters:
id
- The object identifier.
-
getModifiers
public java.util.stream.Stream<Modifier> getModifiers(java.lang.String id, FreeColSpecObjectType fcgot, Turn turn)
Gets the set of modifiers with the given identifier from this container.- Parameters:
id
- The object identifier.fcgot
- An optionalFreeColSpecObjectType
the modifier applies to.turn
- An optional applicableTurn
.- Returns:
- A stream of
Modifier
s.
-
applyModifiers
public static float applyModifiers(float number, Turn turn, java.util.Collection<Modifier> mods)
Applies a collection of modifiers to the given float value.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.mods
- TheModifier
s to apply.- Returns:
- The modified number.
-
applyModifiers
public static float applyModifiers(float number, Turn turn, java.util.stream.Stream<Modifier> mods)
Applies a stream of modifiers to the given float value.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.mods
- TheModifier
s to apply.- Returns:
- The modified number.
-
applyModifiersInternal
private static float applyModifiersInternal(float number, Turn turn, java.util.Collection<Modifier> mods)
Implement applyModifiers.- Parameters:
number
- The number to modify.turn
- An optional applicableTurn
.mods
- TheModifier
s to apply.- Returns:
- The modified number.
-
addModifier
public boolean addModifier(Modifier modifier)
Adds a modifier to a container.- Parameters:
modifier
- TheModifier
to add.- Returns:
- True if the modifier was added.
-
removeModifier
public Modifier removeModifier(Modifier modifier)
Removes a modifier from a container.- Parameters:
modifier
- TheModifier
to remove.- Returns:
- The modifier removed.
-
removeModifiers
public void removeModifiers(java.lang.String id)
Removes all modifiers with a given identifier.- Parameters:
id
- The object identifier.
-
addFeatures
public void addFeatures(FreeColObject fco)
Adds all the features in an object to this object.- Parameters:
fco
- TheFreeColObject
to add features from.
-
removeFeatures
public void removeFeatures(FreeColObject fco)
Removes all the features in an object from a container.- Parameters:
fco
- TheFreeColObject
to find features to remove in.
-
copy
public void copy(FeatureContainer other)
Copy anther feature container.- Parameters:
other
- The otherFeatureContainer
.
-
clear
public void clear()
Clear this feature container.
-
replaceSource
public void replaceSource(FreeColSpecObjectType oldSource, FreeColSpecObjectType newSource)
Replaces the source field. This is necessary because objects may inherit Features from other abstract objects.- Parameters:
oldSource
- The old sourceFreeColSpecObjectType
.newSource
- The new sourceFreeColSpecObjectType
.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-