Interface EnumMapDispatchHasher<DistinctType,ValueType>
- Type Parameters:
DistinctType- the distinct type used to indicate the implementation of aEnumMapDispatchHasher.ValueType- the abstract, base type to hash.
- All Known Implementing Classes:
ConsumeEffectType,NbtComponentType,ObjectContentsType
MapBuilders for a scenario in which there is an abstract type A (EnumMapDispatchHasher), indicated by a typeKey of EnumMapDispatchHasher,
and for each implementation of A, a MapBuilder exists, hashing an instance of the implementation.
This interface builds on the MapBuilder.dispatch(String, MinecraftHasher, Function, Function) method to achieve this.
Essentially, implementations of this interface, when grouped together in e.g. an enum, function as a fancy named map from instances of A to their respective hashers.
A common way of implementing this is as follows:
- Create an enum, implementing
EnumMapDispatchHasher, with the enum asEnumMapDispatchHasherand the base typeAasEnumMapDispatchHasher. - Add a
clazzfield to the enum, of typeClass<? extends A>, and amapBuilderfield, of typeMapBuilder<? extends A>.- The
clazzfield is used to recognise an implementation ofAand find an instance ofEnumMapDispatchHasherfor a certain instance of an implementation ofA.It is therefore important that the
clazzfield is unique for all constants in the enum. - The
mapBuilderfield is then used to hash that implementation ofA. - You can use a typed constructor like this to ensure that the type of
clazzis the same asmapBuilder, and to provide proper typing information for themapBuilder:<T extends A> MyExampleEnum(Class<T> clazz, MapBuilder<T> builder)In which
Tis an implementation ofA, andAis the base type to hash.
- The
- For each implementation of
A, add a constant to that enum, specifying theclazzof that implementation, and amapBuilderfor it.The name of the constant will be used to encode the value of the
typeKey, so be sure it is right and matches Java edition! - The methods of
EnumMapDispatchHasherare to be implemented as follows:distinction()returnsthis.valueTypeClass()returns theclazzfield.mapBuilder()returns themapBuilderfield.
- Finally, create the
MapBuilderwith thedispatch(Supplier)or thedispatch(String, Supplier)method, usingMyExampleEnum::valuesashashersSupplier.
Example implementations can be seen in ConsumeEffectType, NbtComponentType, and ObjectContentsType.
If a MinecraftHasher is required instead of a MapBuilder, use MinecraftHasher.mapBuilder(MapBuilder) to wrap the MapBuilder into a hasher.
The implementation described above uses the enum itself as EnumMapDispatchHasher for A, and then uses dispatch(String, Supplier), which uses MinecraftHasher.fromEnum() to
encode the enum constant for typeKey. This may not always be wanted behaviour, and this interface does allow using a different EnumMapDispatchHasher. In that case, the hasher for the EnumMapDispatchHasher has
to be given, using the dispatch(MinecraftHasher, Supplier) or the dispatch(String, MinecraftHasher, Supplier) method.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <Value,HasherEnum extends Enum<HasherEnum> & EnumMapDispatchHasher<HasherEnum, Value>>
MapBuilder<Value> Delegates todispatch(String, MinecraftHasher, Supplier), using theEnumMapDispatchHasherasDistincttype.static <Distinct,Value, HasherEnum extends EnumMapDispatchHasher<Distinct, Value>>
MapBuilder<Value> dispatch(String distinctTypeKey, MinecraftHasher<Distinct> distinctHasher, Supplier<HasherEnum[]> hashersSupplier) Creates aMapBuilderfor an abstract typeEnumMapDispatchHasher, using an array of implementations ofEnumMapDispatchHasher, and thedistinctTypeKeyto indicate theEnumMapDispatchHashertype of an implementation ofEnumMapDispatchHasher.static <Value,HasherEnum extends Enum<HasherEnum> & EnumMapDispatchHasher<HasherEnum, Value>>
MapBuilder<Value> static <Distinct,Value, HasherEnum extends EnumMapDispatchHasher<Distinct, Value>>
MapBuilder<Value> dispatch(MinecraftHasher<Distinct> distinctHasher, Supplier<HasherEnum[]> hashersSupplier) static <Value,HasherEnum extends EnumMapDispatchHasher<?, Value>>
MapBuilder<Value> dispatchFuzzy(Supplier<HasherEnum[]> hashersSupplier) Creates aMapBuilderfor an abstract typeEnumMapDispatchHasher, using an array of implementations ofEnumMapDispatchHasher.Returns theEnumMapDispatchHasherof this implementation ofEnumMapDispatchHasher.MapBuilder<? extends ValueType>
-
Method Details
-
distinction
DistinctType distinction()Returns theEnumMapDispatchHasherof this implementation ofEnumMapDispatchHasher.Implementations must ensure that each
EnumMapDispatchHasheris unique and only for one implementation ofEnumMapDispatchHasher.- Returns:
- the
EnumMapDispatchHasherof this implementation ofEnumMapDispatchHasher.
-
valueTypeClass
- Returns:
- the
Classof this implementation ofEnumMapDispatchHasher.
-
mapBuilder
MapBuilder<? extends ValueType> mapBuilder()- Returns:
- the
MapBuilderused to encode this implementation ofEnumMapDispatchHasher.
-
dispatchFuzzy
static <Value,HasherEnum extends EnumMapDispatchHasher<?, MapBuilder<Value> dispatchFuzzyValue>> (Supplier<HasherEnum[]> hashersSupplier) Creates aMapBuilderfor an abstract typeEnumMapDispatchHasher, using an array of implementations ofEnumMapDispatchHasher. ThisMapBuilderencodes theEnumMapDispatchHasherfuzzily, as in, there is notypekey indicating theDistinctType. As such, implementing aDistinctTypeto indicate the type is not strictly necessary.- Type Parameters:
Value- the abstract, base type to create aMapBuilderfor.- Parameters:
hashersSupplier- a supplier returning the array ofEnumMapDispatchHasherimplementations, one for each implementation ofEnumMapDispatchHasher.- Returns:
- the created
MapBuilder.
-
dispatch
static <Value,HasherEnum extends Enum<HasherEnum> & EnumMapDispatchHasher<HasherEnum, MapBuilder<Value> dispatchValue>> (Supplier<HasherEnum[]> hashersSupplier) - See Also:
-
dispatch
static <Value,HasherEnum extends Enum<HasherEnum> & EnumMapDispatchHasher<HasherEnum, MapBuilder<Value> dispatchValue>> (String typeKey, Supplier<HasherEnum[]> hashersSupplier) Delegates todispatch(String, MinecraftHasher, Supplier), using theEnumMapDispatchHasherasDistincttype.Uses
MinecraftHasher.fromEnum()as hasher forEnumMapDispatchHasher/theDistincttype, so be sure that the enum constants match what you want to appear in the map!- See Also:
-
dispatch
static <Distinct,Value, MapBuilder<Value> dispatchHasherEnum extends EnumMapDispatchHasher<Distinct, Value>> (MinecraftHasher<Distinct> distinctHasher, Supplier<HasherEnum[]> hashersSupplier) - See Also:
-
dispatch
static <Distinct,Value, MapBuilder<Value> dispatchHasherEnum extends EnumMapDispatchHasher<Distinct, Value>> (String distinctTypeKey, MinecraftHasher<Distinct> distinctHasher, Supplier<HasherEnum[]> hashersSupplier) Creates aMapBuilderfor an abstract typeEnumMapDispatchHasher, using an array of implementations ofEnumMapDispatchHasher, and thedistinctTypeKeyto indicate theEnumMapDispatchHashertype of an implementation ofEnumMapDispatchHasher.- Type Parameters:
Distinct- the distinct type used to indicate an implementation ofEnumMapDispatchHasher.Value- the abstract, base type to create aMapBuilderfor.- Parameters:
distinctTypeKey- the key in the map used to indicate theEnumMapDispatchHashertype.distinctHasher- the hasher used to hash theEnumMapDispatchHashertype.hashersSupplier- a supplier returning the array ofEnumMapDispatchHasherimplementations, one for each implementation ofEnumMapDispatchHasher.- Returns:
- the created
MapBuilder.
-