List of usage examples for java.util SortedMap subMap
SortedMap<K, V> subMap(K fromKey, K toKey);
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
public void testLastKey_after_subMap() { K[] keys = getSortedKeys();/*from w w w. j a va2 s . c o m*/ V[] values = getSortedValues(); NavigableMap<K, V> map = createNavigableMap(); map.put(keys[0], values[0]); map.put(keys[1], values[1]); map.put(keys[2], values[2]); SortedMap<K, V> subMap = map; K firstKey = subMap.firstKey(); for (int i = 0; i < map.size(); i++) { K lastKey = subMap.lastKey(); subMap = subMap.subMap(firstKey, lastKey); } }
From source file:edu.vt.middleware.ldap.search.SearchExecutor.java
/** * This performs a Ldap search with the supplied <code>Query</code>. * * @param ldapPool <code>LdapPool</code> to use for searching * @param query <code>Query</code> to search for * * @return <code>Iterator</code> - of search results * * @throws PeopleSearchException if an error occurs searching the Ldap *///from w ww.ja v a 2 s. c o m public Iterator<SearchResult> executeSearch(final LdapPool<Ldap> ldapPool, final Query query) throws PeopleSearchException { final SortedMap<Integer, SearchResult> m = new TreeMap<Integer, SearchResult>(); final List<String> l = new ArrayList<String>(); if (this.queryTemplates != null && this.queryTemplates.size() > 0) { final SearchThread[] threads = new SearchThread[this.queryTemplates.size()]; for (int i = 1; i <= this.queryTemplates.size(); i++) { final String ldapQuery = this.buildLdapQuery(this.getQueryTemplate(query.getQueryParameters(), i), query.getSearchRestrictions()); if (ldapQuery != null) { threads[i - 1] = new SearchThread(ldapPool, ldapQuery, query.getQueryAttributes()); } else { threads[i - 1] = null; } } if (LOG.isDebugEnabled()) { LOG.debug("Performing search with " + threads.length + " threads"); } int count = 0; boolean loop = true; while (loop && count < threads.length) { List<QueryResult> results = null; if (additive) { results = this.doAdditiveSearch(threads); } else { results = this.doIterativeSearch(threads[count]); } if (LOG.isDebugEnabled()) { LOG.debug("Loop " + count + " found " + results.size() + " results"); } for (int i = 0; i < results.size(); i++) { int j = m.size(); final QueryResult qr = results.get(i); if (qr != null) { final SearchResult sr = qr.getSearchResult(); if (sr != null) { if (!l.contains(sr.getName())) { if (additive) { qr.setSearchIteration(i); qr.setTermCount(this.termCount); this.processResults(qr); } else { qr.setSearchIteration(count); qr.setTermCount(this.termCount); this.processResults(qr); } // store for results m.put(new Integer(j++), sr); l.add(sr.getName()); if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) { LOG.debug("Query " + (additive ? i : count) + " found: " + sr.getName()); } } } } } } if (additive || !m.isEmpty()) { loop = false; } else { count++; } } } else { if (LOG.isWarnEnabled()) { LOG.warn("No searches configured"); } } Iterator<SearchResult> i = null; if (query.getFromResult() != null) { if (query.getToResult() != null) { if (query.getFromResult().intValue() <= query.getToResult().intValue()) { i = m.subMap(query.getFromResult(), query.getToResult()).values().iterator(); } } else { i = m.tailMap(query.getFromResult()).values().iterator(); } } else if (query.getToResult() != null) { i = m.headMap(query.getToResult()).values().iterator(); } else { i = m.values().iterator(); } return i; }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.Map.put(Object, Object)'. This test shows some * bad behavior of the TreeMap class before JDK 7. A mapping with null key can * be put in but several methods are are unusable afterward. * * A SortedMap with natural ordering (no comparator) is supposed to throw a * null pointer exception if a null keys are "not supported". For a natural * ordered TreeMap before JDK 7, a null pointer exception is not thrown. But, * the map is left in a state where any other key based methods result in a * null pointer exception.//from w w w .ja v a 2 s.co m * * @see java.util.Map#put(Object, Object) */ public void testPut_nullKey() { K[] keys = getSortedKeys(); V[] values = getSortedValues(); SortedMap<K, V> sortedMap = createNavigableMap(); if (useNullKey()) { assertNull(sortedMap.put(null, values[0])); assertTrue(sortedMap.containsValue(values[0])); // the map methods the continue to function sortedMap.containsValue(null); sortedMap.containsValue(values[0]); sortedMap.entrySet(); sortedMap.equals(createMap()); sortedMap.hashCode(); sortedMap.isEmpty(); sortedMap.keySet(); sortedMap.putAll(createMap()); sortedMap.size(); sortedMap.values(); // all of the sorted map methods still function sortedMap.comparator(); sortedMap.firstKey(); sortedMap.lastKey(); sortedMap.subMap(getLessThanMinimumKey(), getGreaterThanMaximumKey()); sortedMap.headMap(getLessThanMinimumKey()); sortedMap.tailMap(getLessThanMinimumKey()); } else if (TestUtils.getJdkVersion() > 6) { // nulls are rejected immediately and don't poison the map anymore try { assertNull(sortedMap.put(null, values[0])); fail("should have thrown"); } catch (NullPointerException e) { // expected outcome } try { assertNull(sortedMap.put(null, values[1])); fail("expected exception adding second null"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.containsKey(null); fail("expected exception on containsKey(null)"); } catch (NullPointerException e) { // expected outcome } sortedMap.containsKey(keys[0]); try { sortedMap.get(null); fail("expected exception on get(null)"); } catch (NullPointerException e) { // expected outcome } sortedMap.get(keys[0]); try { sortedMap.remove(null); } catch (NullPointerException e) { // expected } sortedMap.remove(keys[0]); } else { // before JDK 7, nulls poisoned the map try { assertNull(sortedMap.put(null, values[0])); // note: first null added is not required to throw NPE since no // comparisons are needed } catch (NullPointerException e) { // expected outcome } try { assertNull(sortedMap.put(null, values[1])); fail("expected exception adding second null"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.containsKey(null); fail("expected exception on containsKey(null)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.containsKey(keys[0]); fail("expected exception on contains(key)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.get(null); fail("expected exception on get(null)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.get(keys[0]); fail("expected exception on get(key)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.remove(null); fail("expected exception on remove(null)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.remove(keys[0]); fail("expected exception on remove(key)"); } catch (NullPointerException e) { // expected outcome } } }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.SortedMap.lastKey()'. * * @see java.util.SortedMap#lastKey()// w w w . j av a 2 s. c om */ public void testLastKey() { K[] keys = getSortedKeys(); V[] values = getSortedValues(); SortedMap<K, V> map = createNavigableMap(); // test with a single entry map map.put(keys[0], values[0]); assertEquals(keys[0], map.lastKey()); // is it consistent with other methods assertEquals(map.keySet().toArray()[0], map.lastKey()); assertEquals(keys[0], map.firstKey()); assertEquals(map.firstKey(), map.lastKey()); // test with two entry map map.put(keys[1], values[1]); assertEquals(keys[1], map.lastKey()); assertFalse(keys[0].equals(map.lastKey())); // is it consistent with other methods assertEquals(map.keySet().toArray()[1], map.lastKey()); assertEquals(keys[0], map.firstKey()); assertFalse(map.firstKey().equals(map.lastKey())); map.put(keys[2], values[2]); map.put(keys[3], values[3]); assertEquals(keys[0], map.headMap(keys[1]).lastKey()); assertEquals(keys[keys.length - 1], map.tailMap(keys[2]).lastKey()); assertEquals(keys[2], map.subMap(keys[1], keys[3]).lastKey()); }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
public void testHeadMapLjava_lang_ObjectZL() { K[] keys = getSortedKeys();// www . j a v a2 s. co m V[] values = getSortedValues(); NavigableMap<K, V> map = createNavigableMap(); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } // normal case SortedMap<K, V> subMap = map.headMap(keys[2], true); assertEquals(3, subMap.size()); subMap = map.headMap(keys[3], true); assertEquals(4, subMap.size()); for (int i = 0; i < 4; i++) { assertEquals(values[i], subMap.get(keys[i])); } subMap = map.headMap(keys[2], false); assertEquals(2, subMap.size()); assertNull(subMap.get(keys[3])); // Exceptions assertEquals(0, map.headMap(keys[0], false).size()); try { map.headMap(null, true); assertTrue("expected exception", useNullKey()); } catch (NullPointerException e) { assertFalse("unexpected NPE", useNullKey()); } try { map.headMap(null, false); assertTrue("expected exception", useNullKey()); } catch (NullPointerException e) { assertFalse("unexpected NPE", useNullKey()); } subMap = map.headMap(keys[2]); assertEquals(2, subMap.size()); try { subMap.put(keys[2], values[2]); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } assertEquals(keys.length, map.size()); subMap = map.headMap(keys[2], true); assertEquals(3, subMap.size()); subMap.remove(keys[1]); assertFalse(subMap.containsKey(keys[1])); assertFalse(subMap.containsValue(values[1])); assertFalse(map.containsKey(keys[1])); assertFalse(map.containsValue(values[1])); assertEquals(2, subMap.size()); assertEquals(keys.length - 1, map.size()); subMap.put(keys[1], values[1]); try { subMap.subMap(keys[1], keys[3]); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { subMap.subMap(keys[3], keys[1]); fail("should throw IllegalArgumentException"); } catch (IllegalArgumentException expected) { } if (useNullKey() && useNullValue()) { map.put(null, null); subMap = map.headMap(null, true); assertEquals(1, subMap.size()); assertTrue(subMap.containsValue(null)); assertNull(subMap.get(null)); subMap = map.subMap(null, false, keys[2], true); assertEquals(3, subMap.size()); Set<K> keySet = subMap.keySet(); assertEquals(3, keySet.size()); Set<Map.Entry<K, V>> entrySet = subMap.entrySet(); assertEquals(3, entrySet.size()); Collection<V> valueCollection = subMap.values(); assertEquals(3, valueCollection.size()); map.remove(null); } // head map of head map NavigableMap<K, V> headMap = map.headMap(keys[3], true); assertEquals(4, headMap.size()); headMap = headMap.headMap(keys[3], false); assertEquals(3, headMap.size()); headMap = headMap.headMap(keys[2], false); assertEquals(2, headMap.size()); headMap = headMap.tailMap(keys[0], false); assertEquals(1, headMap.size()); headMap = headMap.tailMap(keys[1], false); assertEquals(0, headMap.size()); }
From source file:org.nuclos.common2.StringUtils.java
/** * Returns a living view of the given map containing all strings that starts with the given * prefix (including the prefix string itself). The view is backed by the original map. * <p>/* w ww. ja va2 s.c o m*/ * Note: This method uses {@link SortedMap#subMap(Object, Object)} internally and works * <em>only</em> for sorted string maps with natural (lexiographic) ordering! */ public static <V> SortedMap<String, V> submapWithPrefix(SortedMap<String, V> map, String prefix) { if (map.comparator() != null) throw new IllegalArgumentException("only natural (lexiographic) ordering supported"); int length = prefix.length(); if (length == 0) return map; // create a string lhs which is the _least higher string_ for the prefix, i.e. // there is no other string value between any prefixed string and lhs w.r.t ordering. StringBuilder lhs = new StringBuilder(prefix); char ch = lhs.charAt(length - 1); lhs.setCharAt(length - 1, (char) (ch + 1)); return map.subMap(prefix, lhs.toString()); }
From source file:therian.operator.immutablecheck.DefaultImmutableChecker.java
private static void addTypeTo(final Set<Class<?>> target, final SortedMap<String, ?> sortedMap) { addTypeTo(target, (Map<String, ?>) sortedMap); addTypeTo(target, (Map<String, ?>) sortedMap.headMap("foo")); addTypeTo(target, (Map<String, ?>) sortedMap.tailMap("foo")); addTypeTo(target, (Map<String, ?>) sortedMap.subMap("foo", "foo")); }