Package com.github.alex1304.rdi
Class ServiceReference<T>
- java.lang.Object
-
- com.github.alex1304.rdi.ServiceReference<T>
-
- Type Parameters:
T
- the generic type of the service class
public class ServiceReference<T> extends Object
Refers to a specific service. A service is identified by a name and its class. As such, several services of the same class may live in the application, even if they are declared as singleton, as long as they have a different name. If the service name is not important in your application, you may refer to a service only by its class viaofType(Class)
. In that case, the name of the service will be the name of the class as byClass.getName()
.This class implements
equals(Object)
andhashCode()
on the name field only, meaning that in your application you are not supposed to use two references with the same name but different classes. However, as said earlier, the other way around is possible.- See Also:
of(String, Class)
,ofType(Class)
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
Class<T>
getServiceClass()
Gets the class of the service that is referred to.String
getServiceName()
Gets the name of the service that is referred to.int
hashCode()
static <T> ServiceReference<T>
of(String serviceName, Class<T> serviceClass)
Creates a newServiceReference
with the given name and class.static <T> ServiceReference<T>
ofType(Class<T> serviceClass)
Creates a newServiceReference
with the given class.String
toString()
-
-
-
Method Detail
-
of
public static <T> ServiceReference<T> of(String serviceName, Class<T> serviceClass)
Creates a newServiceReference
with the given name and class.- Type Parameters:
T
- the generic type of the service class- Parameters:
serviceName
- the service nameserviceClass
- the service class- Returns:
- a new
ServiceReference
-
ofType
public static <T> ServiceReference<T> ofType(Class<T> serviceClass)
Creates a newServiceReference
with the given class. The name of the class as byClass.getName()
will be used as name for this reference.- Type Parameters:
T
- the generic type of the service class- Parameters:
serviceClass
- the service class- Returns:
- a new
ServiceReference
-
getServiceName
public String getServiceName()
Gets the name of the service that is referred to.- Returns:
- the service name
-
getServiceClass
public Class<T> getServiceClass()
Gets the class of the service that is referred to.- Returns:
- the service class
-
-