Example usage for java.text Collator getInstance

List of usage examples for java.text Collator getInstance

Introduction

In this page you can find the example usage for java.text Collator getInstance.

Prototype

public static synchronized Collator getInstance() 

Source Link

Document

Gets the Collator for the current default locale.

Usage

From source file:org.sakaiproject.sitestats.tool.wicket.providers.ReportsDataProvider.java

public final Comparator<Stat> getReportDataComparator(final String fieldName, final boolean sortAscending,
        final StatsManager SST_sm, final EventRegistryService SST_ers, final UserDirectoryService M_uds) {
    return new Comparator<Stat>() {
        private transient Collator collator = Collator.getInstance();
        {/* w  ww  .  j av  a  2  s  .  c o m*/
            try {
                collator = new RuleBasedCollator(((RuleBasedCollator) Collator.getInstance()).getRules()
                        .replaceAll("<'\u005f'", "<' '<'\u005f'"));
            } catch (ParseException e) {
                logger.error("Unable to create RuleBasedCollator");
            }
        }

        public int compare(Stat r1, Stat r2) {
            if (fieldName.equals(COL_SITE)) {
                String s1 = Locator.getFacade().getSiteService().getSiteDisplay(r1.getSiteId()).toLowerCase();
                String s2 = Locator.getFacade().getSiteService().getSiteDisplay(r2.getSiteId()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_USERID)) {
                String s1;
                try {
                    s1 = M_uds.getUser(r1.getUserId()).getDisplayId();
                } catch (UserNotDefinedException e) {
                    s1 = "-";
                }
                String s2;
                try {
                    s2 = M_uds.getUser(r2.getUserId()).getDisplayId();
                } catch (UserNotDefinedException e) {
                    s2 = "-";
                }
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_USERNAME)) {
                String s1 = Locator.getFacade().getStatsManager().getUserNameForDisplay(r1.getUserId())
                        .toLowerCase();
                String s2 = Locator.getFacade().getStatsManager().getUserNameForDisplay(r2.getUserId())
                        .toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_EVENT)) {
                EventStat es1 = (EventStat) r1;
                EventStat es2 = (EventStat) r2;
                String s1 = SST_ers.getEventName(es1.getEventId()).toLowerCase();
                String s2 = SST_ers.getEventName(es2.getEventId()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_TOOL)) {
                EventStat es1 = (EventStat) r1;
                EventStat es2 = (EventStat) r2;
                String s1 = SST_ers.getToolName(es1.getToolId()).toLowerCase();
                String s2 = SST_ers.getToolName(es2.getToolId()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_RESOURCE)) {
                ResourceStat rs1 = (ResourceStat) r1;
                ResourceStat rs2 = (ResourceStat) r2;
                String s1 = SST_sm.getResourceName(rs1.getResourceRef()).toLowerCase();
                String s2 = SST_sm.getResourceName(rs2.getResourceRef()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_ACTION)) {
                ResourceStat rs1 = (ResourceStat) r1;
                ResourceStat rs2 = (ResourceStat) r2;
                String s1 = ((String) rs1.getResourceAction()).toLowerCase();
                String s2 = ((String) rs2.getResourceAction()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_DATE)) {
                int res = r1.getDate() != null ? r1.getDate().compareTo(r2.getDate()) : -1;
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_TOTAL)) {
                int res = Long.valueOf(r1.getCount()).compareTo(Long.valueOf(r2.getCount()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_VISITS)) {
                SiteVisits sv1 = (SiteVisits) r1;
                SiteVisits sv2 = (SiteVisits) r2;
                int res = Long.valueOf(sv1.getTotalVisits()).compareTo(Long.valueOf(sv2.getTotalVisits()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_UNIQUEVISITS)) {
                SiteVisits sv1 = (SiteVisits) r1;
                SiteVisits sv2 = (SiteVisits) r2;
                int res = Long.valueOf(sv1.getTotalUnique()).compareTo(Long.valueOf(sv2.getTotalUnique()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_DURATION)) {
                SitePresence sv1 = (SitePresence) r1;
                SitePresence sv2 = (SitePresence) r2;
                int res = Long.valueOf(sv1.getDuration()).compareTo(Long.valueOf(sv2.getDuration()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            }
            return 0;
        }
    };
}

From source file:ro.nextreports.designer.FormLoader.java

public List<File> getFiles(String folderPath, String extension) {
    DataSourceManager manager = DefaultDataSourceManager.getInstance();
    DataSource ds = manager.getConnectedDataSource();
    List<File> result = new ArrayList<File>();
    if (ds == null) {
        return result;
    }/* w w w .  j ava  2s .com*/
    File file = new File(folderPath);
    File[] files = file.listFiles();
    if (files == null) {
        return new ArrayList<File>();
    }

    for (int i = 0, size = files.length; i < size; i++) {
        //            System.out.println(names[i]);
        int index = files[i].getName().indexOf(extension);
        if (index != -1) {
            if (files[i].getName().endsWith(extension)) {
                result.add(files[i]);
            }
        } else {
            if (file.isDirectory()) {
                result.add(files[i]);
            }
        }
    }
    Collections.sort(result, new Comparator<File>() {
        public int compare(File o1, File o2) {
            if ((o1.isDirectory() && o2.isDirectory()) || (o1.isFile() && o2.isFile())) {
                return Collator.getInstance().compare(o1.getName(), o2.getName());
            } else {
                if (o1.isDirectory()) {
                    return -1;
                } else {
                    return 1;
                }
            }
        }
    });
    return result;
}

From source file:de.blizzy.backup.BackupPlugin.java

public List<StorageInterceptorDescriptor> getStorageInterceptors() {
    List<StorageInterceptorDescriptor> result = new ArrayList<>();
    IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(ID + ".storageInterceptors"); //$NON-NLS-1$
    IExtension[] extensions = point.getExtensions();
    for (IExtension extension : extensions) {
        for (IConfigurationElement element : extension.getConfigurationElements()) {
            try {
                String id = extension.getUniqueIdentifier();
                String name = element.getAttribute("name"); //$NON-NLS-1$
                IStorageInterceptor storageInterceptor = (IStorageInterceptor) element
                        .createExecutableExtension("class"); //$NON-NLS-1$
                result.add(new StorageInterceptorDescriptor(id, name, storageInterceptor));
            } catch (CoreException e) {
                logError("error while creating storage interceptor", e); //$NON-NLS-1$
            }/*from  w w w. j a  va  2 s  .  c  o  m*/
        }
    }
    Collections.sort(result, new Comparator<StorageInterceptorDescriptor>() {
        @Override
        public int compare(StorageInterceptorDescriptor d1, StorageInterceptorDescriptor d2) {
            return Collator.getInstance().compare(d1.getName(), d2.getName());
        }
    });
    return result;
}

From source file:simon.databasedemo.DemoFragment.java

@Override
protected void setUpMap() {
    //addCircles();

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();

        // Initializing
        Marker markero;/*from  w  w  w .j  av a 2s.  c  om*/
        Marker markerd;

        @Override
        public void onMapClick(LatLng point) {
            // Already two locations
            if (markerPoints.size() > 1) {

                markerPoints.clear();
                map.clear();
                Calendar c = Calendar.getInstance();
                int day;
                day = c.get(Calendar.DAY_OF_MONTH);
                MarkerGenerator.setUpMap(map, day, month);
            }

            // Adding new item to the ArrayList
            markerPoints.add(point);

            // Creating MarkerOptions
            MarkerOptions options = new MarkerOptions();

            // Setting the position of the marker
            options.position(point);

            //???

            if (markerPoints.size() == 1) {
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); //?
                //markero = map.addMarker(options);
            } else if (markerPoints.size() == 2) {
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)); //?
                //markerd = map.addMarker(options);
            }

            // Add new marker to the Google Map Android API V2
            map.addMarker(options);

            // Checks, whether start and end locations are captured
            if (markerPoints.size() >= 2) {
                LatLng origin = markerPoints.get(0);
                LatLng dest = markerPoints.get(1);

                // Getting URL to the Google Directions API
                String url = getDirectionsUrl(origin, dest);

                DownloadTask downloadTask = new DownloadTask();
                downloadTask.execute(url);
                //                    Uri gmmIntentUri = Uri.parse("geo:" + origin + "?q=" + dest);
                //                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                //                    mapIntent.setPackage("com.google.android.apps.maps");
                //                    startActivity(mapIntent);
            }

        }
    });

    updateClustering(clusterSizeSeekbar.getProgress(), clusterCheckbox.isChecked());

    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        private TextView tv;

        {
            tv = new TextView(getActivity());
            tv.setTextColor(Color.BLACK);
        }

        private Collator collator = Collator.getInstance();
        private Comparator<Marker> comparator = new Comparator<Marker>() {
            public int compare(Marker lhs, Marker rhs) {
                String leftTitle = lhs.getTitle();
                String rightTitle = rhs.getTitle();
                if (leftTitle == null && rightTitle == null) {
                    return 0;
                }
                if (leftTitle == null) {
                    return 1;
                }
                if (rightTitle == null) {
                    return -1;
                }
                return collator.compare(leftTitle, rightTitle);
            }
        };

        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            if (marker.isCluster()) {
                List<Marker> markers = marker.getMarkers();
                int i = 0;
                String text = "";
                while (i < 3 && markers.size() > 0) {
                    Marker m = Collections.min(markers, comparator);
                    String title = m.getTitle();
                    if (title == null) {
                        break;
                    }
                    text += title + "\n";
                    markers.remove(m);
                    i++;
                }
                if (text.length() == 0) {
                    text = "Markers with mutable data";
                } else if (markers.size() > 0) {
                    text += "and " + markers.size() + " more...";
                } else {
                    text = text.substring(0, text.length() - 1);
                }
                tv.setText(text);
                return tv;
            } else {
                if (marker.getData() instanceof MutableData) {
                    MutableData mutableData = marker.getData();
                    tv.setText("Value: " + mutableData.value);
                    return tv;
                }
            }

            return null;
        }
    });

    map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            if (marker.isCluster()) {
                List<Marker> markers = marker.getMarkers();
                LatLngBounds.Builder builder = LatLngBounds.builder();
                for (Marker m : markers) {
                    builder.include(m.getPosition());
                }
                LatLngBounds bounds = builder.build();
                map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,
                        getResources().getDimensionPixelSize(R.dimen.padding)));
            }
        }
    });
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.6440, 120.9337), 7));
    Calendar c = Calendar.getInstance();
    int day;
    day = c.get(Calendar.DAY_OF_MONTH);
    MarkerGenerator.setUpMap(map, day, month);
    BitmapDescriptor icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE);
    //        for (MutableData data : dataArray) {
    //            map.addMarker(new MarkerOptions().position(data.position).icon(icon).data(data));
    //        }
}

From source file:simonlang.coastdove.core.detection.AppDetectionDataSetup.java

/**
 * Parses all "android:id" values using the given XmlResourceParser. The parser must be opened before
 * calling this function.//from  ww w. j ava2s  .  com
 * @param parser            Parser to parse the data from
 * @param resourceParser    ARSC parser to parse resource strings from, in case the XML is parsed from binary.
 *                          If null, resource strings are not replaced.
 * @return Set of all "android:id" values occurring in the XML file
 */
private static Set<String> parseAndroidIDs(XmlPullParser parser, @Nullable ARSCFileParser resourceParser) {
    Set<String> result = new TreeSet<>(Collator.getInstance());
    try {
        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() != XmlPullParser.START_TAG)
                continue;
            for (int i = 0; i < parser.getAttributeCount(); ++i) {
                if (parser.getAttributeName(i).equals("id")) {
                    String androidID = parser.getAttributeValue(i).substring(1);
                    // Parse resource string if needed
                    if (resourceParser != null) {
                        int resourceID = Integer.parseInt(androidID);
                        ARSCFileParser.AbstractResource resource = resourceParser.findResource(resourceID);
                        if (resource == null)
                            continue; // cannot be parsed, do not add
                        androidID = "id/" + resource.getResourceName();
                    }
                    result.add(androidID);
                }
            }
        }
    } catch (XmlPullParserException e) {
        Log.e("AppDetectionDataSetup", "Unable to parse from XML file: " + e.getMessage());
    } catch (IOException e) {
        Log.e("AppDetectionDataSetup", "IO error: " + e.getMessage());
    }
    return result;
}

From source file:com.weebly.opus1269.copyeverywhere.ui.clipviewer.ClipViewerFragment.java

public void setClipItem(ClipItem clipItem) {

    if (!Collator.getInstance().equals(clipItem.getText(), mClipItem.getText())) {
        // skip repaint if text is same
        final TextView textView = (TextView) getActivity().findViewById(R.id.clipViewerText);
        if (textView != null) {
            //force layout change animation
            textView.setVisibility(View.GONE);
            textView.setVisibility(View.VISIBLE);

            textView.setText(clipItem.getText());
        }//from w  w w  . jav  a  2 s.c  o  m
        setViewerTitle();
    }

    mClipItem = clipItem;
    mOnClipChanged.onClipChanged(mClipItem);
}

From source file:com.espertech.esper.regression.resultset.TestOrderBySimple.java

public void testCollatorSortLocale() {
    String frenchForSin = "p\u00E9ch\u00E9";
    String frenchForFruit = "p\u00EAche";

    String[] sortedFrench = (frenchForFruit + "," + frenchForSin).split(",");

    assertEquals(1, frenchForFruit.compareTo(frenchForSin));
    assertEquals(-1, frenchForSin.compareTo(frenchForFruit));
    Locale.setDefault(Locale.FRENCH);
    assertEquals(1, frenchForFruit.compareTo(frenchForSin));
    assertEquals(-1, Collator.getInstance().compare(frenchForFruit, frenchForSin));
    assertEquals(-1, frenchForSin.compareTo(frenchForFruit));
    assertEquals(1, Collator.getInstance().compare(frenchForSin, frenchForFruit));
    assertFalse(frenchForSin.equals(frenchForFruit));

    /*//ww w.  j a  va 2  s.  c o m
    Collections.sort(items);
    System.out.println("Sorted default" + items);
            
    Collections.sort(items, new Comparator<String>() {
    Collator collator = Collator.getInstance(Locale.FRANCE);
    public int compare(String o1, String o2)
    {
        return collator.compare(o1, o2);
    }
    });
    System.out.println("Sorted FR" + items);
    */

    Configuration config = SupportConfigFactory.getConfiguration();
    config.getEngineDefaults().getLanguage().setSortUsingCollator(true);
    epService = EPServiceProviderManager.getDefaultProvider(config);
    epService.initialize();
    epService.getEPAdministrator().getConfiguration().addEventType("SupportBean", SupportBean.class.getName());

    // test order by
    String stmtText = "select theString from SupportBean.win:keepall() order by theString asc";
    EPStatement stmtOne = epService.getEPAdministrator().createEPL(stmtText);
    epService.getEPRuntime().sendEvent(new SupportBean(frenchForSin, 1));
    epService.getEPRuntime().sendEvent(new SupportBean(frenchForFruit, 1));
    EPAssertionUtil.assertPropsPerRow(stmtOne.iterator(), "theString".split(","),
            new Object[][] { { sortedFrench[0] }, { sortedFrench[1] } });

    // test sort view
    SupportUpdateListener listener = new SupportUpdateListener();
    stmtText = "select irstream theString from SupportBean.ext:sort(2, theString asc)";
    EPStatement stmtTwo = epService.getEPAdministrator().createEPL(stmtText);
    stmtTwo.addListener(listener);

    epService.getEPRuntime().sendEvent(new SupportBean(frenchForSin, 1));
    epService.getEPRuntime().sendEvent(new SupportBean(frenchForFruit, 1));
    epService.getEPRuntime().sendEvent(new SupportBean("abc", 1));

    assertEquals(frenchForSin, listener.getLastOldData()[0].get("theString"));
    Locale.setDefault(Locale.US);
}

From source file:org.sakaiproject.tool.roster.RosterGroupMembership.java

private Comparator<GroupedParticipants> sortByGroup() {
    Comparator<GroupedParticipants> groupComparator = new Comparator<GroupedParticipants>() {
        public int compare(GroupedParticipants one, GroupedParticipants another) {
            try {
                RuleBasedCollator r_collator = new RuleBasedCollator(
                        ((RuleBasedCollator) Collator.getInstance()).getRules().replaceAll("<'\u005f'",
                                "<' '<'\u005f'"));
                return r_collator.compare(one.getGroupTitle(), another.getGroupTitle());
            } catch (ParseException e) {
                return Collator.getInstance().compare(one.getGroupTitle(), another.getGroupTitle());
            }/*from   w  w  w.jav  a  2  s. c  om*/
        }
    };
    return groupComparator;
}

From source file:org.sakaiproject.sitestats.tool.wicket.providers.ReportDefsProvider.java

public final Comparator<ReportDef> getReportDefComparator() {
    return new Comparator<ReportDef>() {
        private transient Collator collator = Collator.getInstance();
        {//w w  w . ja  v a  2  s . c o m
            try {
                collator = new RuleBasedCollator(((RuleBasedCollator) Collator.getInstance()).getRules()
                        .replaceAll("<'\u005f'", "<' '<'\u005f'"));
            } catch (ParseException e) {
                log.error("Unable to create RuleBasedCollator");
            }
        }

        public int compare(ReportDef o1, ReportDef o2) {
            String title1 = null;
            String title2 = null;
            if (o1.isTitleLocalized()) {
                title1 = (String) new ResourceModel(o1.getTitleBundleKey()).getObject();
            } else {
                title1 = o1.getTitle();
            }
            if (o2.isTitleLocalized()) {
                title2 = (String) new ResourceModel(o2.getTitleBundleKey()).getObject();
            } else {
                title2 = o2.getTitle();
            }
            return collator.compare(title1, title2);
        }

    };
}

From source file:com.weebly.opus1269.copyeverywhere.ui.clipviewer.ClipViewerFragment.java

/**
 * Highlight all occurrences of the given String
 *
 * @param highlightText the text to highlight (case insensitive)
 *//*from   w ww  .  j  a  v a2 s  .  co m*/
public void setHighlightText(String highlightText) {
    if (Collator.getInstance().equals(mHighlightText, highlightText)) {
        return;
    }

    mHighlightText = highlightText;

    final TextView textView = (TextView) getActivity().findViewById(R.id.clipViewerText);
    if (textView == null) {
        return;
    }

    if (TextUtils.isEmpty(highlightText)) {
        // make sure to reset spans
        textView.setText(mClipItem.getText());
    } else {
        final String text = mClipItem.getText();
        final Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
        final int color = ContextCompat.getColor(getContext(), R.color.accent_highlight);
        final Pattern p = Pattern.compile(highlightText, Pattern.CASE_INSENSITIVE);
        final Matcher m = p.matcher(text);
        final int length = highlightText.length();
        while (m.find()) {
            final int start = m.start();
            final int stop = start + length;
            //noinspection ObjectAllocationInLoop
            spanText.setSpan(new BackgroundColorSpan(color), start, stop, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        textView.setText(spanText);
    }
}