From 77cdcf186275704888e012a04904b943998b8a0d Mon Sep 17 00:00:00 2001 From: Denny Date: Thu, 4 Aug 2016 19:54:43 -0400 Subject: [PATCH] #72 Enhances management of wildcard values for aliases. Calculates a relative weighting for each wildcard alias id and then orders (smallest to largest) the wildcard values so that when attempting to lookup an id value, it first attempts to look for a direct match and then defaults to iterating the list of wildcard values until it finds the most specific or best matching wildcard value. --- .idea/dictionaries/denny.xml | 1 + src/alias/AliasList.java | 1025 +++++++++++++++++----------------- src/alias/id/WildcardID.java | 111 ++++ 3 files changed, 620 insertions(+), 517 deletions(-) create mode 100644 src/alias/id/WildcardID.java diff --git a/.idea/dictionaries/denny.xml b/.idea/dictionaries/denny.xml index a9760811..67e551ce 100644 --- a/.idea/dictionaries/denny.xml +++ b/.idea/dictionaries/denny.xml @@ -1,6 +1,7 @@ + esnid fleetsync ident talkgroup diff --git a/src/alias/AliasList.java b/src/alias/AliasList.java index 89640a65..0d5e64e1 100644 --- a/src/alias/AliasList.java +++ b/src/alias/AliasList.java @@ -1,23 +1,24 @@ /******************************************************************************* - * SDR Trunk - * Copyright (C) 2014-2016 Dennis Sheirer - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see + * SDR Trunk + * Copyright (C) 2014-2016 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see ******************************************************************************/ package alias; import alias.id.AliasID; +import alias.id.WildcardID; import alias.id.esn.Esn; import alias.id.fleetsync.FleetsyncID; import alias.id.lojack.LoJackFunctionAndID; @@ -36,189 +37,323 @@ import sample.Listener; import javax.xml.bind.annotation.XmlAttribute; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; public class AliasList implements Listener { - private final static Logger mLog = LoggerFactory.getLogger( AliasList.class ); + private final static Logger mLog = LoggerFactory.getLogger(AliasList.class); public static final String WILDCARD = "*"; + public static final String REGEX_WILDCARD = "."; - private Map mESN = new HashMap<>(); - private Map mFleetsync = new HashMap<>(); - private Map mLoJack = new HashMap<>(); - private Map mMDC1200 = new HashMap<>(); - private Map mMobileID = new HashMap<>(); - private Map mMPT1327 = new HashMap<>(); - private Map mSiteID = new HashMap<>(); - private Map mStatus = new HashMap<>(); - private Map mTalkgroup = new HashMap<>(); - private Map mUniqueID = new HashMap<>(); + private Map mESN = new HashMap<>(); + private Map mFleetsync = new HashMap<>(); + private Map mLoJack = new HashMap<>(); + private Map mMDC1200 = new HashMap<>(); + private Map mMobileID = new HashMap<>(); + private Map mMPT1327 = new HashMap<>(); + private Map mSiteID = new HashMap<>(); + private Map mStatus = new HashMap<>(); + private Map mTalkgroup = new HashMap<>(); + private Map mUniqueID = new HashMap<>(); - private List mESNWildcards = new ArrayList<>(); - private List mMobileIDWildcards = new ArrayList<>(); - private List mFleetsyncWildcards = new ArrayList<>(); - private List mMDC1200Wildcards = new ArrayList<>(); - private List mMPT1327Wildcards = new ArrayList<>(); - private List mSiteWildcards = new ArrayList<>(); - private List mTalkgroupWildcards = new ArrayList<>(); + private List mESNWildcards = new ArrayList<>(); + private List mMobileIDWildcards = new ArrayList<>(); + private List mFleetsyncWildcards = new ArrayList<>(); + private List mMDC1200Wildcards = new ArrayList<>(); + private List mMPT1327Wildcards = new ArrayList<>(); + private List mSiteWildcards = new ArrayList<>(); + private List mTalkgroupWildcards = new ArrayList<>(); - private String mName; - - /** - * List of aliases where all aliases share the same list name. Contains - * several methods for alias lookup from identifier values, like talkgroups. - * - * Responds to alias change events to keep the internal alias list updated. - */ - public AliasList( String name ) - { - mName = name; - } + private String mName; - /** - * Adds the alias to this list - */ - public void addAlias( Alias alias ) - { - if( alias != null ) - { - for( AliasID aliasID: alias.getId() ) - { - addAliasID( aliasID, alias ); - } - } - } + /** + * List of aliases where all aliases share the same list name. Contains + * several methods for alias lookup from identifier values, like talkgroups. + * + * Responds to alias change events to keep the internal alias list updated. + */ + public AliasList(String name) + { + mName = name; + } - /** - * Adds the alias and alias identifier to the internal type mapping. - */ - private void addAliasID( AliasID id, Alias alias ) - { - if(id.isValid()) + /** + * Adds the alias to this list + */ + public void addAlias(Alias alias) + { + if (alias != null) { - switch( id.getType() ) + for (AliasID aliasID : alias.getId()) + { + addAliasID(aliasID, alias); + } + } + } + + /** + * Adds the alias and alias identifier to the internal type mapping. + */ + private void addAliasID(AliasID id, Alias alias) + { + if (id.isValid()) + { + try + { + switch (id.getType()) + { + case ESN: + String esn = ((Esn)id).getEsn(); + + if(esn != null) + { + if (esn.contains(WILDCARD)) + { + mESNWildcards.add(new WildcardID(esn)); + Collections.sort(mESNWildcards); + } + + mESN.put(esn, alias); + } + break; + case Fleetsync: + String fleetsync = ((FleetsyncID)id).getIdent(); + + if(fleetsync != null) + { + if (fleetsync.contains(WILDCARD)) + { + mFleetsyncWildcards.add(new WildcardID(fleetsync)); + Collections.sort(mFleetsyncWildcards); + } + + mFleetsync.put(fleetsync, alias); + } + break; + case LoJack: + mLoJack.put((LoJackFunctionAndID) id, alias); + break; + case MDC1200: + String mdc = ((MDC1200ID)id).getIdent(); + + if(mdc != null) + { + if (mdc.contains(WILDCARD)) + { + mMDC1200Wildcards.add(new WildcardID(mdc)); + Collections.sort(mMDC1200Wildcards); + } + + mMDC1200.put(mdc, alias); + } + break; + case MPT1327: + String mpt = ((MPT1327ID)id).getIdent(); + + if(mpt != null) + { + if (mpt.contains(WILDCARD)) + { + mMPT1327Wildcards.add(new WildcardID(mpt)); + Collections.sort(mMPT1327Wildcards); + } + + mMPT1327.put(mpt, alias); + } + break; + case MIN: + String min = ((Min)id).getMin(); + + if(min != null) + { + if (min.contains(WILDCARD)) + { + mMobileIDWildcards.add(new WildcardID(min)); + Collections.sort(mMobileIDWildcards); + } + + mMobileID.put(min, alias); + } + break; + case LTRNetUID: + mUniqueID.put(((UniqueID)id).getUid(), alias); + break; + case Site: + String siteID = ((SiteID)id).getSite(); + + if(siteID != null) + { + if (siteID.contains(WILDCARD)) + { + mSiteWildcards.add(new WildcardID(siteID)); + Collections.sort(mSiteWildcards); + } + + mSiteID.put(siteID, alias); + } + break; + case Status: + mStatus.put(((StatusID) id).getStatus(), alias); + break; + case Talkgroup: + String tgid = ((TalkgroupID)id).getTalkgroup(); + + if(tgid != null) + { + if (tgid.contains(WILDCARD)) + { + mTalkgroupWildcards.add(new WildcardID(tgid)); + Collections.sort(mTalkgroupWildcards); + } + + mTalkgroup.put(tgid, alias); + } + break; + case NonRecordable: + case Priority: + //We don't maintain lookups for these items + break; + default: + mLog.warn("Unrecognized Alias ID Type:" + id.getType().name()); + break; + } + } + catch (Exception e) + { + mLog.error("Couldn't add alias ID " + id + " for alias " + alias); + } + } + } + + /** + * Removes the alias from this list + */ + public void removeAlias(Alias alias) + { + if (alias != null) + { + for (AliasID aliasID : alias.getId()) + { + removeAliasID(aliasID, alias); + } + } + } + + private void removeWildcard(String value, List wildcards) + { + if(value != null) + { + for(WildcardID wildcardID: wildcards) + { + if(wildcardID.value().equals(value)) + { + wildcards.remove(wildcardID); + } + } + } + } + + /** + * Removes the alias and alias identifier from internal mappings. + */ + private void removeAliasID(AliasID id, Alias alias) + { + if (id.isValid()) + { + switch (id.getType()) { case ESN: - Esn esn = (Esn)id; - if( esn.getEsn().contains(WILDCARD) ) - { - String regexESN = convertToRegex(esn.getEsn()); + String esn = ((Esn)id).getEsn(); - mESNWildcards.add(regexESN); - mESN.put( regexESN, alias ); - } - else + if(esn != null) { - mESN.put( esn.getEsn(), alias ); + if(esn.contains(WILDCARD)) + { + removeWildcard(esn, mESNWildcards); + } } + + mESN.remove(esn); break; case Fleetsync: - FleetsyncID fs = (FleetsyncID)id; + String fleetsync = ((FleetsyncID)id).getIdent(); - if( fs.getIdent().contains(WILDCARD) ) + if(fleetsync != null) { - String regexFleetsync = convertToRegex( fs.getIdent() ); + if(fleetsync.contains(WILDCARD)) + { + removeWildcard(fleetsync,mFleetsyncWildcards); + } - mFleetsyncWildcards.add(regexFleetsync); - mFleetsync.put( regexFleetsync, alias ); - } - else - { - mFleetsync.put( fs.getIdent(), alias ); + mFleetsync.remove(fleetsync); } break; case LoJack: - mLoJack.put( (LoJackFunctionAndID)id, alias ); + mLoJack.remove((LoJackFunctionAndID) id); break; case MDC1200: - MDC1200ID mdc = (MDC1200ID)id; + String mdc = ((MDC1200ID)id).getIdent(); - if( mdc.getIdent().contains(WILDCARD) ) + if(mdc != null) { - String regexMDC = convertToRegex(mdc.getIdent()); + if(mdc.contains(WILDCARD)) + { + removeWildcard(mdc, mMDC1200Wildcards); + } - mMDC1200Wildcards.add(regexMDC); - mMDC1200.put(regexMDC, alias); - } - else - { - mMDC1200.put( mdc.getIdent(), alias ); + mMDC1200.remove(mdc); } break; case MPT1327: - MPT1327ID mpt = (MPT1327ID)id; + String mpt = ((MPT1327ID)id).getIdent(); - String ident = mpt.getIdent(); - - if( ident != null ) + if(mpt != null) { - if( ident.contains(WILDCARD) ) + if(mpt.contains(WILDCARD)) { - String regexMPT1327 = convertToRegex(ident); + removeWildcard(mpt, mMPT1327Wildcards); + } - mMPT1327Wildcards.add(regexMPT1327); - mMPT1327.put(regexMPT1327, alias); - } - else - { - mMPT1327.put( ident, alias ); - } + mMPT1327.remove(mpt); } break; case MIN: - Min min = (Min)id; + String min = ((Min)id).getMin(); - if( min.getMin().contains(WILDCARD) ) + if(min != null) { - String regexMIN = convertToRegex(min.getMin()); + if(min.contains(WILDCARD)) + { + removeWildcard(min, mMobileIDWildcards); + } - mMobileIDWildcards.add(regexMIN); - mMobileID.put( regexMIN, alias ); - } - else - { - mMobileID.put( min.getMin(), alias ); + mMobileID.remove(min); } break; case LTRNetUID: - UniqueID uid = (UniqueID)id; - - mUniqueID.put( uid.getUid(), alias ); + mUniqueID.remove(((UniqueID) id).getUid()); break; case Site: - SiteID siteID = (SiteID)id; - - if( siteID.getSite().contains(WILDCARD) ) - { - String regexSiteID = convertToRegex(siteID.getSite()); - - mSiteWildcards.add(regexSiteID); - mSiteID.put( regexSiteID, alias ); - } - else - { - mSiteID.put( siteID.getSite(), alias ); - } - + mSiteID.remove(((SiteID) id).getSite()); break; case Status: - mStatus.put( ((StatusID)id).getStatus(), alias ); + mStatus.remove(((StatusID) id).getStatus()); break; case Talkgroup: - TalkgroupID tgid = (TalkgroupID)id; + String tgid = ((TalkgroupID)id).getTalkgroup(); - if( tgid.getTalkgroup().contains(WILDCARD) ) + if(tgid != null) { - String regexTalkgroup = convertToRegex(tgid.getTalkgroup()); + if(tgid.contains(WILDCARD)) + { + removeWildcard(tgid, mTalkgroupWildcards); + } - mTalkgroupWildcards.add(regexTalkgroup); - mTalkgroup.put( regexTalkgroup, alias ); - } - else - { - mTalkgroup.put( tgid.getTalkgroup(), alias ); + mTalkgroup.remove(tgid); } break; case NonRecordable: @@ -226,155 +361,11 @@ public class AliasList implements Listener //We don't maintain lookups for these items break; default: - mLog.warn( "Unrecognized Alias ID Type:" + id.getType().name() ); + mLog.warn("Unrecognized Alias ID Type:" + id.getType().name()); break; } } - } - - /** - * Removes the alias from this list - */ - public void removeAlias( Alias alias ) - { - if( alias != null ) - { - for( AliasID aliasID: alias.getId() ) - { - removeAliasID( aliasID, alias ); - } - } - } - - /** - * Removes the alias and alias identifier from internal mappings. - */ - private void removeAliasID( AliasID id, Alias alias ) - { - if(id.isValid()) - { - switch( id.getType() ) - { - case ESN: - Esn esn = (Esn)id; - - if( esn.getEsn().contains(WILDCARD)) - { - String regexESN = convertToRegex(esn.getEsn()); - mESN.remove(regexESN); - mESNWildcards.remove(regexESN); - } - else if( mESN.containsKey( esn.getEsn() ) ) - { - mESN.remove(esn.getEsn()); - } - break; - case Fleetsync: - FleetsyncID fs = (FleetsyncID)id; - - if(fs.getIdent().contains(WILDCARD)) - { - String regexFleetsync = convertToRegex(fs.getIdent()); - mFleetsync.remove(regexFleetsync); - mFleetsyncWildcards.remove(regexFleetsync); - } - else - { - mFleetsync.remove( fs.getIdent() ); - } - break; - case LoJack: - mLoJack.remove((LoJackFunctionAndID)id); - break; - case MDC1200: - MDC1200ID mdc = (MDC1200ID)id; - - if(mdc.getIdent().contains(WILDCARD)) - { - String regexMDC1200 = convertToRegex(mdc.getIdent()); - mMDC1200.remove(regexMDC1200); - mMDC1200Wildcards.remove(regexMDC1200); - } - else - { - mMDC1200.remove( mdc.getIdent() ); - } - break; - case MPT1327: - MPT1327ID mpt = (MPT1327ID)id; - - if(mpt.getIdent().contains(WILDCARD)) - { - String regexMPT1327 = convertToRegex(mpt.getIdent()); - mMPT1327.remove(regexMPT1327); - mMPT1327Wildcards.remove(regexMPT1327); - } - else - { - mMPT1327.remove( mpt.getIdent() ); - } - break; - case MIN: - Min min = (Min)id; - - if(min.getMin().contains(WILDCARD)) - { - String regexMIN = convertToRegex(min.getMin()); - mMobileID.remove(regexMIN); - mMobileIDWildcards.remove(regexMIN); - } - else - { - mMobileID.remove( min.getMin() ); - } - break; - case LTRNetUID: - mUniqueID.remove( ((UniqueID)id).getUid() ); - break; - case Site: - mSiteID.remove( ((SiteID)id).getSite() ); - break; - case Status: - mStatus.remove( ((StatusID)id).getStatus() ); - break; - case Talkgroup: - TalkgroupID tg = (TalkgroupID)id; - - if(tg.getTalkgroup().contains(WILDCARD)) - { - String regexTalkgroup = convertToRegex(tg.getTalkgroup()); - mTalkgroup.remove(regexTalkgroup); - mTalkgroupWildcards.remove(regexTalkgroup); - } - else - { - mTalkgroup.remove( tg.getTalkgroup() ); - } - break; - case NonRecordable: - case Priority: - //We don't maintain lookups for these items - break; - default: - mLog.warn( "Unrecognized Alias ID Type:" + id.getType().name() ); - break; - } - } - } - - /** - * Converts user wildcard character (*) to regex single character wildcard (.) - * but ignores a regex multi-character wildcard (.*) - */ - private String convertToRegex(String value ) - { - if( value.contains(WILDCARD) && !value.contains( ".*" ) ) - { - return value.replace(WILDCARD, "." ); - } - - return value; - } + } /** * Returns the first matching regex wildcard from the list of wildcards that matches the @@ -384,15 +375,15 @@ public class AliasList implements Listener * @param wildcards to match against * @return matching wildcard ID or null */ - private String getWildcardMatch( String id, List wildcards ) + private String getWildcardMatch(String id, List wildcards) { - if(id != null) + if (id != null) { - for(String wildcard: wildcards) + for (WildcardID wildcard : wildcards) { - if(id.matches(wildcard)) + if (wildcard.matches(id)) { - return wildcard; + return wildcard.value(); } } } @@ -400,63 +391,63 @@ public class AliasList implements Listener return null; } - /** - * Lookup alias by site ID - */ - public Alias getSiteID( String siteID ) - { + /** + * Lookup alias by site ID + */ + public Alias getSiteID(String siteID) + { Alias alias = null; - if(siteID != null) + if (siteID != null) { alias = mSiteID.get(siteID); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(siteID, mSiteWildcards); - if(wildcard != null) + if (wildcard != null) { alias = mSiteID.get(wildcard); } } } - return alias; - } + return alias; + } - /** - * Lookup alias by status ID - */ - public Alias getStatus( int status ) - { - return mStatus.get( status ); - } - - /** - * Lookup alias by Unique ID (UID) - */ - public Alias getUniqueID( int uniqueID ) - { - return mUniqueID.get( uniqueID ); - } + /** + * Lookup alias by status ID + */ + public Alias getStatus(int status) + { + return mStatus.get(status); + } - /** - * Lookup alias by ESN - */ - public Alias getESNAlias( String esn ) - { + /** + * Lookup alias by Unique ID (UID) + */ + public Alias getUniqueID(int uniqueID) + { + return mUniqueID.get(uniqueID); + } + + /** + * Lookup alias by ESN + */ + public Alias getESNAlias(String esn) + { Alias alias = null; - if(esn != null) + if (esn != null) { alias = mESN.get(esn); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(esn, mESNWildcards); - if(wildcard != null) + if (wildcard != null) { alias = mESN.get(wildcard); } @@ -464,254 +455,254 @@ public class AliasList implements Listener } return alias; - } - - /** - * Lookup alias by fleetsync ID - */ - public Alias getFleetsyncAlias( String ident ) - { + } + + /** + * Lookup alias by fleetsync ID + */ + public Alias getFleetsyncAlias(String ident) + { Alias alias = null; - if( ident != null ) - { + if (ident != null) + { alias = mFleetsync.get(ident); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(ident, mFleetsyncWildcards); - if(wildcard != null) + if (wildcard != null) { alias = mFleetsync.get(wildcard); } } - } - - return alias; - } - - /** - * Lookup alias by lojack function and ID - */ - public Alias getLoJackAlias( Function function, String id ) - { - if( id != null ) - { - for( LoJackFunctionAndID lojack: mLoJack.keySet() ) - { - if( lojack.matches( function, id ) ) - { - return mLoJack.get( lojack ); - } - } - } - - return null; - } - - /** - * Lookup alias by MDC1200 ID - */ - public Alias getMDC1200Alias( String ident ) - { + } + + return alias; + } + + /** + * Lookup alias by lojack function and ID + */ + public Alias getLoJackAlias(Function function, String id) + { + if (id != null) + { + for (LoJackFunctionAndID lojack : mLoJack.keySet()) + { + if (lojack.matches(function, id)) + { + return mLoJack.get(lojack); + } + } + } + + return null; + } + + /** + * Lookup alias by MDC1200 ID + */ + public Alias getMDC1200Alias(String ident) + { Alias alias = null; - if( ident != null ) - { + if (ident != null) + { alias = mMDC1200.get(ident); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(ident, mMDC1200Wildcards); - if(wildcard != null) + if (wildcard != null) { alias = mMDC1200.get(wildcard); } } - } - - return alias; - } - - /** - * Lookup alias by MPT1327 Ident - */ - public Alias getMPT1327Alias( String ident ) - { + } + + return alias; + } + + /** + * Lookup alias by MPT1327 Ident + */ + public Alias getMPT1327Alias(String ident) + { Alias alias = null; - if( ident != null ) - { + if (ident != null) + { alias = mMPT1327.get(ident); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(ident, mMPT1327Wildcards); - if(wildcard != null) + if (wildcard != null) { alias = mMPT1327.get(wildcard); } } - } - - return alias; - } - - /** - * Lookup alias by Mobile ID Number (MIN) - */ - public Alias getMobileIDNumberAlias( String ident ) - { + } + + return alias; + } + + /** + * Lookup alias by Mobile ID Number (MIN) + */ + public Alias getMobileIDNumberAlias(String ident) + { Alias alias = null; - if( ident != null ) - { + if (ident != null) + { alias = mMobileID.get(ident); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(ident, mMobileIDWildcards); - if(wildcard != null) + if (wildcard != null) { alias = mMobileID.get(wildcard); } } - } - - return alias; - } - - /** - * Lookup alias by talkgroup - */ - public Alias getTalkgroupAlias( String tgid ) - { + } + + return alias; + } + + /** + * Lookup alias by talkgroup + */ + public Alias getTalkgroupAlias(String tgid) + { Alias alias = null; - if( tgid != null ) - { + if (tgid != null) + { alias = mTalkgroup.get(tgid); - if(alias == null) + if (alias == null) { String wildcard = getWildcardMatch(tgid, mTalkgroupWildcards); - if(wildcard != null) + if (wildcard != null) { alias = mTalkgroup.get(wildcard); } } - } - - return alias; - } + } - /** - * Alias list name - */ - public String toString() - { - return mName; - } + return alias; + } - /** - * Alias list name - */ - @XmlAttribute - public String getName() - { - return mName; - } + /** + * Alias list name + */ + public String toString() + { + return mName; + } - /** - * Lookup an alias by metadata type and string value - */ - public Alias getAlias( String value, MetadataType type ) - { - switch( type ) - { - case ESN: - return getESNAlias( value ); - case FLEETSYNC: - return getFleetsyncAlias( value ); - case MDC1200: - return getMDC1200Alias( value ); - case MOBILE_ID: - return getMobileIDNumberAlias( value ); - case MPT1327: - return getMPT1327Alias( value ); - case SITE_ID: - return getSiteID( value ); - case STATUS: - if( value != null ) - { - try - { - return getStatus( Integer.valueOf( value ) ); - } - catch( Exception e ) - { - //do nothing, we couldn't parse the int value - } - } - break; - case FROM: - case TO: - return getTalkgroupAlias( value ); - case UNIQUE_ID: - if( value != null ) - { - try - { - return getUniqueID( Integer.valueOf( value ) ); - } - catch( Exception e ) - { - //do nothing, we couldn't parse the int value - } - } - break; - default: - break; - } - - return null; - } + /** + * Alias list name + */ + @XmlAttribute + public String getName() + { + return mName; + } - /** - * Receive alias change event notifications and modify this list accordingly - */ - @Override - public void receive( AliasEvent event ) - { - Alias alias = event.getAlias(); - - switch( event.getEvent() ) - { - case ADD: - if( alias.getList() != null && getName().equalsIgnoreCase( alias.getList() ) ) - { - addAlias( alias ); - } - break; - case CHANGE: - if( alias.getList() != null && getName().equalsIgnoreCase( alias.getList() ) ) - { - addAlias( alias ); - } - break; - case DELETE: - if( alias.getList() != null && getName().equalsIgnoreCase( alias.getList() ) ) - { - removeAlias( alias ); - } - break; - default: - break; - } - } + /** + * Lookup an alias by metadata type and string value + */ + public Alias getAlias(String value, MetadataType type) + { + switch (type) + { + case ESN: + return getESNAlias(value); + case FLEETSYNC: + return getFleetsyncAlias(value); + case MDC1200: + return getMDC1200Alias(value); + case MOBILE_ID: + return getMobileIDNumberAlias(value); + case MPT1327: + return getMPT1327Alias(value); + case SITE_ID: + return getSiteID(value); + case STATUS: + if (value != null) + { + try + { + return getStatus(Integer.valueOf(value)); + } + catch (Exception e) + { + //do nothing, we couldn't parse the int value + } + } + break; + case FROM: + case TO: + return getTalkgroupAlias(value); + case UNIQUE_ID: + if (value != null) + { + try + { + return getUniqueID(Integer.valueOf(value)); + } + catch (Exception e) + { + //do nothing, we couldn't parse the int value + } + } + break; + default: + break; + } + + return null; + } + + /** + * Receive alias change event notifications and modify this list accordingly + */ + @Override + public void receive(AliasEvent event) + { + Alias alias = event.getAlias(); + + switch (event.getEvent()) + { + case ADD: + if (alias.getList() != null && getName().equalsIgnoreCase(alias.getList())) + { + addAlias(alias); + } + break; + case CHANGE: + if (alias.getList() != null && getName().equalsIgnoreCase(alias.getList())) + { + addAlias(alias); + } + break; + case DELETE: + if (alias.getList() != null && getName().equalsIgnoreCase(alias.getList())) + { + removeAlias(alias); + } + break; + default: + break; + } + } } diff --git a/src/alias/id/WildcardID.java b/src/alias/id/WildcardID.java new file mode 100644 index 00000000..d4d00c3f --- /dev/null +++ b/src/alias/id/WildcardID.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * sdrtrunk + * Copyright (C) 2014-2016 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * + ******************************************************************************/ +package alias.id; + +import java.util.regex.Pattern; + +public class WildcardID implements Comparable +{ + public static final String WILDCARD = "*"; + public static final String REGEX_WILDCARD = "."; + + private String mValue; + private Pattern mPattern; + private int mWeight; + + /** + * Wildcard identifier for matching to string identifiers containing single-character (*) wildcard values. + * + * Supports ordering from most-specific to least-specific using the build-in weighting calculation where identifier + * patterns containing fewer wildcards closer to the least significant digit will be weighted more heavily than + * identifier patterns containing more wildcards, or wildcard characters in the most significant digits. + * + * @param value is a string value containing one or more asterisk (*) single character wildcard with a maximum + * length of 10 characters. + * + * @throws AssertionError if the value is null, longer than 10 characters, or doesn't contain at least 1 asterisk + * @throws IllegalArgumentException if the value cannot be compiled as a regular expression after converting the + * asterisk characters to the regex single-character wildcard value (.) + */ + public WildcardID(String value) + { + assert(value != null && value.contains(WILDCARD) && value.length() < 10); + + mValue = value; + + try + { + mPattern = Pattern.compile(value.replace(WILDCARD, REGEX_WILDCARD)); + } + catch(Exception e) + { + throw new IllegalArgumentException("Invalid regex pattern for alias ID value [" + value + "]", e); + } + + mWeight = calculateWeight(); + } + + public String value() + { + return mValue; + } + + /** + * Indicates if the id matches the internal regex pattern for this wildcard alias ID + * @param id + * @return + */ + public boolean matches(String id) + { + return id != null && mPattern.matcher(id).matches(); + } + + public int weight() + { + return mWeight; + } + + /** + * Calculates a weighting value for wildcard character quantity and significant digit location + */ + private int calculateWeight() + { + int weight = 0; + int characterCount = -1; + + for( int x = 0; x < mValue.length(); x++) + { + if(mValue.substring(x, x + 1).equals(WILDCARD)) + { + weight += (int)(Math.pow(2, mValue.length() - x - 1)); //Position weight + characterCount++; + } + } + + weight += (int)(Math.pow(2, characterCount)) * 1000; //Character count weight + + return weight; + } + + @Override + public int compareTo(WildcardID otherWildcardID) + { + return Integer.compare(this.weight(), otherWildcardID.weight()); + } +}