| 1 |
package org.astrogrid.samp; |
| 2 |
|
| 3 |
import java.util.HashMap; |
| 4 |
import java.util.Iterator; |
| 5 |
import java.util.Map; |
| 6 |
import java.util.regex.Matcher; |
| 7 |
import java.util.regex.Pattern; |
| 8 |
|
| 9 |
/** |
| 10 |
* Represents the set of subscribed messages for a SAMP client. |
| 11 |
* This has the form of a Map in which each key is an MType (perhaps |
| 12 |
* wildcarded) and the corresponding values are maps with keys which are |
| 13 |
* so far undefined (thus typically empty). |
| 14 |
* |
| 15 |
* @author Mark Taylor |
| 16 |
* @since 14 Jul 2008 |
| 17 |
*/ |
| 18 |
public class Subscriptions extends SampMap { |
| 19 |
|
| 20 |
private static final String ATOM_REGEX = "[0-9a-zA-Z\\-_]+"; |
| 21 |
private static String MTYPE_REGEX = |
| 22 |
"(" + ATOM_REGEX + "\\.)*" + ATOM_REGEX; |
| 23 |
private static String MSUB_REGEX = |
| 24 |
"(" + MTYPE_REGEX + "|" + MTYPE_REGEX + "\\.\\*" + "|" + "\\*" + ")"; |
| 25 |
private static final Pattern MSUB_PATTERN = Pattern.compile( MSUB_REGEX ); |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructs an empty subscriptions object. |
| 29 |
*/ |
| 30 |
public Subscriptions() { |
| 31 |
super( new String[ 0 ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructs a subscriptions object based on an existing map. |
| 36 |
* |
| 37 |
* @param map map containing initial data for this object |
| 38 |
*/ |
| 39 |
public Subscriptions( Map map ) { |
| 40 |
this(); |
| 41 |
putAll( map ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Adds a subscription to a given MType. <code>mtype</code> may include |
| 46 |
* a wildcard according to the SAMP rules. |
| 47 |
* |
| 48 |
* @param mtype subscribed MType, possibly wildcarded |
| 49 |
*/ |
| 50 |
public void addMType( String mtype ) { |
| 51 |
put( mtype, new HashMap() ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Determines whether a given (non-wildcarded) MType is subscribed to |
| 56 |
* by this object. |
| 57 |
* |
| 58 |
* @param mtype MType to test |
| 59 |
*/ |
| 60 |
public boolean isSubscribed( String mtype ) { |
| 61 |
if ( containsKey( mtype ) ) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
for ( Iterator it = keySet().iterator(); it.hasNext(); ) { |
| 65 |
if ( matchLevel( (String) it.next(), mtype ) >= 0 ) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the map which forms the value for a given MType key. |
| 74 |
* If a wildcarded subscription is recorded which matches |
| 75 |
* <code>mtype</code>, the corresponding value is returned. |
| 76 |
* If <code>mtype</code> is not subscribed to, <code>null</code> |
| 77 |
* is returned. |
| 78 |
* |
| 79 |
* @param mtype MType to query |
| 80 |
* @return map value corresponding to <code>mtype</code>, or null |
| 81 |
*/ |
| 82 |
public Map getSubscription( String mtype ) { |
| 83 |
if ( containsKey( mtype ) ) { |
| 84 |
Object value = get( mtype ); |
| 85 |
return value instanceof Map ? (Map) value |
| 86 |
: (Map) new HashMap(); |
| 87 |
} |
| 88 |
else { |
| 89 |
int bestLevel = -1; |
| 90 |
Map bestValue = null; |
| 91 |
for ( Iterator it = entrySet().iterator(); it.hasNext(); ) { |
| 92 |
Map.Entry entry = (Map.Entry) it.next(); |
| 93 |
int level = matchLevel( (String) entry.getKey(), mtype ); |
| 94 |
if ( level > bestLevel ) { |
| 95 |
bestLevel = level; |
| 96 |
Object value = entry.getValue(); |
| 97 |
bestValue = value instanceof Map ? (Map) value |
| 98 |
: (Map) new HashMap(); |
| 99 |
} |
| 100 |
} |
| 101 |
return bestValue; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public void check() { |
| 106 |
super.check(); |
| 107 |
for ( Iterator it = entrySet().iterator(); it.hasNext(); ) { |
| 108 |
Map.Entry entry = (Map.Entry) it.next(); |
| 109 |
String key = (String) entry.getKey(); |
| 110 |
Object value = entry.getValue(); |
| 111 |
if ( ! MSUB_PATTERN.matcher( key ).matches() ) { |
| 112 |
throw new DataException( "Illegal subscription key " |
| 113 |
+ "\"" + key + "\"" ); |
| 114 |
} |
| 115 |
if ( ! ( value instanceof Map ) ) { |
| 116 |
throw new DataException( "Subscription values " |
| 117 |
+ "are not all maps" ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns a given map in the form of a Subscriptions object. |
| 124 |
* |
| 125 |
* @param map map |
| 126 |
* @return subscriptions |
| 127 |
*/ |
| 128 |
public static Subscriptions asSubscriptions( Map map ) { |
| 129 |
return ( map instanceof Subscriptions || map == null ) |
| 130 |
? (Subscriptions) map |
| 131 |
: new Subscriptions( map ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Performs wildcard matching of MTypes. The result is the number of |
| 136 |
* dot-separated "atoms" which match between the two. |
| 137 |
* |
| 138 |
* @param pattern MType pattern; may contain a wildcard |
| 139 |
* @param mtype unwildcarded MType for comparison with |
| 140 |
* <code>pattern</code> |
| 141 |
* @return the number of atoms of <code>pattern</code> which match |
| 142 |
* <code>mtype</code>; if <code>pattern</code>="*" the result is |
| 143 |
* 0, and if there is no match the result is -1 |
| 144 |
*/ |
| 145 |
public static int matchLevel( String pattern, String mtype ) { |
| 146 |
if ( mtype.equals( pattern ) ) { |
| 147 |
return countAtoms( pattern ); |
| 148 |
} |
| 149 |
else if ( "*".equals( pattern ) ) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
else if ( pattern.endsWith( ".*" ) ) { |
| 153 |
String prefix = pattern.substring( 0, pattern.length() - 2 ); |
| 154 |
return mtype.startsWith( prefix ) ? countAtoms( prefix ) |
| 155 |
: -1; |
| 156 |
} |
| 157 |
else { |
| 158 |
return -1; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Counts the number of dot-separated "atoms" in a string. |
| 164 |
* |
| 165 |
* @param text string to test |
| 166 |
*/ |
| 167 |
private static int countAtoms( String text ) { |
| 168 |
int leng = text.length(); |
| 169 |
int natom = 1; |
| 170 |
for ( int i = 0; i < leng; i++ ) { |
| 171 |
if ( text.charAt( i ) == '.' ) { |
| 172 |
natom++; |
| 173 |
} |
| 174 |
} |
| 175 |
return natom; |
| 176 |
} |
| 177 |
} |