mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-20 04:27:48 -05:00
#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.
This commit is contained in:
1
.idea/dictionaries/denny.xml
generated
1
.idea/dictionaries/denny.xml
generated
@@ -1,6 +1,7 @@
|
|||||||
<component name="ProjectDictionaryState">
|
<component name="ProjectDictionaryState">
|
||||||
<dictionary name="denny">
|
<dictionary name="denny">
|
||||||
<words>
|
<words>
|
||||||
|
<w>esnid</w>
|
||||||
<w>fleetsync</w>
|
<w>fleetsync</w>
|
||||||
<w>ident</w>
|
<w>ident</w>
|
||||||
<w>talkgroup</w>
|
<w>talkgroup</w>
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
package alias;
|
package alias;
|
||||||
|
|
||||||
import alias.id.AliasID;
|
import alias.id.AliasID;
|
||||||
|
import alias.id.WildcardID;
|
||||||
import alias.id.esn.Esn;
|
import alias.id.esn.Esn;
|
||||||
import alias.id.fleetsync.FleetsyncID;
|
import alias.id.fleetsync.FleetsyncID;
|
||||||
import alias.id.lojack.LoJackFunctionAndID;
|
import alias.id.lojack.LoJackFunctionAndID;
|
||||||
@@ -36,14 +37,17 @@ import sample.Listener;
|
|||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class AliasList implements Listener<AliasEvent>
|
public class AliasList implements Listener<AliasEvent>
|
||||||
{
|
{
|
||||||
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 WILDCARD = "*";
|
||||||
|
public static final String REGEX_WILDCARD = ".";
|
||||||
|
|
||||||
private Map<String, Alias> mESN = new HashMap<>();
|
private Map<String, Alias> mESN = new HashMap<>();
|
||||||
private Map<String, Alias> mFleetsync = new HashMap<>();
|
private Map<String, Alias> mFleetsync = new HashMap<>();
|
||||||
@@ -56,13 +60,13 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
private Map<String, Alias> mTalkgroup = new HashMap<>();
|
private Map<String, Alias> mTalkgroup = new HashMap<>();
|
||||||
private Map<Integer, Alias> mUniqueID = new HashMap<>();
|
private Map<Integer, Alias> mUniqueID = new HashMap<>();
|
||||||
|
|
||||||
private List<String> mESNWildcards = new ArrayList<>();
|
private List<WildcardID> mESNWildcards = new ArrayList<>();
|
||||||
private List<String> mMobileIDWildcards = new ArrayList<>();
|
private List<WildcardID> mMobileIDWildcards = new ArrayList<>();
|
||||||
private List<String> mFleetsyncWildcards = new ArrayList<>();
|
private List<WildcardID> mFleetsyncWildcards = new ArrayList<>();
|
||||||
private List<String> mMDC1200Wildcards = new ArrayList<>();
|
private List<WildcardID> mMDC1200Wildcards = new ArrayList<>();
|
||||||
private List<String> mMPT1327Wildcards = new ArrayList<>();
|
private List<WildcardID> mMPT1327Wildcards = new ArrayList<>();
|
||||||
private List<String> mSiteWildcards = new ArrayList<>();
|
private List<WildcardID> mSiteWildcards = new ArrayList<>();
|
||||||
private List<String> mTalkgroupWildcards = new ArrayList<>();
|
private List<WildcardID> mTalkgroupWildcards = new ArrayList<>();
|
||||||
|
|
||||||
private String mName;
|
private String mName;
|
||||||
|
|
||||||
@@ -97,128 +101,116 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
private void addAliasID(AliasID id, Alias alias)
|
private void addAliasID(AliasID id, Alias alias)
|
||||||
{
|
{
|
||||||
if (id.isValid())
|
if (id.isValid())
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
switch (id.getType())
|
switch (id.getType())
|
||||||
{
|
{
|
||||||
case ESN:
|
case ESN:
|
||||||
Esn esn = (Esn)id;
|
String esn = ((Esn)id).getEsn();
|
||||||
if( esn.getEsn().contains(WILDCARD) )
|
|
||||||
{
|
|
||||||
String regexESN = convertToRegex(esn.getEsn());
|
|
||||||
|
|
||||||
mESNWildcards.add(regexESN);
|
if(esn != null)
|
||||||
mESN.put( regexESN, alias );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
mESN.put( esn.getEsn(), alias );
|
if (esn.contains(WILDCARD))
|
||||||
|
{
|
||||||
|
mESNWildcards.add(new WildcardID(esn));
|
||||||
|
Collections.sort(mESNWildcards);
|
||||||
|
}
|
||||||
|
|
||||||
|
mESN.put(esn, alias);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Fleetsync:
|
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))
|
||||||
|
{
|
||||||
mFleetsyncWildcards.add(regexFleetsync);
|
mFleetsyncWildcards.add(new WildcardID(fleetsync));
|
||||||
mFleetsync.put( regexFleetsync, alias );
|
Collections.sort(mFleetsyncWildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mFleetsync.put(fleetsync, alias);
|
||||||
mFleetsync.put( fs.getIdent(), alias );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LoJack:
|
case LoJack:
|
||||||
mLoJack.put((LoJackFunctionAndID) id, alias);
|
mLoJack.put((LoJackFunctionAndID) id, alias);
|
||||||
break;
|
break;
|
||||||
case MDC1200:
|
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))
|
||||||
|
{
|
||||||
mMDC1200Wildcards.add(regexMDC);
|
mMDC1200Wildcards.add(new WildcardID(mdc));
|
||||||
mMDC1200.put(regexMDC, alias);
|
Collections.sort(mMDC1200Wildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mMDC1200.put(mdc, alias);
|
||||||
mMDC1200.put( mdc.getIdent(), alias );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MPT1327:
|
case MPT1327:
|
||||||
MPT1327ID mpt = (MPT1327ID)id;
|
String mpt = ((MPT1327ID)id).getIdent();
|
||||||
|
|
||||||
String ident = mpt.getIdent();
|
if(mpt != null)
|
||||||
|
|
||||||
if( ident != null )
|
|
||||||
{
|
{
|
||||||
if( ident.contains(WILDCARD) )
|
if (mpt.contains(WILDCARD))
|
||||||
{
|
{
|
||||||
String regexMPT1327 = convertToRegex(ident);
|
mMPT1327Wildcards.add(new WildcardID(mpt));
|
||||||
|
Collections.sort(mMPT1327Wildcards);
|
||||||
mMPT1327Wildcards.add(regexMPT1327);
|
|
||||||
mMPT1327.put(regexMPT1327, alias);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mMPT1327.put( ident, alias );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mMPT1327.put(mpt, alias);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MIN:
|
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))
|
||||||
|
{
|
||||||
mMobileIDWildcards.add(regexMIN);
|
mMobileIDWildcards.add(new WildcardID(min));
|
||||||
mMobileID.put( regexMIN, alias );
|
Collections.sort(mMobileIDWildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mMobileID.put(min, alias);
|
||||||
mMobileID.put( min.getMin(), alias );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LTRNetUID:
|
case LTRNetUID:
|
||||||
UniqueID uid = (UniqueID)id;
|
mUniqueID.put(((UniqueID)id).getUid(), alias);
|
||||||
|
|
||||||
mUniqueID.put( uid.getUid(), alias );
|
|
||||||
break;
|
break;
|
||||||
case Site:
|
case Site:
|
||||||
SiteID siteID = (SiteID)id;
|
String siteID = ((SiteID)id).getSite();
|
||||||
|
|
||||||
if( siteID.getSite().contains(WILDCARD) )
|
if(siteID != null)
|
||||||
{
|
{
|
||||||
String regexSiteID = convertToRegex(siteID.getSite());
|
if (siteID.contains(WILDCARD))
|
||||||
|
|
||||||
mSiteWildcards.add(regexSiteID);
|
|
||||||
mSiteID.put( regexSiteID, alias );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
mSiteID.put( siteID.getSite(), alias );
|
mSiteWildcards.add(new WildcardID(siteID));
|
||||||
|
Collections.sort(mSiteWildcards);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mSiteID.put(siteID, alias);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case Status:
|
case Status:
|
||||||
mStatus.put(((StatusID) id).getStatus(), alias);
|
mStatus.put(((StatusID) id).getStatus(), alias);
|
||||||
break;
|
break;
|
||||||
case Talkgroup:
|
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))
|
||||||
|
{
|
||||||
mTalkgroupWildcards.add(regexTalkgroup);
|
mTalkgroupWildcards.add(new WildcardID(tgid));
|
||||||
mTalkgroup.put( regexTalkgroup, alias );
|
Collections.sort(mTalkgroupWildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mTalkgroup.put(tgid, alias);
|
||||||
mTalkgroup.put( tgid.getTalkgroup(), alias );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NonRecordable:
|
case NonRecordable:
|
||||||
@@ -230,6 +222,11 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
mLog.error("Couldn't add alias ID " + id + " for alias " + alias);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,6 +243,20 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void removeWildcard(String value, List<WildcardID> 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.
|
* Removes the alias and alias identifier from internal mappings.
|
||||||
*/
|
*/
|
||||||
@@ -256,76 +267,71 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
switch (id.getType())
|
switch (id.getType())
|
||||||
{
|
{
|
||||||
case ESN:
|
case ESN:
|
||||||
Esn esn = (Esn)id;
|
String esn = ((Esn)id).getEsn();
|
||||||
|
|
||||||
if( esn.getEsn().contains(WILDCARD))
|
if(esn != null)
|
||||||
{
|
{
|
||||||
String regexESN = convertToRegex(esn.getEsn());
|
if(esn.contains(WILDCARD))
|
||||||
mESN.remove(regexESN);
|
|
||||||
mESNWildcards.remove(regexESN);
|
|
||||||
}
|
|
||||||
else if( mESN.containsKey( esn.getEsn() ) )
|
|
||||||
{
|
{
|
||||||
mESN.remove(esn.getEsn());
|
removeWildcard(esn, mESNWildcards);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mESN.remove(esn);
|
||||||
break;
|
break;
|
||||||
case Fleetsync:
|
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))
|
||||||
mFleetsync.remove(regexFleetsync);
|
{
|
||||||
mFleetsyncWildcards.remove(regexFleetsync);
|
removeWildcard(fleetsync,mFleetsyncWildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mFleetsync.remove(fleetsync);
|
||||||
mFleetsync.remove( fs.getIdent() );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LoJack:
|
case LoJack:
|
||||||
mLoJack.remove((LoJackFunctionAndID) id);
|
mLoJack.remove((LoJackFunctionAndID) id);
|
||||||
break;
|
break;
|
||||||
case MDC1200:
|
case MDC1200:
|
||||||
MDC1200ID mdc = (MDC1200ID)id;
|
String mdc = ((MDC1200ID)id).getIdent();
|
||||||
|
|
||||||
if(mdc.getIdent().contains(WILDCARD))
|
if(mdc != null)
|
||||||
{
|
{
|
||||||
String regexMDC1200 = convertToRegex(mdc.getIdent());
|
if(mdc.contains(WILDCARD))
|
||||||
mMDC1200.remove(regexMDC1200);
|
{
|
||||||
mMDC1200Wildcards.remove(regexMDC1200);
|
removeWildcard(mdc, mMDC1200Wildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mMDC1200.remove(mdc);
|
||||||
mMDC1200.remove( mdc.getIdent() );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MPT1327:
|
case MPT1327:
|
||||||
MPT1327ID mpt = (MPT1327ID)id;
|
String mpt = ((MPT1327ID)id).getIdent();
|
||||||
|
|
||||||
if(mpt.getIdent().contains(WILDCARD))
|
if(mpt != null)
|
||||||
{
|
{
|
||||||
String regexMPT1327 = convertToRegex(mpt.getIdent());
|
if(mpt.contains(WILDCARD))
|
||||||
mMPT1327.remove(regexMPT1327);
|
{
|
||||||
mMPT1327Wildcards.remove(regexMPT1327);
|
removeWildcard(mpt, mMPT1327Wildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mMPT1327.remove(mpt);
|
||||||
mMPT1327.remove( mpt.getIdent() );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MIN:
|
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))
|
||||||
mMobileID.remove(regexMIN);
|
{
|
||||||
mMobileIDWildcards.remove(regexMIN);
|
removeWildcard(min, mMobileIDWildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mMobileID.remove(min);
|
||||||
mMobileID.remove( min.getMin() );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LTRNetUID:
|
case LTRNetUID:
|
||||||
@@ -338,17 +344,16 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
mStatus.remove(((StatusID) id).getStatus());
|
mStatus.remove(((StatusID) id).getStatus());
|
||||||
break;
|
break;
|
||||||
case Talkgroup:
|
case Talkgroup:
|
||||||
TalkgroupID tg = (TalkgroupID)id;
|
String tgid = ((TalkgroupID)id).getTalkgroup();
|
||||||
|
|
||||||
if(tg.getTalkgroup().contains(WILDCARD))
|
if(tgid != null)
|
||||||
{
|
{
|
||||||
String regexTalkgroup = convertToRegex(tg.getTalkgroup());
|
if(tgid.contains(WILDCARD))
|
||||||
mTalkgroup.remove(regexTalkgroup);
|
{
|
||||||
mTalkgroupWildcards.remove(regexTalkgroup);
|
removeWildcard(tgid, mTalkgroupWildcards);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
mTalkgroup.remove(tgid);
|
||||||
mTalkgroup.remove( tg.getTalkgroup() );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NonRecordable:
|
case NonRecordable:
|
||||||
@@ -362,20 +367,6 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* Returns the first matching regex wildcard from the list of wildcards that matches the
|
||||||
* identifier.
|
* identifier.
|
||||||
@@ -384,15 +375,15 @@ public class AliasList implements Listener<AliasEvent>
|
|||||||
* @param wildcards to match against
|
* @param wildcards to match against
|
||||||
* @return matching wildcard ID or null
|
* @return matching wildcard ID or null
|
||||||
*/
|
*/
|
||||||
private String getWildcardMatch( String id, List<String> wildcards )
|
private String getWildcardMatch(String id, List<WildcardID> 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
111
src/alias/id/WildcardID.java
Normal file
111
src/alias/id/WildcardID.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||||
|
*
|
||||||
|
******************************************************************************/
|
||||||
|
package alias.id;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class WildcardID implements Comparable<WildcardID>
|
||||||
|
{
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user