Search in sources :

Example 1 with CollationKey

use of java.text.CollationKey in project j2objc by google.

the class NodeSorter method compare.

/**
   * Return the results of a compare of two nodes.
   * TODO: Optimize compare -- cache the getStringExpr results, key by m_selectPat + hash of node.
   *
   * @param n1 First node to use in compare
   * @param n2 Second node to use in compare
   * @param kIndex Index of NodeSortKey to use for sort
   * @param support XPath context to use
   *
   * @return The results of the compare of the two nodes.
   *
   * @throws TransformerException
   */
int compare(NodeCompareElem n1, NodeCompareElem n2, int kIndex, XPathContext support) throws TransformerException {
    int result = 0;
    NodeSortKey k = (NodeSortKey) m_keys.elementAt(kIndex);
    if (k.m_treatAsNumbers) {
        double n1Num, n2Num;
        if (kIndex == 0) {
            n1Num = ((Double) n1.m_key1Value).doubleValue();
            n2Num = ((Double) n2.m_key1Value).doubleValue();
        } else if (kIndex == 1) {
            n1Num = ((Double) n1.m_key2Value).doubleValue();
            n2Num = ((Double) n2.m_key2Value).doubleValue();
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
      double n1Num = (double)n1.m_keyValue[kIndex];
      double n2Num = (double)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1Num = r1.num();
            // Can't use NaN for compare. They are never equal. Use zero instead.
            // That way we can keep elements in document order. 
            //n1Num = Double.isNaN(d) ? 0.0 : d;
            n2Num = r2.num();
        //n2Num = Double.isNaN(d) ? 0.0 : d;
        }
        if ((n1Num == n2Num) && ((kIndex + 1) < m_keys.size())) {
            result = compare(n1, n2, kIndex + 1, support);
        } else {
            double diff;
            if (Double.isNaN(n1Num)) {
                if (Double.isNaN(n2Num))
                    diff = 0.0;
                else
                    diff = -1;
            } else if (Double.isNaN(n2Num))
                diff = 1;
            else
                diff = n1Num - n2Num;
            // process order parameter 
            result = (int) ((diff < 0.0) ? (k.m_descending ? 1 : -1) : (diff > 0.0) ? (k.m_descending ? -1 : 1) : 0);
        }
    } else // end treat as numbers 
    {
        CollationKey n1String, n2String;
        if (kIndex == 0) {
            n1String = (CollationKey) n1.m_key1Value;
            n2String = (CollationKey) n2.m_key1Value;
        } else if (kIndex == 1) {
            n1String = (CollationKey) n1.m_key2Value;
            n2String = (CollationKey) n2.m_key2Value;
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
        String n1String = (String)n1.m_keyValue[kIndex];
        String n2String = (String)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1String = k.m_col.getCollationKey(r1.str());
            n2String = k.m_col.getCollationKey(r2.str());
        }
        // Use collation keys for faster compare, but note that whitespaces 
        // etc... are treated differently from if we were comparing Strings.
        result = n1String.compareTo(n2String);
        //Process caseOrder parameter
        if (k.m_caseOrderUpper) {
            String tempN1 = n1String.getSourceString().toLowerCase();
            String tempN2 = n2String.getSourceString().toLowerCase();
            if (tempN1.equals(tempN2)) {
                //java defaults to upper case is greater.
                result = result == 0 ? 0 : -result;
            }
        }
        //Process order parameter
        if (k.m_descending) {
            result = -result;
        }
    }
    if (0 == result) {
        if ((kIndex + 1) < m_keys.size()) {
            result = compare(n1, n2, kIndex + 1, support);
        }
    }
    if (0 == result) {
        // I shouldn't have to do this except that there seems to 
        // be a glitch in the mergesort
        // if(r1.getType() == r1.CLASS_NODESET)
        // {
        // %OPT%
        DTM dtm = support.getDTM(n1.m_node);
        result = dtm.isNodeAfter(n1.m_node, n2.m_node) ? -1 : 1;
    // }
    }
    return result;
}
Also used : CollationKey(java.text.CollationKey) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 2 with CollationKey

use of java.text.CollationKey in project jdbc-shards by wplatform.

the class CompareModeDefault method getKey.

private CollationKey getKey(String a) {
    synchronized (collationKeys) {
        CollationKey key = collationKeys.get(a);
        if (key == null) {
            key = collator.getCollationKey(a);
            collationKeys.put(a, key);
        }
        return key;
    }
}
Also used : CollationKey(java.text.CollationKey)

Example 3 with CollationKey

use of java.text.CollationKey in project zm-mailbox by Zimbra.

the class Entry method sortByDisplayName.

/**
     * Sort a collection of Entries by locale-sensitive String comparison on
     * each entry's displayName.  If there is no display name, use entry.getLabel()
     * as the key.
     */
public static List<Entry> sortByDisplayName(Collection<? extends Entry> entries, Locale locale) {
    List<Entry> sorted = Lists.newArrayList();
    // short-circuit if there is only one entry or no entry
    if (entries.size() <= 1) {
        sorted.addAll(entries);
    } else {
        Collator collator = Collator.getInstance(locale);
        TreeMultimap<CollationKey, Entry> map = TreeMultimap.create(Ordering.natural(), new SortByLabelAsc());
        for (Entry entry : entries) {
            String key = entry.getAttr(Provisioning.A_displayName);
            if (key == null) {
                key = entry.getLabel();
            }
            CollationKey collationKey = collator.getCollationKey(key);
            map.put(collationKey, entry);
        }
        sorted.addAll(map.values());
    }
    return sorted;
}
Also used : CollationKey(java.text.CollationKey) Collator(java.text.Collator)

Example 4 with CollationKey

use of java.text.CollationKey in project robovm by robovm.

the class NodeSorter method compare.

/**
   * Return the results of a compare of two nodes.
   * TODO: Optimize compare -- cache the getStringExpr results, key by m_selectPat + hash of node.
   *
   * @param n1 First node to use in compare
   * @param n2 Second node to use in compare
   * @param kIndex Index of NodeSortKey to use for sort
   * @param support XPath context to use
   *
   * @return The results of the compare of the two nodes.
   *
   * @throws TransformerException
   */
int compare(NodeCompareElem n1, NodeCompareElem n2, int kIndex, XPathContext support) throws TransformerException {
    int result = 0;
    NodeSortKey k = (NodeSortKey) m_keys.elementAt(kIndex);
    if (k.m_treatAsNumbers) {
        double n1Num, n2Num;
        if (kIndex == 0) {
            n1Num = ((Double) n1.m_key1Value).doubleValue();
            n2Num = ((Double) n2.m_key1Value).doubleValue();
        } else if (kIndex == 1) {
            n1Num = ((Double) n1.m_key2Value).doubleValue();
            n2Num = ((Double) n2.m_key2Value).doubleValue();
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
      double n1Num = (double)n1.m_keyValue[kIndex];
      double n2Num = (double)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1Num = r1.num();
            // Can't use NaN for compare. They are never equal. Use zero instead.
            // That way we can keep elements in document order. 
            //n1Num = Double.isNaN(d) ? 0.0 : d;
            n2Num = r2.num();
        //n2Num = Double.isNaN(d) ? 0.0 : d;
        }
        if ((n1Num == n2Num) && ((kIndex + 1) < m_keys.size())) {
            result = compare(n1, n2, kIndex + 1, support);
        } else {
            double diff;
            if (Double.isNaN(n1Num)) {
                if (Double.isNaN(n2Num))
                    diff = 0.0;
                else
                    diff = -1;
            } else if (Double.isNaN(n2Num))
                diff = 1;
            else
                diff = n1Num - n2Num;
            // process order parameter 
            result = (int) ((diff < 0.0) ? (k.m_descending ? 1 : -1) : (diff > 0.0) ? (k.m_descending ? -1 : 1) : 0);
        }
    } else // end treat as numbers 
    {
        CollationKey n1String, n2String;
        if (kIndex == 0) {
            n1String = (CollationKey) n1.m_key1Value;
            n2String = (CollationKey) n2.m_key1Value;
        } else if (kIndex == 1) {
            n1String = (CollationKey) n1.m_key2Value;
            n2String = (CollationKey) n2.m_key2Value;
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
        String n1String = (String)n1.m_keyValue[kIndex];
        String n2String = (String)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1String = k.m_col.getCollationKey(r1.str());
            n2String = k.m_col.getCollationKey(r2.str());
        }
        // Use collation keys for faster compare, but note that whitespaces 
        // etc... are treated differently from if we were comparing Strings.
        result = n1String.compareTo(n2String);
        //Process caseOrder parameter
        if (k.m_caseOrderUpper) {
            String tempN1 = n1String.getSourceString().toLowerCase();
            String tempN2 = n2String.getSourceString().toLowerCase();
            if (tempN1.equals(tempN2)) {
                //java defaults to upper case is greater.
                result = result == 0 ? 0 : -result;
            }
        }
        //Process order parameter
        if (k.m_descending) {
            result = -result;
        }
    }
    if (0 == result) {
        if ((kIndex + 1) < m_keys.size()) {
            result = compare(n1, n2, kIndex + 1, support);
        }
    }
    if (0 == result) {
        // I shouldn't have to do this except that there seems to 
        // be a glitch in the mergesort
        // if(r1.getType() == r1.CLASS_NODESET)
        // {
        // %OPT%
        DTM dtm = support.getDTM(n1.m_node);
        result = dtm.isNodeAfter(n1.m_node, n2.m_node) ? -1 : 1;
    // }
    }
    return result;
}
Also used : CollationKey(java.text.CollationKey) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 5 with CollationKey

use of java.text.CollationKey in project robovm by robovm.

the class OldCollationKeyTest method test_toByteArray.

public void test_toByteArray() {
    // Test for method byte [] java.text.CollationKey.toByteArray()
    Collator collator = Collator.getInstance();
    collator.setStrength(Collator.PRIMARY);
    CollationKey key1 = collator.getCollationKey("abc");
    byte[] bytes = key1.toByteArray();
    assertTrue("Not enough bytes", bytes.length >= 3);
    try {
        collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7");
    } catch (ParseException e) {
        fail("ParseException");
        return;
    }
    /*
         * CollationElementIterator it =
         * ((RuleBasedCollator)collator).getCollationElementIterator("1234567");
         * int order; while ((order = it.next()) !=
         * CollationElementIterator.NULLORDER) {
         * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length;
         * i+=2) { System.out.print(Integer.toHexString(bytes[i]) +
         * Integer.toHexString(bytes[i+1]) + " "); } System.out.println();
         */
    // The RI has a different algorithm to generate the collation keys.
    // bytes = collator.getCollationKey("1234567").toByteArray();
    // byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1,
    //         0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 };
    byte[] bytes1 = collator.getCollationKey("12").toByteArray();
    byte[] bytes2 = collator.getCollationKey("123").toByteArray();
    byte[] bytes3 = collator.getCollationKey("124").toByteArray();
    byte[] bytes4 = collator.getCollationKey("1245").toByteArray();
    byte[] bytes5 = collator.getCollationKey("1245").toByteArray();
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes1, bytes2) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes2, bytes3) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes3, bytes4) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes4, bytes5) == 0);
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator) CollationKey(java.text.CollationKey) ParseException(java.text.ParseException) RuleBasedCollator(java.text.RuleBasedCollator) Collator(java.text.Collator)

Aggregations

CollationKey (java.text.CollationKey)6 Collator (java.text.Collator)2 DTM (org.apache.xml.dtm.DTM)2 XObject (org.apache.xpath.objects.XObject)2 ParseException (java.text.ParseException)1 RuleBasedCollator (java.text.RuleBasedCollator)1