private List<KeyValue> convertAndOrderField(HashMap<String, Integer> tmpField, Integer threshold) { List<Entry<String, Integer>> list = new ArrayList<>(tmpField.entrySet()); Collections.sort(list, new Comparator<Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o2.getValue().compareTo(o1.getValue()); //Note: exchanged o2 and o1 for descending order! } }); List<KeyValue> result = new ArrayList<>(); int i = 0; for (Iterator<Entry<String, Integer>> it = list.iterator(); it.hasNext(); i++) { if (i >= threshold) break; Entry<String, Integer> entry = it.next(); result.add(new DefaultKeyValue(entry.getKey(), entry.getValue())); } return result; }
/** * Method to handle the fcbkcomplete library, which has "tag" hardcoded as * param. Changing it is not preferable, because of possible upgrades * * @param tag * @param currentUser * @param model * @return the view with results */ @RequestMapping("/autocompleteList") @ResponseBody public List<DefaultKeyValue> suggestUsersList(@RequestParam String tag) { List<UserDetails> details = userService.findUsers(tag); List<DefaultKeyValue> result = new ArrayList<DefaultKeyValue>(details.size()); for (UserDetails detail : details) { // doing HTML stuff here, which is not particularly nice, but no // other way to do it String pic = detail.getSmallPhotoUri(); if (pic == null) { pic = "http://www.gravatar.com/avatar/" + detail.getId() + "?s=16&d=identicon&r=PG"; } String display = "<img class=\"userAutocompletePicture\" " + "src=\"" + pic + "\"/>" + detail.getFirstName() + " " + detail.getLastName(); result.add(new DefaultKeyValue(display, "id:" + detail.getId())); } return result; }
/** * Method to handle the fcbkcomplete library, which has "tag" hardcoded as * param. Changing it is not preferable, because of possible upgrades * * @param tag * @param currentUser * @param model * @return the view with results */ @RequestMapping("/autocompleteList") @ResponseBody public List<DefaultKeyValue> suggestUsersList(@RequestParam String tag, @SessionAttribute User currentUser) { List<UserDetails> details = userService.suggestUsers(tag, currentUser); List<DefaultKeyValue> result = new ArrayList<DefaultKeyValue>(details.size()); for (UserDetails detail : details) { // doing HTML stuff here, which is not particularly nice, but no // other way to do it String pic = detail.getSmallProfilePictureURI(); if (pic == null) { pic = "http://www.gravatar.com/avatar/" + detail.getGravatarHash() + "?s=16&d=identicon&r=PG"; } String display = "<img class=\"userAutocompletePicture\" " + "src=\"" + pic + "\"/>" + detail.getNames() + " (" + detail.getUsername() + ")"; result.add(new DefaultKeyValue(display, detail.getId())); } return result; }
@Test(groups = {"unit"}) public void testReadColumns() throws Exception { Map<String, String> testResultMap = new HashMap<String,String>(); testResultMap.put(columnNames.get(0), columnValues.get(0)); testResultMap.put(columnNames.get(1), columnValues.get(1)); ColumnSlice columnSlice = mock(ColumnSlice.class); HColumn column1 = mock(HColumn.class); HColumn column2 = mock(HColumn.class); when(column1.getName()).thenReturn(columnNames.get(0)); when(column1.getValue()).thenReturn(columnValues.get(0)); when(column2.getName()).thenReturn(columnNames.get(1)); when(column2.getValue()).thenReturn(columnValues.get(1)); when(columnSlice.getColumns()).thenReturn(Arrays.asList(column1, column2)); when(executionResult.get()).thenReturn(columnSlice); //========================= List<KeyValue> actualResult = columnFamilyTestDao.readColumns(rowKey, new ColumnMapper<KeyValue,String,String>() { @Override public KeyValue mapColumn(String columnName, String columnValue) { return new DefaultKeyValue(columnName, columnValue); } }); //========================= Map<String,String> resultMap = new HashMap<String,String>(); for (KeyValue kv : actualResult) { resultMap.put((String)kv.getKey(), (String)kv.getValue()); } assertEquals(resultMap, testResultMap); }