Class FlagValueCalculator
Since there may be multiple overlapping regions, regions with differing priorities, regions with inheritance, flags with region groups assigned to them, and much more, the task of calculating the "effective" value of a flag is far from trivial. This class abstracts away the difficult with a number of methods for performing these calculations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Describes the membership result fromgetMembership(RegionAssociable)
. -
Constructor Summary
ConstructorsConstructorDescriptionFlagValueCalculator
(List<ProtectedRegion> regions, ProtectedRegion globalRegion) Create a new instance. -
Method Summary
Modifier and TypeMethodDescription<V> V
getEffectiveFlag
(ProtectedRegion region, Flag<V> flag, RegionAssociable subject) Get a region's state flag, checking parent regions until a value for the flag can be found (if one even exists).static <V> V
getEffectiveFlagOf
(ProtectedRegion region, Flag<V> flag, RegionAssociable subject) <V,
K> V getEffectiveMapValue
(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) static <V,
K> V getEffectiveMapValueOf
(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) getMembership
(RegionAssociable subject) Return the membership status of the given subject, indicating whether there are no (counted) regions in the list of regions, whether the subject is a member of all (counted) regions, or whether the subject is not a member of all (counted) regions.int
getPriority
(ProtectedRegion region) Get the effective priority of a region, overriding a region's priority when appropriate (i.e.static int
getPriorityOf
(ProtectedRegion region) <V> Collection
<V> queryAllValues
(RegionAssociable subject, Flag<V> flag) Get the effective values for a flag, returning a collection of all values.<V,
K> V queryMapValue
(RegionAssociable subject, MapFlag<K, V> flag, K key, Flag<V> fallback) Get the effective value for a key in aMapFlag
.queryState
(RegionAssociable subject, StateFlag flag) Get the effective value for a list of state flags.queryState
(RegionAssociable subject, StateFlag... flags) Get the effective value for a list of state flags.<V> V
queryValue
(RegionAssociable subject, Flag<V> flag) Get the effective value for a flag.
-
Constructor Details
-
FlagValueCalculator
Create a new instance.- Parameters:
regions
- a list of applicable regions that must be sorted according toNormativeOrders
globalRegion
- an optional global region (null to not use one)
-
-
Method Details
-
getMembership
Return the membership status of the given subject, indicating whether there are no (counted) regions in the list of regions, whether the subject is a member of all (counted) regions, or whether the subject is not a member of all (counted) regions.A region is "counted" if it doesn't have the
Flags.PASSTHROUGH
flag set toALLOW
and if there isn't another "counted" region with a higher priority. (The explicit purpose of the PASSTHROUGH flag is to have the region be skipped over in this check.)This method is mostly for internal use. It's not particularly useful.
- Parameters:
subject
- the subject- Returns:
- the membership result
-
queryState
Get the effective value for a list of state flags. The rules of states is observed here; that is,DENY
overridesALLOW
, andALLOW
overridesNONE
.A subject can be provided that is used to determine whether the value of a flag on a particular region should be used. For example, if a flag's region group is set to
RegionGroup.MEMBERS
and the given subject is not a member, then the region would be skipped when querying that flag. Ifnull
is provided for the subject, then only flags that useRegionGroup.ALL
,RegionGroup.NON_MEMBERS
, etc. will apply.- Parameters:
subject
- an optional subject, which would be used to determine the region group to applyflags
- a list of flags to check- Returns:
- a state
-
queryState
Get the effective value for a list of state flags. The rules of states is observed here; that is,DENY
overridesALLOW
, andALLOW
overridesNONE
.This method is the same as
queryState(RegionAssociable, StateFlag...)
.- Parameters:
subject
- an optional subject, which would be used to determine the region group to applyflag
- a flag to check- Returns:
- a state
-
queryValue
Get the effective value for a flag. If there are multiple values (for example, if there are multiple regions with the same priority but with different farewell messages set, there would be multiple completing values), then the selected (or "winning") value will depend on the flag type.Only some flag types actually have a strategy for picking the "best value." For most types, the actual value that is chosen to be returned is undefined (it could be any value). As of writing, the only type of flag that can consistently return the same 'best' value is
StateFlag
.A subject can be provided that is used to determine whether the value of a flag on a particular region should be used. For example, if a flag's region group is set to
RegionGroup.MEMBERS
and the given subject is not a member, then the region would be skipped when querying that flag. Ifnull
is provided for the subject, then only flags that useRegionGroup.ALL
,RegionGroup.NON_MEMBERS
, etc. will apply.- Parameters:
subject
- an optional subject, which would be used to determine the region group to applyflag
- the flag- Returns:
- a value, which could be
null
-
queryMapValue
@Nullable public <V,K> V queryMapValue(@Nullable RegionAssociable subject, MapFlag<K, V> flag, K key, Flag<V> fallback) Get the effective value for a key in aMapFlag
. If there are multiple values (for example, if there are multiple regions with the same priority but with different farewell messages set, there would be multiple completing values), then the selected (or "winning") value will be undefined.A subject can be provided that is used to determine whether the value of a flag on a particular region should be used. For example, if a flag's region group is set to
RegionGroup.MEMBERS
and the given subject is not a member, then the region would be skipped when querying that flag. Ifnull
is provided for the subject, then only flags that useRegionGroup.ALL
,RegionGroup.NON_MEMBERS
, etc. will apply.- Parameters:
subject
- an optional subject, which would be used to determine the region group to applyflag
- the flag of typeMapFlag
key
- the key for the map flag- Returns:
- a value, which could be
null
-
getEffectiveMapValue
@Nullable public <V,K> V getEffectiveMapValue(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) -
getEffectiveMapValueOf
@Nullable public static <V,K> V getEffectiveMapValueOf(ProtectedRegion region, MapFlag<K, V> mapFlag, K key, RegionAssociable subject) -
queryAllValues
Get the effective values for a flag, returning a collection of all values. It is up to the caller to determine which value, if any, from the collection will be used.A subject can be provided that is used to determine whether the value of a flag on a particular region should be used. For example, if a flag's region group is set to
RegionGroup.MEMBERS
and the given subject is not a member, then the region would be skipped when querying that flag. Ifnull
is provided for the subject, then only flags that useRegionGroup.ALL
,RegionGroup.NON_MEMBERS
, etc. will apply.- Parameters:
subject
- an optional subject, which would be used to determine the region group to applyflag
- the flag- Returns:
- a collection of values
-
getPriority
Get the effective priority of a region, overriding a region's priority when appropriate (i.e. with the global region).- Parameters:
region
- the region- Returns:
- the priority
-
getPriorityOf
-
getEffectiveFlag
@Nullable public <V> V getEffectiveFlag(ProtectedRegion region, Flag<V> flag, @Nullable RegionAssociable subject) Get a region's state flag, checking parent regions until a value for the flag can be found (if one even exists).- Parameters:
region
- the regionflag
- the flagsubject
- an subject object- Returns:
- the value
-
getEffectiveFlagOf
@Nullable public static <V> V getEffectiveFlagOf(ProtectedRegion region, Flag<V> flag, @Nullable RegionAssociable subject)
-