Search in sources :

Example 46 with Comparator

use of java.util.Comparator in project robovm by robovm.

the class ArraysTest method test_binarySearch$Ljava_lang_ObjectLjava_lang_ObjectLjava_util_Comparator.

/**
     * java.util.Arrays#binarySearch(java.lang.Object[],
     *        java.lang.Object, java.util.Comparator)
     */
public void test_binarySearch$Ljava_lang_ObjectLjava_lang_ObjectLjava_util_Comparator() {
    // Test for method int java.util.Arrays.binarySearch(java.lang.Object
    // [], java.lang.Object, java.util.Comparator)
    Comparator comp = new ReversedIntegerComparator();
    for (int counter = 0; counter < arraySize; counter++) objectArray[counter] = objArray[arraySize - counter - 1];
    assertTrue("Binary search succeeded for value not present in array 1", Arrays.binarySearch(objectArray, new Integer(-1), comp) == -(arraySize + 1));
    assertEquals("Binary search succeeded for value not present in array 2", -1, Arrays.binarySearch(objectArray, new Integer(arraySize), comp));
    for (int counter = 0; counter < arraySize; counter++) assertTrue("Binary search on Object[] with custom comparator answered incorrect position", Arrays.binarySearch(objectArray, objArray[counter], comp) == arraySize - counter - 1);
}
Also used : Comparator(java.util.Comparator)

Example 47 with Comparator

use of java.util.Comparator in project rstudio by rstudio.

the class BranchToolbarButton method populateMenu.

private void populateMenu(final ToolbarPopupMenu menu, final Map<String, List<String>> branchMap) {
    MapUtil.forEach(branchMap, new MapUtil.ForEachCommand<String, List<String>>() {

        @Override
        public void execute(final String caption, final List<String> branches) {
            // place commonly-used branches at the top
            Collections.sort(branches, new Comparator<String>() {

                private final String[] specialBranches_ = new String[] { "master", "develop", "trunk" };

                @Override
                public int compare(String o1, String o2) {
                    for (String specialBranch : specialBranches_) {
                        if (o1.endsWith(specialBranch))
                            return -1;
                        else if (o2.endsWith(specialBranch))
                            return 1;
                    }
                    return o1.compareToIgnoreCase(o2);
                }
            });
            menu.addSeparator(new CustomMenuItemSeparator() {

                @Override
                public Widget createMainWidget() {
                    String branchLabel = caption.equals(LOCAL_BRANCHES) ? LOCAL_BRANCHES : "(Remote: " + caption + ")";
                    Label label = new Label(branchLabel);
                    label.addStyleName(ThemeStyles.INSTANCE.menuSubheader());
                    label.getElement().getStyle().setPaddingLeft(2, Unit.PX);
                    return label;
                }
            });
            menu.addSeparator();
            for (String branch : branches) {
                // skip detached branches
                if (branch.contains("HEAD detached at"))
                    continue;
                // skip HEAD branches
                if (branch.contains("HEAD ->"))
                    continue;
                // construct branch label without remotes prefix
                final String branchLabel = branch.replaceAll("^remotes/" + caption + "/", "");
                final String branchValue = branch.replaceAll("\\s+\\-\\>.*", "");
                menu.addItem(new MenuItem(branchLabel, new SwitchBranchCommand(branchLabel, branchValue)));
            }
        }
    });
}
Also used : CustomMenuItemSeparator(org.rstudio.core.client.widget.CustomMenuItemSeparator) MapUtil(org.rstudio.core.client.MapUtil) Label(com.google.gwt.user.client.ui.Label) ArrayList(java.util.ArrayList) List(java.util.List) MenuItem(com.google.gwt.user.client.ui.MenuItem) JsArrayString(com.google.gwt.core.client.JsArrayString) Comparator(java.util.Comparator)

Example 48 with Comparator

use of java.util.Comparator in project StylishMusicPlayer by ryanhoo.

the class FolderPresenter method loadFolders.

@Override
public void loadFolders() {
    Subscription subscription = mRepository.folders().subscribeOn(Schedulers.io()).doOnNext(new Action1<List<Folder>>() {

        @Override
        public void call(List<Folder> folders) {
            Collections.sort(folders, new Comparator<Folder>() {

                @Override
                public int compare(Folder f1, Folder f2) {
                    return f1.getName().compareToIgnoreCase(f2.getName());
                }
            });
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Folder>>() {

        @Override
        public void onStart() {
            mView.showLoading();
        }

        @Override
        public void onCompleted() {
            mView.hideLoading();
        }

        @Override
        public void onError(Throwable e) {
            mView.hideLoading();
            mView.handleError(e);
        }

        @Override
        public void onNext(List<Folder> folders) {
            mView.onFoldersLoaded(folders);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : List(java.util.List) PlayList(io.github.ryanhoo.music.data.model.PlayList) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Folder(io.github.ryanhoo.music.data.model.Folder) Comparator(java.util.Comparator)

Example 49 with Comparator

use of java.util.Comparator in project spring-framework by spring-projects.

the class RequestMappingInfoTests method compareTwoHttpMethodsOneParam.

@Test
public void compareTwoHttpMethodsOneParam() {
    RequestMappingInfo none = paths().build();
    RequestMappingInfo oneMethod = paths().methods(RequestMethod.GET).build();
    RequestMappingInfo oneMethodOneParam = paths().methods(RequestMethod.GET).params("foo").build();
    ServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange();
    Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange);
    List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam);
    Collections.shuffle(list);
    list.sort(comparator);
    assertEquals(oneMethodOneParam, list.get(0));
    assertEquals(oneMethod, list.get(1));
    assertEquals(none, list.get(2));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Test(org.junit.Test) RequestMappingInfo.paths(org.springframework.web.reactive.result.method.RequestMappingInfo.paths) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Arrays.asList(java.util.Arrays.asList) Assert.assertFalse(org.junit.Assert.assertFalse) Comparator(java.util.Comparator) Collections(java.util.Collections) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) Test(org.junit.Test)

Example 50 with Comparator

use of java.util.Comparator in project spring-framework by spring-projects.

the class RequestMappingInfoTests method compareToWithHttpHeadMapping.

// SPR-14383
@Test
public void compareToWithHttpHeadMapping() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("HEAD");
    request.addHeader("Accept", "application/json");
    RequestMappingInfo noMethods = paths().build();
    RequestMappingInfo getMethod = paths().methods(GET).produces("application/json").build();
    RequestMappingInfo headMethod = paths().methods(HEAD).build();
    Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, request);
    List<RequestMappingInfo> list = asList(noMethods, getMethod, headMethod);
    Collections.shuffle(list);
    Collections.sort(list, comparator);
    assertEquals(headMethod, list.get(0));
    assertEquals(getMethod, list.get(1));
    assertEquals(noMethods, list.get(2));
}
Also used : RequestMappingInfo.paths(org.springframework.web.servlet.mvc.method.RequestMappingInfo.paths) HEAD(org.springframework.web.bind.annotation.RequestMethod.HEAD) HttpHeaders(org.springframework.http.HttpHeaders) Assert.assertNotNull(org.junit.Assert.assertNotNull) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Test(org.junit.Test) GET(org.springframework.web.bind.annotation.RequestMethod.GET) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) Assert.assertNull(org.junit.Assert.assertNull) Arrays.asList(java.util.Arrays.asList) Assert.assertFalse(org.junit.Assert.assertFalse) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

Comparator (java.util.Comparator)322 ArrayList (java.util.ArrayList)123 List (java.util.List)58 Test (org.junit.Test)58 HashMap (java.util.HashMap)50 IOException (java.io.IOException)36 Map (java.util.Map)35 File (java.io.File)24 HashSet (java.util.HashSet)23 TreeSet (java.util.TreeSet)20 Set (java.util.Set)18 Iterator (java.util.Iterator)15 Method (java.lang.reflect.Method)14 Collections (java.util.Collections)14 Date (java.util.Date)14 TreeMap (java.util.TreeMap)14 ArrayMap (android.util.ArrayMap)12 Collection (java.util.Collection)11 LinkedList (java.util.LinkedList)11 SimpleDateFormat (java.text.SimpleDateFormat)10