mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-09 15:17:24 -05:00
* Misc minor performance improvements * Use more concise loop syntax * Use more concise Collection sort syntax * Use StringUtils pad methods * Optimize some loops and double-map-lookups * Optimize array instantiation There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. * Cleanup string concatenation * Use EnumMap where possible * Use FastMath instead of Math * Initialize complex things only once * Reduce method size to aloow more JIT inlining * remove map of short->float * Move field to local variable * Cleanup Map lookup Co-authored-by: Rob Ekl <xwq876@zebra.com>
121 lines
3.9 KiB
Java
121 lines
3.9 KiB
Java
/*******************************************************************************
|
|
* 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 io.github.dsheirer.alias.id;
|
|
|
|
import org.apache.commons.lang3.Validate;
|
|
import org.apache.commons.math3.util.FastMath;
|
|
|
|
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)
|
|
{
|
|
Validate.isTrue(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)(FastMath.pow(2, mValue.length() - x - 1)); //Position weight
|
|
characterCount++;
|
|
}
|
|
}
|
|
|
|
weight += (int)(FastMath.pow(2, characterCount)) * 1000; //Character count weight
|
|
|
|
return weight;
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(WildcardID otherWildcardID)
|
|
{
|
|
return Integer.compare(this.weight(), otherWildcardID.weight());
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (!(o instanceof WildcardID)) return false;
|
|
return compareTo((WildcardID) o) == 0;
|
|
}
|
|
}
|