Trie Matching Method - Version 3
Trie provider is a faster alternative to Pattern provider, however it requires more memory at runtime to store device data. See our metrics page for a summary and comparison of both Trie and Pattern methods.
The main difference between Trie and Pattern matching is the way the provider is constructed. Trie relies on explicit call to TrieFactory in order to create the provider object. Complete working example below:
import fiftyone.mobile.detection.Match ; import fiftyone.mobile.detection.TrieProvider ; import fiftyone.mobile.detection.factories.TrieFactory ; import java.io.IOException ; import java.util.logging.Level ; import java.util.logging.Logger ; public class TrieTest { //Trie Provider object. private static TrieProvider provider ; //Index of detected device. private static int index ; //Store detected property in a string. private static String result ; //Path to trie data. private static final String DATA_FILE = "<path to TRIE data file>" ; //Test user agent. private static final String UA = "<user agent to match>" ; public static void main ( String [] args ) { //Initialise provider through TrieFactory object. try { provider = TrieFactory . create ( DATA_FILE ); } catch ( IOException ex ) { Logger . getLogger ( TrieTest . class . getName ()). log ( Level . SEVERE , null , ex ); } //Get device index based on user agent. Basically perform match. try { index = provider . getDeviceIndex ( UA ); } catch ( Exception ex ) { Logger . getLogger ( TrieTest . class . getName ()). log ( Level . SEVERE , null , ex ); } //Check if property exists then get property value. if ( provider . PropertyNames (). contains ( "IsMobile" )) { result = provider . getPropertyValue ( index , "IsMobile" ); } //Some application logic. if ( Boolean . valueOf ( result )) { System . out . println ( "Mobile!" ); } else { System . out . println ( "Not Mobile!" ); } } }
Extended properties and Results objects are not available with the Trie provider.
The latest Lite Trie format data can be downloaded from GitHub . Premium and Enterprise Trie data files can be manually downloaded from the 51Degrees customer downloads area .
Trie Matching Method - Version 2
The Trie Provider is an alternative Provider that can be used instead of the default Pattern Provider. The Trie provider offers a considerably faster matching method by using a very large data file.
Reference the core package in your java project as normal. A different type of Provider and Reader are used to return results. Therefore the import changes to fiftyone.mobile.detection.trie.*. See the following example code.
import fiftyone.mobile.detection.trie.* ; public static main ( String [] args ) throws IOException { //Create a Provider object Provider p = Reader . create ( PATH_TO_TRIE_DATA_FILE ); //Get the value of a property, in this case "IsMobile" String result = p . getDeviceProperty (< INSERT_USER_AGENT_STRING_HERE >, "IsMobile" ); //Check the property value if ( result . equalsIgnoreCase ( "True" )){ system . out . println ( "This is mobile" ); } if ( result . equalsIgnoreCase ( "False" )){ system . out . println ( "This is not mobile" ); } }
Extended properties and Results objects are not available with the Trie provider.
Lite Trie format data can be downloaded from Sourceforge . Due to the size of the file it is not possible to distribute via other methods at this time. All Premium subscriptions include the option to receive a Trie format data file.