esmska.data
Class Gateways

java.lang.Object
  extended by esmska.data.Gateways

public class Gateways
extends java.lang.Object

Class managing all gateways


Nested Class Summary
static class Gateways.Events
          Events fired when gateway collection is changed.
 
Method Summary
 boolean add(Gateway gateway)
          Add new gateway
 boolean addAll(java.util.Collection<Gateway> gateways)
          Add new gateways
 void addValuedListener(ValuedListener<Gateways.Events,Gateway> valuedListener)
           
 void clear()
          Remove all gateways
 boolean contains(Gateway gateway)
          Search for an existing gateway
static java.lang.String convertDelayToHumanString(long delay, boolean inMilliseconds)
          Convert message delay to more human readable string delay.
 Gateway get(java.lang.String gatewayName)
          Find gateway by name.
 java.util.TreeSet<Gateway> getAll()
          Get collection of all gateways sorted by name
 java.util.HashSet<DeprecatedGateway> getDeprecatedGateways()
          Get set of currently deprecated gateways
 java.util.TreeSet<Gateway> getFavorites()
          Get gateways marked as favorites
 java.util.TreeSet<Gateway> getHidden()
          Get gateways marked as hidden
static Gateways getInstance()
          Get shared instance
 java.util.TreeSet<Gateway> getVisible()
          Get just the visible (non-hidden) gateways
 boolean isEmpty()
          Return if there are no gateways
static boolean isFakeGateway(java.lang.String gatewayName)
          Returns whether the gateway is a fake one (used just for development purposes).
static boolean isNumberPreferred(Gateway gateway, java.lang.String number)
          Returns whether gateway matches the number with its preferred prefixes.
static boolean isNumberSupported(Gateway gateway, java.lang.String number)
          Returns whether gateway matches the number with its supported prefixes.
static GatewayInfo parseInfo(java.net.URL script)
          Parse GatewayInfo implementation from the provided URL.
 boolean remove(Gateway gateway)
          Remove existing gateway
 boolean removeAll(java.util.Collection<Gateway> gateways)
          Remove existing gateways
 void removeValuedListener(ValuedListener<Gateways.Events,Gateway> valuedListener)
           
 void setDeprecatedGateways(java.util.Set<DeprecatedGateway> deprecatedGateways)
          Set currently deprecated gateways.
 int size()
          Return number of gateways
 Tuple<java.util.ArrayList<Gateway>,java.lang.Boolean> suggestGateway(java.lang.String number)
          Guess gateway according to phone number or phone number prefix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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)

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:
  1. Any fake gateway is disqualified.
  2. Any gateway that has some supported prefixes listed and yet is not matching the number is disqualified.
  3. If some gateways are marked as favorite:
    1. Discard gateways that have the number outside their preferred prefixes.
    2. If there are some remaining, return all of them, sorted by the number of contacts assigned. Selection ends here.
  4. If user has some contacts defined, count their numbers for each gateway:
    1. Discard gateways that have the number outside their preferred prefixes.
    2. Gateway with the highest number of contacts win.
    3. If multiple gateways have the same (highest) score, return all of them.
    4. Selection ends here.
  5. Do the last-resort algorithm:
    1. All gateways have 0 points by default.
    2. If a gateway requires login, but no credentials are filled in, subtract 1 point.
    3. If a gateway has preferred prefixes defined and they don't match the number, subtract 1 point.
    4. 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 - gateway
number - 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 - gateway
number - 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 delay
inMilliseconds - if true,then delay is 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 file
javax.script.ScriptException - when the script is not valid
java.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