Package esmska.data
Class Gateways
- java.lang.Object
-
- esmska.data.Gateways
-
public class Gateways extends java.lang.ObjectClass managing all gateways
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classGateways.EventsEvents fired when gateway collection is changed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(Gateway gateway)Add new gatewaybooleanaddAll(java.util.Collection<Gateway> gateways)Add new gatewaysvoidaddValuedListener(ValuedListener<Gateways.Events,Gateway> valuedListener)voidclear()Remove all gatewaysbooleancontains(Gateway gateway)Search for an existing gatewaystatic java.lang.StringconvertDelayToHumanString(long delay, boolean inMilliseconds)Convert message delay to more human readable string delay.Gatewayget(java.lang.String gatewayName)Find gateway by name.java.util.TreeSet<Gateway>getAll()Get collection of all gateways sorted by namejava.util.HashSet<DeprecatedGateway>getDeprecatedGateways()Get set of currently deprecated gatewaysjava.util.TreeSet<Gateway>getFavorites()Get gateways marked as favoritesjava.util.TreeSet<Gateway>getHidden()Get gateways marked as hiddenstatic GatewaysgetInstance()Get shared instancejava.util.TreeSet<Gateway>getVisible()Get just the visible (non-hidden) gatewaysbooleanisEmpty()Return if there are no gatewaysstatic booleanisFakeGateway(java.lang.String gatewayName)Returns whether the gateway is a fake one (used just for development purposes).static booleanisNumberPreferred(Gateway gateway, java.lang.String number)Returns whether gateway matches the number with its preferred prefixes.static booleanisNumberSupported(Gateway gateway, java.lang.String number)Returns whether gateway matches the number with its supported prefixes.static GatewayInfoparseInfo(java.net.URL script)Parse GatewayInfo implementation from the provided URL.booleanremove(Gateway gateway)Remove existing gatewaybooleanremoveAll(java.util.Collection<Gateway> gateways)Remove existing gatewaysvoidremoveValuedListener(ValuedListener<Gateways.Events,Gateway> valuedListener)voidsetDeprecatedGateways(java.util.Set<DeprecatedGateway> deprecatedGateways)Set currently deprecated gateways.intsize()Return number of gatewaysTuple<java.util.ArrayList<Gateway>,java.lang.Boolean>suggestGateway(java.lang.String number)Guess gateway according to phone number or phone number prefix.
-
-
-
Method Detail
-
addValuedListener
public void addValuedListener(ValuedListener<Gateways.Events,Gateway> valuedListener)
-
removeValuedListener
public void removeValuedListener(ValuedListener<Gateways.Events,Gateway> valuedListener)
-
getInstance
public static Gateways getInstance()
Get shared instance
-
getAll
public java.util.TreeSet<Gateway> getAll()
Get collection of all gateways sorted by name
-
add
public boolean add(Gateway gateway)
Add new gateway- Parameters:
gateway- new gateway, not null- Returns:
- See
Collection.add(E)
-
addAll
public boolean addAll(java.util.Collection<Gateway> gateways)
Add new gateways- Parameters:
gateways- collection of gateways, not null, no null element- Returns:
- See
Collection.addAll(java.util.Collection<? extends E>)
-
remove
public boolean remove(Gateway gateway)
Remove existing gateway- Parameters:
gateway- gateway to be removed, not null- Returns:
- See
Collection.remove(java.lang.Object)
-
removeAll
public boolean removeAll(java.util.Collection<Gateway> gateways)
Remove existing gateways- Parameters:
gateways- collection of gateways to be removed, not null, no null element- Returns:
- See
Collection.removeAll(java.util.Collection<?>)
-
clear
public void clear()
Remove all gateways
-
contains
public boolean contains(Gateway gateway)
Search for an existing gateway- Parameters:
gateway- gateway to be searched, not null- Returns:
- See
Collection.contains(java.lang.Object)
-
size
public int size()
Return number of gateways- Returns:
- See
Collection.size()
-
isEmpty
public boolean isEmpty()
Return if there are no gateways- Returns:
- See
Collection.isEmpty()
-
getDeprecatedGateways
public java.util.HashSet<DeprecatedGateway> getDeprecatedGateways()
Get set of currently deprecated gateways
-
setDeprecatedGateways
public void setDeprecatedGateways(java.util.Set<DeprecatedGateway> deprecatedGateways)
Set currently deprecated gateways. May be null to clear them.
-
get
public Gateway get(java.lang.String gatewayName)
Find gateway by name.- Parameters:
gatewayName- Name of the gateway. Search is case sensitive. May be null.- Returns:
- Gateway implementation, when a gateway with such name is found. If multiple such gateways are found, returns the first one found. Returns null if no gateway is found or provided name was null.
-
isFakeGateway
public static boolean isFakeGateway(java.lang.String gatewayName)
Returns whether the gateway is a fake one (used just for development purposes).
-
suggestGateway
public Tuple<java.util.ArrayList<Gateway>,java.lang.Boolean> suggestGateway(java.lang.String number)
Guess gateway according to phone number or phone number prefix. Searches through all visible (non-hidden) gateways and finds the best suited ones (one or many) supporting this phone number.
Algorithm:- Any fake gateway is disqualified.
- Any gateway that has some supported prefixes listed and yet is not matching the number is disqualified.
- If some gateways are marked as favorite:
- Discard gateways that have the number outside their preferred prefixes.
- If there are some remaining, return all of them, sorted by the number of contacts assigned. Selection ends here.
- If user has some contacts defined, count their numbers for each gateway:
- Discard gateways that have the number outside their preferred prefixes.
- Gateway with the highest number of contacts win.
- If multiple gateways have the same (highest) score, return all of them.
- Selection ends here.
- Do the last-resort algorithm:
- All gateways have 0 points by default.
- If a gateway requires login, but no credentials are filled in, subtract 1 point.
- If a gateway has preferred prefixes defined and they don't match the number, subtract 1 point.
- Return all gateways with the highest score. Selection ends here.
- Parameters:
number- phone number or its prefix. The minimum length is two characters, for shorter input (or null) the method does nothing.- Returns:
- tuple consisting of: 1. list of suggested gateways (may be empty); 2. boolean whether this suggestion is recommended (the decision was based on favorite gateways or the number of gateways users) or completely arbitrary (the last-resort algorithm was used).
-
isNumberSupported
public static boolean isNumberSupported(Gateway gateway, java.lang.String number)
Returns whether gateway matches the number with its supported prefixes.- Parameters:
gateway- gatewaynumber- phone number- Returns:
- true if at least one of supported prefixes matches the number or if the gateway does not have any supported prefixes; false otherwise
-
isNumberPreferred
public static boolean isNumberPreferred(Gateway gateway, java.lang.String number)
Returns whether gateway matches the number with its preferred prefixes.- Parameters:
gateway- gatewaynumber- phone number- Returns:
- true if at least one of preferred prefixes matches the number or if the gateway does not have any preferred prefixes; false otherwise (or when gateway or number is null, or if number is shorter then 2 characters)
-
convertDelayToHumanString
public static java.lang.String convertDelayToHumanString(long delay, boolean inMilliseconds)Convert message delay to more human readable string delay.- Parameters:
delay- number of seconds (or milliseconds) of the delayinMilliseconds- if true,thendelayis specified in milliseconds, otherwise in seconds- Returns:
- human readable string of the delay, eg: "3h 15m 47s"
-
parseInfo
public static GatewayInfo parseInfo(java.net.URL script) throws java.io.IOException, javax.script.ScriptException, java.beans.IntrospectionException
Parse GatewayInfo implementation from the provided URL.- Parameters:
script- URL (file or jar) of gateway script- Returns:
- GatewayInfo implementation
- Throws:
java.io.IOException- when there is problem accessing the script filejavax.script.ScriptException- when the script is not validjava.beans.IntrospectionException- when current JRE does not support JavaScript execution
-
getFavorites
public java.util.TreeSet<Gateway> getFavorites()
Get gateways marked as favorites
-
getHidden
public java.util.TreeSet<Gateway> getHidden()
Get gateways marked as hidden
-
getVisible
public java.util.TreeSet<Gateway> getVisible()
Get just the visible (non-hidden) gateways
-
-