fiftyone.mobile.detection
Class TrieProvider

java.lang.Object
  extended by fiftyone.mobile.detection.TrieProvider
All Implemented Interfaces:
Disposable

public class TrieProvider
extends Object
implements Disposable

Decision trie data structure provider.


Nested Class Summary
static class TrieProvider.OffsetType
          The type of integers used to represent the offset to the children.
 
Field Summary
private  ByteBuffer _devices
          Byte array of the devices list.
private  short[] _lookupList
          Byte array of the look up list loaded into memory.
private  long _nodesOffset
          The position in the source data file of the nodes.
private  TriePool _pool
          A pool of readers that can be used in multi threaded operation.
private  ByteBuffer _properties
          Byte array of the available properties.
private  int _propertyCount
          The number of properties available in total.
private  Map<String,Integer> _propertyIndex
          Dictionary of property names to indexes.
private  List<String> _propertyNames
          List of the available property names.
private  ByteBuffer _Strings
          Byte array of the Strings available.
 String Copyright
          The copy right notice associated with the data file.
private static int SIZE_OF_BYTE
           
private static int SIZE_OF_INT
           
private static int SIZE_OF_LONG
           
private static int SIZE_OF_SHORT
           
private static int SIZE_OF_UBYTE
           
private static int SIZE_OF_UINT
           
private static int SIZE_OF_USHORT
           
 
Constructor Summary
TrieProvider(String copyright, byte[] strings, byte[] properties, byte[] devices, short[] lookupList, long nodesLength, long nodesOffset, String fileName)
          Constructs a new instance of a tree provider.
 
Method Summary
 void dispose()
          Disposes of the pool assigned to the provider.
private  short getChild(int lookupOffset, byte value)
          Returns the offset in the node for the current character.
 String getDeviceId(int deviceIndex)
          Returns the device id matching the device index.
 int getDeviceIndex(String userAgent)
          Returns the index of the device associated with the given user agent.
private  int getDeviceIndex(TrieReader reader, byte[] userAgent, int index, int parentDeviceIndex)
          Returns the offset in the device byte array to the device matching the useragent provided.
private  int getDeviceIndex(TrieReader reader, byte[] userAgent, int index, int parentDeviceIndex, StringBuilder matchedUserAgent)
          Returns the offset in the device byte array to the device matching the useragent provided.
 int getPropertyIndex(String property)
          Returns the integer index of the property in the list of values associated with the device.
 String getPropertyValue(int deviceIndex, int propertyIndex)
          Returns the value of the property index provided for the device index provided.
 String getPropertyValue(int deviceIndex, String property)
          Returns the property value based on the useragent provided.
private  String getStringValue(int offset)
          Returns the String at the offset provided.
 String getUserAgent(String userAgent)
          Returns the user agent matched against the one provided.
private static byte[] getUserAgentByteArray(String userAgent)
          Converts a user agent in to a null terminated byte array.
private  void initPropertyNames()
          Initialises the full list of property names available from the provider.
 List<String> PropertyNames()
          List of all property names for the provider.
private  void setNextNodePosition(TrieReader reader, short childIndex, short numberOfChildren, TrieProvider.OffsetType offsetType)
          Returns the position in the nodes stream of the next node.
static int sizeOfOffsets(TrieProvider.OffsetType offsetType)
          The number of bytes each offset takes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZE_OF_LONG

private static final int SIZE_OF_LONG
See Also:
Constant Field Values

SIZE_OF_UINT

private static final int SIZE_OF_UINT
See Also:
Constant Field Values

SIZE_OF_INT

private static final int SIZE_OF_INT
See Also:
Constant Field Values

SIZE_OF_USHORT

private static final int SIZE_OF_USHORT
See Also:
Constant Field Values

SIZE_OF_SHORT

private static final int SIZE_OF_SHORT
See Also:
Constant Field Values

SIZE_OF_UBYTE

private static final int SIZE_OF_UBYTE
See Also:
Constant Field Values

SIZE_OF_BYTE

private static final int SIZE_OF_BYTE
See Also:
Constant Field Values

Copyright

public String Copyright
The copy right notice associated with the data file.


_Strings

private ByteBuffer _Strings
Byte array of the Strings available.


_properties

private ByteBuffer _properties
Byte array of the available properties.


_devices

private ByteBuffer _devices
Byte array of the devices list.


_lookupList

private short[] _lookupList
Byte array of the look up list loaded into memory.


_pool

private TriePool _pool
A pool of readers that can be used in multi threaded operation.


_nodesOffset

private long _nodesOffset
The position in the source data file of the nodes.


_propertyIndex

private final Map<String,Integer> _propertyIndex
Dictionary of property names to indexes.


_propertyNames

private final List<String> _propertyNames
List of the available property names.


_propertyCount

private int _propertyCount
The number of properties available in total.

Constructor Detail

TrieProvider

public TrieProvider(String copyright,
                    byte[] strings,
                    byte[] properties,
                    byte[] devices,
                    short[] lookupList,
                    long nodesLength,
                    long nodesOffset,
                    String fileName)
             throws FileNotFoundException
Constructs a new instance of a tree provider.

Parameters:
copyright - The copyright notice for the data file.
Strings - Array containing all Strings in the output.
properties - Array of properties.
devices - Array of devices.
lookupList - Lookups data array.
nodesLength - The length of the node data.
nodesOffset - The position of the start of the nodes in the file provided.
fileName - Name of the source data file used to create the provider.
Throws:
FileNotFoundException
Method Detail

PropertyNames

public List<String> PropertyNames()
List of all property names for the provider.


getUserAgent

public String getUserAgent(String userAgent)
                    throws Exception
Returns the user agent matched against the one provided.

Parameters:
userAgent -
Throws:
Exception

getDeviceIndex

public int getDeviceIndex(String userAgent)
                   throws Exception
Returns the index of the device associated with the given user agent. The index returned may vary across different versions of the source data file and should not be stored. The "Id" property will remain unique.

Parameters:
userAgent -
Throws:
Exception

getDeviceId

public String getDeviceId(int deviceIndex)
Returns the device id matching the device index.

Parameters:
deviceIndex">Index - of the device whose Id should be returned

getPropertyValue

public String getPropertyValue(int deviceIndex,
                               String property)
Returns the property value based on the useragent provided.

Parameters:
deviceIndex - The index of the device whose property should be returned.
property">The - name of the property required.
Returns:
The value of the property for the given device index.

getPropertyValue

public String getPropertyValue(int deviceIndex,
                               int propertyIndex)
Returns the value of the property index provided for the device index provided.

Parameters:
deviceIndex - Index for the device.
propertyIndex - Index of the property required.

getPropertyIndex

public int getPropertyIndex(String property)
Returns the integer index of the property in the list of values associated with the device.

Parameters:
property -

dispose

public void dispose()
Disposes of the pool assigned to the provider.

Specified by:
dispose in interface Disposable

initPropertyNames

private void initPropertyNames()
Initialises the full list of property names available from the provider.


getStringValue

private String getStringValue(int offset)
Returns the String at the offset provided.

Parameters:
offset -

getUserAgentByteArray

private static byte[] getUserAgentByteArray(String userAgent)
Converts a user agent in to a null terminated byte array.

Parameters:
userAgent - The useragent to be tested.
Returns:
A null terminated byte array.

getChild

private short getChild(int lookupOffset,
                       byte value)
                throws ArrayIndexOutOfBoundsException
Returns the offset in the node for the current character.

Parameters:
lookupOffset - The offset in the byte array
value - The value to be checked.
Returns:
The position to move to.
Throws:
ArrayIndexOutOfBoundsException

sizeOfOffsets

public static int sizeOfOffsets(TrieProvider.OffsetType offsetType)
The number of bytes each offset takes.

Parameters:
offsetType -

setNextNodePosition

private void setNextNodePosition(TrieReader reader,
                                 short childIndex,
                                 short numberOfChildren,
                                 TrieProvider.OffsetType offsetType)
                          throws IOException
Returns the position in the nodes stream of the next node.

Parameters:
reader - Reader with exclusive access to the underlying file
childIndex -
numberOfChildren -
offsetType -
Throws:
IOException

getDeviceIndex

private int getDeviceIndex(TrieReader reader,
                           byte[] userAgent,
                           int index,
                           int parentDeviceIndex)
                    throws Exception
Returns the offset in the device byte array to the device matching the useragent provided.

Parameters:
reader - Reader with exclusive access to the underlying file.
userAgent - A null terminated byte array of the user agent to be tested.
index - The index in the array of the current character.
parentDeviceIndex - The device index of the parent node.
Returns:
The device id with the most number of matching characters.
Throws:
Exception

getDeviceIndex

private int getDeviceIndex(TrieReader reader,
                           byte[] userAgent,
                           int index,
                           int parentDeviceIndex,
                           StringBuilder matchedUserAgent)
                    throws Exception
Returns the offset in the device byte array to the device matching the useragent provided.

Parameters:
reader - Reader with exclusive access to the underlying file.
userAgent - A null terminated byte array of the user agent to be tested.
index - The index in the array of the current character.
parentDeviceIndex - The parent device index to be used if this node doesn't have a different one.
matchedUserAgent">The - characters of the user agent matched.
Returns:
The device id with the most number of matching characters.
Throws:
Exception