public abstract class Service extends Object
ServiceLoader
if available, but backports the functionality to
previous Java versions and adds some error handling to ignore misconfigured
service implementations.
Additionally this class can be used as a base class for implementing services that are differentiated by a String key. An example implementation might be:
public abstract class StringConverter extends org.neo4j.commons.Service
{
protected StringConverter(String id)
{
super( id );
}
public abstract String convert( String input );
public static StringConverter load( String id )
{
return org.neo4j.commons.Service.load( StringConverter.class, id );
}
}
With for example these implementations:
public final class UppercaseConverter extends StringConverter
{
public UppercaseConverter()
{
super( "uppercase" );
}
public String convert( String input )
{
return input.toUpperCase();
}
}
public final class ReverseConverter extends StringConverter
{
public ReverseConverter()
{
super( "reverse" );
}
public String convert( String input )
{
char[] chars = input.toCharArray();
for ( int i = 0; i < chars.length/2; i++ )
{
char intermediate = chars[i];
chars[i] = chars[chars.length-1-i];
chars[chars.length-1-i] = chars[i];
}
return new String( chars );
}
}
This would then be used as:
String atad = StringConverter.load( "reverse" ).convert( "data" );
Modifier and Type | Class and Description |
---|---|
static class |
Service.CaseInsensitiveService
A base class for services, similar to
Service , that compares keys
using case insensitive comparison instead of exact comparison. |
static interface |
Service.Implementation
Designates that a class implements the specified service and should be
added to the services listings file (META-INF/services/[service-name]).
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o) |
Iterable<String> |
getKeys() |
int |
hashCode() |
static <T> Iterable<T> |
load(Class<T> type)
Load all implementations of a Service.
|
static <T extends Service> |
load(Class<T> type,
String key)
Load the Service implementation with the specified key.
|
static <T extends Service> |
loadSilently(Class<T> type,
String key)
Load the Service implementation with the specified key.
|
boolean |
matches(String key) |
String |
toString() |
public static <T> Iterable<T> load(Class<T> type)
T
- the type of the Servicetype
- the type of the Service to loadpublic static <T extends Service> T loadSilently(Class<T> type, String key)
T
- the type of the Service to loadtype
- the type of the Service to loadkey
- the key that identifies the desired implementationpublic static <T extends Service> T load(Class<T> type, String key)
T
- the type of the Servicetype
- the type of the Service to loadkey
- the key that identifies the desired implementationpublic boolean matches(String key)
Copyright © 2002–2016 The Neo4j Graph Database Project. All rights reserved.