Class QueryData


  • public class QueryData
    extends java.lang.Object
    Represents the query string of a Url as a sequence of name/value pairs.

    The syntax of a URL is:

    
     scheme://domain:port/path?query_string#fragment_id
     
    Often, the query string consists of a list of name/value pairs, separated by an ampersand.

    e.g. key1=value1&key2=value2&key3=value2

    It is convenient to use the QueryData class to define your query string as a series of name/value pairs.

    e.g.

    
     // Construct the url "http://localhost/?key1=value1&key2=value2&key3=value3
     Url url = new Url("http://localhost/");
     QueryData queryData = new QueryData();
     queryData.add("key1", "value1");
     queryData.add("key2", "value2");
     queryData.add("key3", "value3");
     url = url.withQuery(queryData);
     
    • Constructor Summary

      Constructors 
      Constructor Description
      QueryData()
      Creates a QueryData object with no name/value pairs.
      QueryData​(long cPtr, boolean cMemoryOwn)
      For internal use only.
      QueryData​(java.lang.String urlEncoded)
      Initialises a new instance of the QueryData class by parsing a URL encoded string.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.lang.String name, QueryData queryData)
      Adds a name/value pair to the end of the list, using a value extracted from another QueryData object.
      void add​(java.lang.String name, java.lang.String value)
      Adds a name/value pair to the end of the list.
      void clear()
      Removes all of the name/value pairs from the list.
      void delete()  
      NameValuePair get​(int index)
      Gets the NameValuePair at the specified index in the list.
      int getCount()
      Gets the number of name/value pairs in the list.
      static long getCPtr​(QueryData obj)
      For internal use only.
      int getNameCount​(java.lang.String name)
      Gets the number of name/value pairs in the list that have the specified name.
      java.lang.String getValue​(java.lang.String name)
      Gets the value of the first name/value pair in the list that has the specified name.
      java.lang.String getValue​(java.lang.String name, int index)
      Gets the value of the nth name/value pair in the list that has the specified name.
      void insert​(java.lang.String name, QueryData queryData, int index)
      Inserts a name/value pair into the list at the specified index, using a value extracted from another QueryData object.
      void insert​(java.lang.String name, java.lang.String value, int index)
      Inserts a name/value pair into the list at the specified index.
      void remove​(java.lang.String name)
      Removes the first item in the list with the specified name.
      void remove​(java.lang.String name, int remove)
      Removes the nth item in the list with the specified name.
      void remove​(java.lang.String name, java.lang.String value)
      Removes the first item in the list with the specified name and value.
      void remove​(java.lang.String name, java.lang.String value, int remove)
      Removes the nth item in the list with the specified name and value.
      boolean setValue​(java.lang.String name, java.lang.String value)
      Sets the value of the first name/value pair in the list that has the specified name.
      boolean setValue​(java.lang.String name, java.lang.String value, int index)
      Sets the value of the nth name/value pair in the list that has the specified name.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • QueryData

        public QueryData​(long cPtr,
                         boolean cMemoryOwn)
        For internal use only. Of no interest to the user.
        Parameters:
        cPtr - pointer to the CPP wrapped object
        cMemoryOwn - indicates if this object is responsible for memory management of the CPP object
      • QueryData

        public QueryData()
        Creates a QueryData object with no name/value pairs.
      • QueryData

        public QueryData​(java.lang.String urlEncoded)
        Initialises a new instance of the QueryData class by parsing a URL encoded string.
        Parameters:
        urlEncoded - the query section of a url, i.e. the part after the ?
    • Method Detail

      • getCPtr

        public static long getCPtr​(QueryData obj)
        For internal use only. Of no interest to the user.
        Parameters:
        obj - a reference to an object of this class
        Returns:
        a long containing the address of the CPP wrapped object
      • delete

        public void delete()
      • add

        public void add​(java.lang.String name,
                        java.lang.String value)
        Adds a name/value pair to the end of the list.
        Parameters:
        name - the name to add
        value - the value to add
      • add

        public void add​(java.lang.String name,
                        QueryData queryData)
        Adds a name/value pair to the end of the list, using a value extracted from another QueryData object. The name used to find the name/value pair in the other object is the same as the name parameter passed.

        This is equivalent to add(name, queryData.getValue(name)).

        Parameters:
        name - the name to add
        queryData - the QueryData object from which to extract the value to add
      • insert

        public void insert​(java.lang.String name,
                           QueryData queryData,
                           int index)
        Inserts a name/value pair into the list at the specified index, using a value extracted from another QueryData object. The name used to find the name/value pair in the other object is the same as the name parameter passed.

        This is equivalent to insert(name, queryData.getValue(name), index).

        Parameters:
        name - the name to insert
        queryData - the QueryData object from which to extract the value to add
        index - the zero-based position at which to insert the name/value pair in the list
      • insert

        public void insert​(java.lang.String name,
                           java.lang.String value,
                           int index)
        Inserts a name/value pair into the list at the specified index.
        Parameters:
        name - the name to insert
        value - the value to insert
        index - the zero-based position at which to insert the name/value pair in the list
      • getCount

        public int getCount()
        Gets the number of name/value pairs in the list.
        Returns:
        the number of name/value pairs in the list
      • getNameCount

        public int getNameCount​(java.lang.String name)
        Gets the number of name/value pairs in the list that have the specified name.
        Parameters:
        name - the name to look for
        Returns:
        the number of name/value pairs in the list that have the specified name
      • getValue

        public java.lang.String getValue​(java.lang.String name)
        Gets the value of the first name/value pair in the list that has the specified name.
        Parameters:
        name - the name to look for
        Returns:
        the first value in the list that has the specified name
        See Also:
        getValue(String, int), getNameCount(String)
      • getValue

        public java.lang.String getValue​(java.lang.String name,
                                         int index)
        Gets the value of the nth name/value pair in the list that has the specified name.

        e.g.

        
         // Find all the values with the name "r"
         for (int i = 0; i < queryData.getNameCount("r"); i++)
         {
             String name = queryData.getValue("r", i);
         }
         
        Parameters:
        name - the name to look for
        index - the zero-based index of the value to return
        Returns:
        the nth value in the list that has the specified name
        See Also:
        getValue(String), getNameCount(String)
      • setValue

        public boolean setValue​(java.lang.String name,
                                java.lang.String value,
                                int index)
        Sets the value of the nth name/value pair in the list that has the specified name.

        If the list contains fewer than n items with the specified name, then a new name/value pair is added to the end of the list.

        Parameters:
        name - the name to look for
        value - the value to set
        index - the zero-based index of the item to change
        Returns:
        true if the nth item with the specified name was set successfully, or false if a new name/value pair was added
        See Also:
        setValue(String, String), getValue(String)
      • setValue

        public boolean setValue​(java.lang.String name,
                                java.lang.String value)
        Sets the value of the first name/value pair in the list that has the specified name.

        If the list does not contain an item with the specified name, then a new name/value pair is added to the end of the list.

        Parameters:
        name - the name to look for
        value - the value to set
        Returns:
        true if the first item with the specified name was set successfully, or false if a new name/value pair was added
        See Also:
        setValue(String, String, int), getValue(String, int)
      • remove

        public void remove​(java.lang.String name,
                           int remove)
        Removes the nth item in the list with the specified name.
        Parameters:
        name - the name to look for
        remove - the zero-based index of the item to remove
        See Also:
        remove(String), add(String, String)
      • remove

        public void remove​(java.lang.String name,
                           java.lang.String value,
                           int remove)
        Removes the nth item in the list with the specified name and value.
        Parameters:
        name - the name to look for
        value - the value to look for
        remove - the zero-based index of the item to remove
        See Also:
        remove(String), remove(String, int), remove(String, String), add(String, String)
      • remove

        public void remove​(java.lang.String name,
                           java.lang.String value)
        Removes the first item in the list with the specified name and value.
        Parameters:
        name - the name to look for
        value - the value to look for
      • get

        public NameValuePair get​(int index)
        Gets the NameValuePair at the specified index in the list.

        e.g.

        
         // List all the name/value pairs in queryData
         for (int i = 0; i < queryData.getCount(); i++)
         {
             NameValuePair pair = queryData.get(i);
             writeMessage(String.format("%s=%s", pair.getName(), pair.getValue()));
         }
         
        Parameters:
        index - the zero-based index of the name/value pair to get
        Returns:
        the NameValuePair at the specified index in the list
      • clear

        public void clear()
        Removes all of the name/value pairs from the list.