Example #1
0
  public void run() {
    //
    // Test for TERTIARY(default)
    //
    doCompare(compData1);
    doEquals(compData2);

    //
    // Test for SECONDARY
    //
    col.setStrength(Collator.SECONDARY);
    doCompare(compData1);
    doEquals(compData2);

    //
    // Test for PRIMARY
    //
    col.setStrength(Collator.PRIMARY);
    doCompare(compData1);
    doEquals(compData2);

    if (result != 0) {
      throw new RuntimeException("Unexpected results on Korean collation.");
    }
  }
  @Override
  public int compare(Portlet portlet1, Portlet portlet2) {
    String portletTitle1 = StringPool.BLANK;
    String portletTitle2 = StringPool.BLANK;

    if (_servletContext != null) {
      portletTitle1 = PortalUtil.getPortletTitle(portlet1, _servletContext, _locale);
      portletTitle2 = PortalUtil.getPortletTitle(portlet2, _servletContext, _locale);
    } else {
      portletTitle1 = PortalUtil.getPortletTitle(portlet1, _locale);
      portletTitle2 = PortalUtil.getPortletTitle(portlet2, _locale);
    }

    if (Validator.isNull(portletTitle1) && Validator.isNull(portletTitle2)) {

      return 0;
    }

    if (Validator.isNull(portletTitle1)) {
      return 1;
    }

    if (Validator.isNull(portletTitle2)) {
      return -1;
    }

    Collator collator = Collator.getInstance(_locale);

    return collator.compare(portletTitle1, portletTitle2);
  }
 /**
  * Retrieves all related items of any type.
  *
  * @param inItem The item whose related will be retrieved.
  * @return Collection<IItem> of <code>IItem</code>
  * @throws VException
  * @throws SQLException
  */
 public static Collection<ItemWithIcon> getRelatedItems(final IItem inItem)
     throws VException, SQLException {
   // term
   final List<ItemWithIcon> outRelated =
       getItems(
           BOMHelper.getJoinRelatedTerm1Home().select(getKey1(inItem, IItem.TERM)),
           new TermWrapper());
   outRelated.addAll(
       getItems(
           BOMHelper.getJoinRelatedTerm2Home().select(getKey2(inItem, IItem.TERM)),
           new TermWrapper()));
   // text
   outRelated.addAll(
       getItems(
           BOMHelper.getJoinRelatedText1Home().select(getKey1(inItem, IItem.TEXT)),
           new TextWrapper()));
   outRelated.addAll(
       getItems(
           BOMHelper.getJoinRelatedText2Home().select(getKey2(inItem, IItem.TEXT)),
           new TextWrapper()));
   // person
   outRelated.addAll(
       getItems(
           BOMHelper.getJoinRelatedPerson1Home().select(getKey1(inItem, IItem.PERSON)),
           new PersonWrapper()));
   outRelated.addAll(
       getItems(
           BOMHelper.getJoinRelatedPerson2Home().select(getKey2(inItem, IItem.PERSON)),
           new PersonWrapper()));
   // sort
   final Collator lCollator = new ItemCollator();
   lCollator.setStrength(Collator.SECONDARY);
   Collections.sort(outRelated, lCollator);
   return outRelated;
 }
Example #4
0
  public ArrayList<HelpEntry> getSortedHelp(Player player, int start, int size, String plugin) {
    ArrayList<HelpEntry> ret = new ArrayList<HelpEntry>();
    if (!pluginHelpList.containsKey(plugin)) {
      return ret;
    } else {
      List<String> names = new ArrayList<String>(pluginHelpList.get(plugin).keySet());
      Collator collator = Collator.getInstance();
      collator.setStrength(Collator.SECONDARY);
      Collections.sort(names, collator);

      int index = 0;
      int currentCount = 0;
      int lineLength = 0;
      while (index < names.size() && lineLength < size) {
        String currName = names.get(index);
        HelpEntry entry = pluginHelpList.get(plugin).get(currName);
        if (entry.playerCanUse(player) && entry.visible) {
          if (currentCount >= start) {
            ret.add(entry);
            lineLength += entry.lineLength;
          } else {
            currentCount++;
          }
        }
        index++;
      }
      return ret;
    }
  }
Example #5
0
 private static int compareName(Vector source, Vector target) {
   Collator collator = Collator.getInstance(new Locale("vi", "VN"));
   int totalS = source.size() - 1;
   int totalT = target.size() - 1;
   String nameS = "";
   String nameT = "";
   int compare = 0;
   for (int s = totalS, t = totalT; (s > -1) && (t > -1); s--, t--) {
     nameS = (String) source.get(s);
     nameT = (String) target.get(t);
     compare = collator.compare(nameS, nameT);
     // compare = nameS.compareTo(nameT);
     if (compare > 0) {
       return 1;
     } else if (compare < 0) {
       return -1;
     } else {
       continue;
     }
   }
   if (totalS > totalT) {
     return 1;
   } else if (totalS < totalT) {
     return -1;
   } else {
     return 0;
   }
 }
Example #6
0
 /**
  * Compare two UML elements names, ignoring case, using names from the path as tie breakers. As a
  * convenience, we also compare simple strings using the same primary strength collator.
  *
  * @param o1 first model element
  * @param o2 second model element
  * @return -1, 0, 1
  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  */
 public int compare(Object o1, Object o2) {
   if (o1 == null) {
     if (o2 == null) {
       return 0;
     }
     return -1;
   }
   if (o2 == null) {
     return 1;
   }
   if (o1.equals(o2)) {
     return 0;
   }
   if (o1 instanceof String && o2 instanceof String) {
     return collator.compare((String) o1, (String) o2);
   }
   // Elements are collated first by name hoping for a quick solution
   String name1, name2;
   try {
     name1 = Model.getFacade().getName(o1);
     name2 = Model.getFacade().getName(o2);
   } catch (IllegalArgumentException e) {
     throw new ClassCastException("Model element or String required");
   }
   if (name1 != null && name2 != null) {
     int comparison = collator.compare(name1, name2);
     if (comparison != 0) {
       return comparison;
     }
   }
   // and then by their enclosing path to fully distinguish them
   return comparePaths(o1, o2);
 }
Example #7
0
  @Before
  public void setUp() throws Exception {
    sort = SrtTextReader.sortForCodepage(65001);

    Collator collator = sort.getCollator();
    collator.setStrength(Collator.TERTIARY);
  }
Example #8
0
  public static net.osmand.Collator primaryCollator() {
    final Collator instance = Collator.getInstance();
    instance.setStrength(Collator.PRIMARY);
    return new net.osmand.Collator() {

      @Override
      public int compare(Object o1, Object o2) {
        return instance.compare(o1, o2);
      }

      @Override
      public boolean equals(Object obj) {
        return instance.equals(obj);
      }

      @Override
      public boolean equals(String source, String target) {
        return instance.equals(source, target);
      }

      @Override
      public int compare(String source, String target) {
        return instance.compare(source, target);
      }
    };
  }
Example #9
0
  public static boolean isSortedDescending(List<String> list, int startIndex) {
    Collator collator = Collator.getInstance(Locale.getDefault());
    boolean sorted = true;
    for (int i = startIndex; i < list.size(); i++) {
      if (collator.compare(list.get(i - 1), list.get(i)) < 0) sorted = false;
    }

    return sorted;
  }
 private static byte[] getCollationKeyInBytes(String paramString)
 {
   if (mColl == null)
   {
     Collator localCollator = Collator.getInstance();
     mColl = localCollator;
     localCollator.setStrength(0);
   }
   return mColl.getCollationKey(paramString).toByteArray();
 }
Example #11
0
 /* equals() should return true for each combination. */
 void doEquals(String[][] s) {
   for (int i = 0; i < s.length; i++) {
     if (!col.equals(s[i][0], s[i][1])) {
       result++;
       System.err.println(
           "TERTIARY: The first string should be equals to the second string:  "
               + s[i][2]
               + "  compare() returned "
               + col.compare(s[i][0], s[i][1] + "."));
     }
   }
 }
Example #12
0
    public long evalAsInteger(Focus focus, EvalContext context) throws EvaluationException {
      context.at(this);
      String s1 = args[0].evalAsOptString(focus, context);
      String s2 = args[1].evalAsOptString(focus, context);

      if (s1 == null || s2 == null) throw EmptyException.instance();

      Collator coll = getCollator(args.length <= 2 ? null : args[2], focus, context);
      context.at(this);
      // use Comparison.of to obtain EQ LT GT etc
      return Comparison.of(coll != null ? coll.compare(s1, s2) : s1.compareTo(s2), 0);
    }
Example #13
0
 public int compare(Object o1, Object o2) {
   SelectItem i1 = (SelectItem) o1;
   SelectItem i2 = (SelectItem) o2;
   RuleBasedCollator collator_ini = (RuleBasedCollator) Collator.getInstance();
   try {
     RuleBasedCollator collator =
         new RuleBasedCollator(collator_ini.getRules().replaceAll("<'\u005f'", "<' '<'\u005f'"));
     return collator.compare(i1.getLabel(), i2.getLabel());
   } catch (ParseException e) {
   }
   return Collator.getInstance().compare(i1.getLabel(), i2.getLabel());
 }
Example #14
0
  public static boolean isSortedAscending(List<String> list, int startIndex) {
    Collator collator = Collator.getInstance(new Locale("tr", "TR"));

    boolean sorted = true;
    for (int i = startIndex; i < list.size(); i++) {
      // System.out.println(list.get(i-1) +" vs "+list.get(i));
      // System.out.println(collator.compare(list.get(i-1), list.get(i)));
      if (collator.compare(list.get(i - 1), list.get(i)) > 0) sorted = false;
    }

    return sorted;
  }
 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setLayout(new FillLayout());
   final Table table = new Table(shell, SWT.BORDER);
   table.setHeaderVisible(true);
   final TableColumn column1 = new TableColumn(table, SWT.NONE);
   column1.setText("Column 1");
   final TableColumn column2 = new TableColumn(table, SWT.NONE);
   column2.setText("Column 2");
   TableItem item = new TableItem(table, SWT.NONE);
   item.setText(new String[] {"a", "3"});
   item = new TableItem(table, SWT.NONE);
   item.setText(new String[] {"b", "2"});
   item = new TableItem(table, SWT.NONE);
   item.setText(new String[] {"c", "1"});
   column1.setWidth(100);
   column2.setWidth(100);
   Listener sortListener =
       e -> {
         TableItem[] items = table.getItems();
         Collator collator = Collator.getInstance(Locale.getDefault());
         TableColumn column = (TableColumn) e.widget;
         int index = column == column1 ? 0 : 1;
         for (int i = 1; i < items.length; i++) {
           String value1 = items[i].getText(index);
           for (int j = 0; j < i; j++) {
             String value2 = items[j].getText(index);
             if (collator.compare(value1, value2) < 0) {
               String[] values = {items[i].getText(0), items[i].getText(1)};
               items[i].dispose();
               TableItem item1 = new TableItem(table, SWT.NONE, j);
               item1.setText(values);
               items = table.getItems();
               break;
             }
           }
         }
         table.setSortColumn(column);
       };
   column1.addListener(SWT.Selection, sortListener);
   column2.addListener(SWT.Selection, sortListener);
   table.setSortColumn(column1);
   table.setSortDirection(SWT.UP);
   shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) display.sleep();
   }
   display.dispose();
 }
  private boolean referenceNameIsValid() {
    if (getSourceTable() == null) return true;

    for (Iterator references = getSourceTable().references(); references.hasNext(); ) {
      MWReference reference = (MWReference) references.next();
      String name = reference.getName();
      Collator ignoreCaseCollator = Collator.getInstance();
      ignoreCaseCollator.setStrength(Collator.PRIMARY);

      if (ignoreCaseCollator.compare(name, getReferenceName()) == 0) return false;
    }

    return true;
  }
Example #17
0
  private void rebuildSections() {
    sectionKeys = new ArrayList<String>();
    graphObjectsBySection = new HashMap<String, ArrayList<T>>();
    graphObjectsById = new HashMap<String, T>();
    displaySections = false;

    if (cursor == null || cursor.getCount() == 0) {
      return;
    }

    int objectsAdded = 0;
    cursor.moveToFirst();
    do {
      T graphObject = cursor.getGraphObject();

      if (!filterIncludesItem(graphObject)) {
        continue;
      }

      objectsAdded++;

      String sectionKeyOfItem = getSectionKeyOfGraphObject(graphObject);
      if (!graphObjectsBySection.containsKey(sectionKeyOfItem)) {
        sectionKeys.add(sectionKeyOfItem);
        graphObjectsBySection.put(sectionKeyOfItem, new ArrayList<T>());
      }
      List<T> section = graphObjectsBySection.get(sectionKeyOfItem);
      section.add(graphObject);

      graphObjectsById.put(getIdOfGraphObject(graphObject), graphObject);
    } while (cursor.moveToNext());

    if (sortFields != null) {
      final Collator collator = Collator.getInstance();
      for (List<T> section : graphObjectsBySection.values()) {
        Collections.sort(
            section,
            new Comparator<GraphObject>() {
              @Override
              public int compare(GraphObject a, GraphObject b) {
                return compareGraphObjects(a, b, sortFields, collator);
              }
            });
      }
    }

    Collections.sort(sectionKeys, Collator.getInstance());

    displaySections = sectionKeys.size() > 1 && objectsAdded > DISPLAY_SECTIONS_THRESHOLD;
  }
  /** Returns a list of all files under the root directory by absolute path. */
  public static void buildFileList(List<String> lst, File rootDir, boolean recursive) {
    internalBuildFileList(lst, rootDir, recursive);

    // Get the local collator and set its strength to PRIMARY
    final Collator localCollator = Collator.getInstance(Locale.getDefault());
    localCollator.setStrength(Collator.PRIMARY);
    Collections.sort(
        lst,
        new Comparator<String>() {
          @Override
          public int compare(String o1, String o2) {
            return localCollator.compare(o1, o2);
          }
        });
  }
Example #19
0
    public Collator getObject(
        CollatorProvider collatorProvider, Locale locale, String key, Object... params) {
      assert params.length == 1;
      Collator result = collatorProvider.getInstance(locale);
      if (result != null) {
        // put this Collator instance in the cache for two locales, one
        // is for the desired locale, and the other is for the actual
        // locale where the provider is found, which may be a fall back locale.
        cache.put((Locale) params[0], result);
        cache.put(locale, result);
        return (Collator) result.clone();
      }

      return null;
    }
Example #20
0
  @Test
  public void setLabelKey_de() {

    // given
    SitemapNode node = new SitemapNode();
    node.setTranslate(translate);
    Locale locale = Locale.GERMAN;
    Collator collator = Collator.getInstance(locale);
    // when

    node.setLabelKey(TestLabelKey.Yes, locale, collator);
    // then
    assertThat(node.getLabelKey()).isEqualTo(TestLabelKey.Yes);
    assertThat(node.getLabel()).isEqualTo("Ja");
    assertThat(node.getCollationKey()).isEqualTo(collator.getCollationKey("Ja"));
  }
Example #21
0
  private void sort() {
    final Locale loc = getLocale();
    final Collator collator = Collator.getInstance(loc);
    final Comparator<Locale> comp =
        new Comparator<Locale>() {
          public int compare(Locale a, Locale b) {
            return collator.compare(a.getDisplayName(loc), b.getDisplayName(loc));
          }
        };
    Arrays.sort(locales, comp);
    setModel(
        new ComboBoxModel<Locale>() {
          public Locale getElementAt(int i) {
            return locales[i];
          }

          public int getSize() {
            return locales.length;
          }

          public void addListDataListener(ListDataListener l) {}

          public void removeListDataListener(ListDataListener l) {}

          public Locale getSelectedItem() {
            return selected >= 0 ? locales[selected] : null;
          }

          public void setSelectedItem(Object anItem) {
            if (anItem == null) selected = -1;
            else selected = Arrays.binarySearch(locales, (Locale) anItem, comp);
          }
        });
    setSelectedItem(selected);
  }
Example #22
0
 public static CityObj[] loadCitiesDataBase(Context c) {
   final Collator collator = Collator.getInstance();
   Resources r = c.getResources();
   // Read strings array of name,timezone, id
   // make sure the list are the same length
   String[] cities = r.getStringArray(R.array.cities_names);
   String[] timezones = r.getStringArray(R.array.cities_tz);
   String[] ids = r.getStringArray(R.array.cities_id);
   if (cities.length != timezones.length || ids.length != cities.length) {
     Log.wtf("City lists sizes are not the same, cannot use the data");
     return null;
   }
   CityObj[] tempList = new CityObj[cities.length];
   for (int i = 0; i < cities.length; i++) {
     tempList[i] = new CityObj(cities[i], timezones[i], ids[i]);
   }
   // Sort alphabetically
   Arrays.sort(
       tempList,
       new Comparator<CityObj>() {
         @Override
         public int compare(CityObj c1, CityObj c2) {
           Comparator<CityObj> mCollator;
           return collator.compare(c1.mCityName, c2.mCityName);
         }
       });
   return tempList;
 }
 public List<PoiFilter> getUserDefinedPoiFilters() {
   if (cacheUserDefinedFilters == null) {
     cacheUserDefinedFilters = new ArrayList<PoiFilter>();
     PoiFilter filter =
         new PoiFilter(
             application.getString(R.string.poi_filter_custom_filter),
             PoiFilter.CUSTOM_FILTER_ID,
             new LinkedHashMap<AmenityType, LinkedHashSet<String>>(),
             application); //$NON-NLS-1$
     filter.setStandardFilter(true);
     cacheUserDefinedFilters.add(filter);
     filter = new SearchByNameFilter(application);
     cacheUserDefinedFilters.add(filter);
     PoiFilterDbHelper helper = openDbHelper();
     List<PoiFilter> userDefined = helper.getFilters(helper.getReadableDatabase());
     final Collator instance = Collator.getInstance();
     Collections.sort(
         userDefined,
         new Comparator<PoiFilter>() {
           @Override
           public int compare(PoiFilter object1, PoiFilter object2) {
             return instance.compare(object1.getName(), object2.getName());
           }
         });
     cacheUserDefinedFilters.addAll(userDefined);
     helper.close();
   }
   return Collections.unmodifiableList(cacheUserDefinedFilters);
 }
Example #24
0
  private static void assertObsolete(String newCode, String oldCode, String displayName) {
    // Either code should get you the same locale.
    Locale newLocale = new Locale(newCode);
    Locale oldLocale = new Locale(oldCode);
    assertEquals(newLocale, oldLocale);

    // No matter what code you used to create the locale, you should get the old code back.
    assertEquals(oldCode, newLocale.getLanguage());
    assertEquals(oldCode, oldLocale.getLanguage());

    // Check we get the right display name.
    assertEquals(displayName, newLocale.getDisplayLanguage(newLocale));
    assertEquals(displayName, oldLocale.getDisplayLanguage(newLocale));
    assertEquals(displayName, newLocale.getDisplayLanguage(oldLocale));
    assertEquals(displayName, oldLocale.getDisplayLanguage(oldLocale));

    // Check that none of the 'getAvailableLocales' methods are accidentally returning two
    // equal locales (because to ICU they're different, but we mangle one into the other).
    assertOnce(newLocale, BreakIterator.getAvailableLocales());
    assertOnce(newLocale, Calendar.getAvailableLocales());
    assertOnce(newLocale, Collator.getAvailableLocales());
    assertOnce(newLocale, DateFormat.getAvailableLocales());
    assertOnce(newLocale, DateFormatSymbols.getAvailableLocales());
    assertOnce(newLocale, NumberFormat.getAvailableLocales());
    assertOnce(newLocale, Locale.getAvailableLocales());
  }
    @Override
    public int compare(GdlTerm arg0, GdlTerm arg1) {
      GdlConstant a1 = (GdlConstant) arg0;
      GdlConstant a2 = (GdlConstant) arg1;
      String s1 = a1.toString();
      String s2 = a2.toString();

      int num1 = -1;
      int num2 = -1;

      try {
        num1 = Integer.parseInt(s1);
      } catch (Exception ex) {
      }
      try {
        num2 = Integer.parseInt(s2);
      } catch (Exception ex) {
      }

      if (num1 == -1 && num2 == -1) return Collator.getInstance().compare(s1, s2);

      if (num1 == -1) return 1;

      if (num2 == -1) return -1;

      return num1 - num2;
    }
Example #26
0
  @Test
  public void constructor_de() {

    // given
    Locale locale = Locale.GERMAN;
    Collator collator = Collator.getInstance(locale);

    // when
    SitemapNode node =
        new SitemapNode("one", PublicHomeView.class, TestLabelKey.Yes, locale, collator, translate);
    // then
    assertThat(node.getUriSegment()).isEqualTo("one");
    assertThat(node.getViewClass()).isEqualTo(PublicHomeView.class);
    assertThat(node.getLabel()).isEqualTo("Ja");
    assertThat(node.getCollationKey()).isEqualTo(collator.getCollationKey("Ja"));
  }
 private static byte[] getCollationKeyInBytes(String name) {
   if (mColl == null) {
     mColl = Collator.getInstance();
     mColl.setStrength(Collator.PRIMARY);
   }
   return mColl.getCollationKey(name).toByteArray();
 }
  public AppNameComparator(Context context) {
    mCollator = Collator.getInstance();
    mAppInfoComparator =
        new AbstractUserComparator<ItemInfo>(context) {

          @Override
          public final int compare(ItemInfo a, ItemInfo b) {
            // Order by the title in the current locale
            int result = compareTitles(a.title.toString(), b.title.toString());
            if (result == 0 && a instanceof AppInfo && b instanceof AppInfo) {
              AppInfo aAppInfo = (AppInfo) a;
              AppInfo bAppInfo = (AppInfo) b;
              // If two apps have the same title, then order by the component name
              result = aAppInfo.componentName.compareTo(bAppInfo.componentName);
              if (result == 0) {
                // If the two apps are the same component, then prioritize by the order that
                // the app user was created (prioritizing the main user's apps)
                return super.compare(a, b);
              }
            }
            return result;
          }
        };
    mSectionNameComparator =
        new Comparator<String>() {
          @Override
          public int compare(String o1, String o2) {
            return compareTitles(o1, o2);
          }
        };
  }
Example #29
0
 @Override
 public int compare(final Country country1, Country country2) {
   final String name1 = country1.getName().getContent();
   final String name2 = country2.getName().getContent();
   final int c = Collator.getInstance().compare(name1, name2);
   if (c == 0) {
     final String acronym1 = country1.getAcronym();
     final String acronym2 = country2.getAcronym();
     if (acronym1 == null || acronym2 == null) {
       return country2.hashCode() - country1.hashCode();
     }
     final int a = Collator.getInstance().compare(acronym1, acronym2);
     return a == 0 ? country2.hashCode() - country1.hashCode() : a;
   }
   return c;
 }
Example #30
0
 private int comparePersonAsString(Person p, Person person) {
   String sP1 =
       p.getFio().getLastName()
           + " "
           + p.getFio().getMidllName()
           + " "
           + p.getFio().getFirstName();
   String sP2 =
       person.getFio().getLastName()
           + " "
           + person.getFio().getMidllName()
           + " "
           + person.getFio().getFirstName();
   Collator collator = Collator.getInstance(new Locale("Ru"));
   return collator.compare(sP1, sP2);
 }