The complete API consists of 2 annotations.
@ComponentMapKey
The @ComponentMapKey
annotation defines the method that returns the keys of the map. The annotated method return type is the type of the map's key.
Kotlin Java
Copy enum class CalculatorOperationType {
ADD, SUBTRACT, MULTIPLY, DIVIDE
}
internal interface CalculatorOperation {
@get : ComponentMapKey
val operationType: CalculatorOperationType
fun calculate (a: Int , b: Int ): Int
}
Copy public enum CalculatorOperationType {
ADD , SUBTRACT , MULTIPLY , DIVIDE
}
public interface CalculatorOperation {
@ ComponentMapKey
CalculatorOperationType getOperationType ();
int calculate ( int a , int b);
}
Lets implement few operations:
Kotlin Java
Copy internal class AddOperation : CalculatorOperation {
override val type: CalculatorOperationType = CalculatorOperationType.ADD
override fun calculate (a: Int , b: Int ): Int {
return a + b
}
}
internal class MultiplyOperation : CalculatorOperation {
override val type: CalculatorOperationType = CalculatorOperationType.MULTIPLY
override fun calculate (a: Int , b: Int ): Int {
return a * b
}
}
Copy public class AddOperation implements CalculatorOperation {
@ Override
public CalculatorOperationType getOperationType () {
return CalculatorOperationType . ADD ;
}
@ Override
public int calculate ( int a , int b) {
return a + b;
}
}
public class MultiplyOperation implements CalculatorOperation {
@ Override
public CalculatorOperationType getOperationType () {
return CalculatorOperationType . MULTIPLY ;
}
@ Override
public int calculate ( int a , int b) {
return a * b;
}
}
No annotations or any library specific code is required in the implementations.
@ComponentMap
Component Maps can only be injected into properties, other injection methods are not supported.
Add the @ComponentMap
annotation to map to inject.
Kotlin Java
Copy class CalculatorServiceImpl : CalculatorService {
@ComponentMap
private lateinit var operations: Map < CalculatorOperationType , CalculatorOperation >
override fun calculate (type: CalculatorOperationType , a: Int , b: Int ): Number {
val operationHandler = operations[type] ?: throw IllegalArgumentException ( "Unknown operation type: $type" )
return operationHandler. calculate (a, b)
}
}
Copy public class CalculatorServiceImpl implements CalculatorService {
@ ComponentMap
private Map < CalculatorOperationType , CalculatorOperation > operations;
@ Override
public int calculate ( CalculatorOperationType type , int a , int b) {
CalculatorOperation operationHandler = operations . get (type);
if (operationHandler == null ) {
throw new IllegalArgumentException( "Unknown operation type: $type" ) ;
}
return operationHandler . calculate (a , b);
}
}
The @ComponentMap
annotation can also inject list of beans to the map value:
Kotlin Java
Copy @ComponentMap
private lateinit var operations: Map < CalculatorOperationType , List < CalculatorOperation >>
Copy @ ComponentMap
private Map < CalculatorOperationType , List < CalculatorOperation >> operations;
By defining the value of the map as List
of beans the library will add all the beans of each key to their relevant list.
For example if you have multiple AnnouncementSubscriber
for each AnnouncementType
just change the type of the map value to List<AnnouncementSubscriber>
and for each AnnouncementType
the list will contain multiple subscribers.
Kotlin Java
Copy @ComponentMap
private lateinit var subscribers: Map < AnnouncementType , List < AnnouncementSubscriber >>
Copy @ ComponentMap
private Map < AnnouncementType , List < AnnouncementSubscriber >> subscribers;