Java Code Examples for java.util.SortedMap#subMap()

The following examples show how to use java.util.SortedMap#subMap() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: cognition   File: AccumuloItem.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Construct a map based upon all ColumnQualifiers and Values in the Column Family <code>columnFamily</code>,
 * optionally removing any used Key-Value pairs from <code>row</code>, the source map.
 *
 * @param row
 * @param columnFamily
 * @param clearUsed    when true, clear used entries from <code>row</code>
 * @return
 */
public static Map<String, String> readMap(SortedMap<Key, Value> row, String columnFamily, boolean clearUsed) {
  Map<String, String> retVal = new LinkedHashMap<>();
  String rowid = row.firstKey().getRow().toString();

  SortedMap<Key, Value> familyMap = row.subMap(
      new Key(rowid, columnFamily),
      new Key(rowid, columnFamily + "\0"));
  for (Entry<Key, Value> entry : familyMap.entrySet()) {
    retVal.put(entry.getKey().getColumnQualifier().toString(), entry.getValue().toString());
  }
  if (clearUsed) {
    familyMap.clear();
  }
  return retVal;
}
 
Example 2
Source Project: j2objc   File: TreeMapTest.java    License: Apache License 2.0 6 votes vote down vote up
public void test_lastKey_after_subMap() {
    TreeMap<String, String> tm = new TreeMap<String, String>();
    tm.put("001", "VAL001");
    tm.put("003", "VAL003");
    tm.put("002", "VAL002");
    SortedMap<String, String> sm = tm;
    String firstKey = (String) sm.firstKey();
    String lastKey = "";
    for (int i = 1; i <= tm.size(); i++) {
        try {
            lastKey = (String) sm.lastKey();
        } catch (NoSuchElementException excep) {
            fail("NoSuchElementException thrown when there are elements in the map");
        }
        sm = sm.subMap(firstKey, lastKey);
    }
}
 
Example 3
Source Project: secor   File: LogFileVerifier.java    License: Apache License 2.0 6 votes vote down vote up
private void filterOffsets(long fromOffset, long toOffset) {
    Iterator iterator = mTopicPartitionToOffsetToFiles.entrySet().iterator();
    while (iterator.hasNext()) {
        long firstOffset = -2;
        long lastOffset = Long.MAX_VALUE;
        Map.Entry entry = (Map.Entry) iterator.next();
        SortedMap<Long, HashSet<LogFilePath>> offsetToFiles =
            (SortedMap<Long, HashSet<LogFilePath>>) entry.getValue();
        for (long offset : offsetToFiles.keySet()) {
            if (offset <= fromOffset || firstOffset == -2) {
                firstOffset = offset;
            }
            if (offset >= toOffset && toOffset == Long.MAX_VALUE) {
                lastOffset = offset;
            }
        }
        if (firstOffset != -2) {
            TopicPartition topicPartition = (TopicPartition) entry.getKey();
            offsetToFiles = offsetToFiles.subMap(firstOffset, lastOffset);
            mTopicPartitionToOffsetToFiles.put(topicPartition, offsetToFiles);
        }
    }
}
 
Example 4
/**
 * @param qualifier
 * @param initialsToInfo this is where we are going to get the info from (currently: inner or top level list)
 * @param toks (out) the tokens will be added to this list
 * @return
 */
protected void getWithFilter(String qualifier, SortedMap<String, Set<IInfo>> initialsToInfo,
        Collection<IInfo> toks, Filter filter, boolean useLowerCaseQual) {
    String initials = getInitials(qualifier);
    String qualToCompare = qualifier;
    if (useLowerCaseQual) {
        qualToCompare = qualifier.toLowerCase();
    }

    //get until the end of the alphabet
    SortedMap<String, Set<IInfo>> subMap = initialsToInfo.subMap(initials, initials + "\uffff\uffff\uffff\uffff");

    for (Set<IInfo> listForInitials : subMap.values()) {

        for (IInfo info : listForInitials) {
            if (filter.doCompare(qualToCompare, info)) {
                toks.add(info);
            }
        }
    }
}
 
Example 5
/**
 * Returns sub map from SortedMap, where keys match the prefix
 */
private static <V> SortedMap<String, V> filterPrefix(SortedMap<String,V> baseMap, String prefix) {
    if(prefix.length() > 0) {
        char nextLetter = (char)(prefix.charAt(prefix.length() -1) + 1);
        String end = prefix.substring(0, prefix.length()-1) + nextLetter;
        return baseMap.subMap(prefix, end);
    }
    return baseMap;
}
 
Example 6
/**
 * Returns true if value is found in SortedMap, including also simple wildcard *
 */
private static <V> boolean simpleWildcardCheck(SortedMap<String,V> baseMap, String value) {
    if (value == null) {
        return false;
    }
    
    if (baseMap.get(value) != null) {
        //System.out.println("======= " + value);
        return true;
    } else if (value.length() > 0){
        String v = value;
        v = v.substring(0, v.length() - 1);
            
        while (v.length() > 0) {
            char nextLetter = (char)(v.charAt(v.length() -1) + 1);
            String end = v.substring(0, v.length()-1) + nextLetter;
            SortedMap<String, V> b = baseMap.subMap(v, end);
            
            for (String key : b.keySet()) {
                if ((key.length() == v.length() + 1) && key.endsWith("*")) {
                    //System.out.println("======= " + key);
                    return true;
                }
            }
            
            v = v.substring(0, v.length() - 1);
        }        
    }   
  return false;
}
 
Example 7
/**
 * Returns sub map from SortedMap, where keys match the prefix
 */
private static <V> SortedMap<String, V> filterPrefix(SortedMap<String,V> baseMap, String prefix) {
    if(prefix.length() > 0) {
        char nextLetter = (char)(prefix.charAt(prefix.length() -1) + 1);
        String end = prefix.substring(0, prefix.length()-1) + nextLetter;
        return baseMap.subMap(prefix, end);
    }
    return baseMap;
}
 
Example 8
/**
 * Returns Key if value is found in SortedMap, including also simple wildcard *
 */
public static <V> String simpleWildcardKeyFind(SortedMap<String,V> baseMap, String value) {
    if (value == null) {
        return null;
    }
    
    if (baseMap.get(value) != null) {
        //System.out.println("======= " + value);
        return value;
    } else if (value.length() > 0){
        String v = value;
        v = v.substring(0, v.length() - 1);
            
        while (v.length() > 0) {
            char nextLetter = (char)(v.charAt(v.length() -1) + 1);
            String end = v.substring(0, v.length()-1) + nextLetter;
            SortedMap<String, V> b = baseMap.subMap(v, end);
            
            for (String key : b.keySet()) {
                if ((key.length() == v.length() + 1) && key.endsWith("*")) {
                    //System.out.println("======= " + key);
                    return key;
                }
            }
            
            v = v.substring(0, v.length() - 1);
        }        
    }   
    return null;
}
 
Example 9
static SortedMap<String, FreqValue> getCut(SortedMap<String, FreqValue> coll, String[] arr, String pattern) {
	int startpos = Arrays.binarySearch(arr, pattern);
	if (startpos == -1 || (startpos < 0 && !pattern.startsWith(arr[-startpos - 2]))) return null;
	if (startpos < 0) {
		startpos = -startpos - 2;
	}
	
	int endpos = Arrays.binarySearch(arr, startpos, arr.length, pattern + Character.MAX_VALUE);
	assert(endpos < 0);
	endpos = -endpos - 1;
	return endpos == arr.length ?
			coll.tailMap(arr[startpos]) :
				coll.subMap(arr[startpos], arr[endpos]);
}
 
Example 10
Source Project: tlaplus   File: ToolboxCompletionProcessor.java    License: MIT License 5 votes vote down vote up
public static SortedMap<String, List<CompletionProposalTemplate>> filterPrefix(SortedMap<String, List<CompletionProposalTemplate>> baseMap, String prefix) {
	if (prefix.length() > 0) {
		final char nextLetter = (char) (prefix.charAt(prefix.length() - 1) + 1);
			final String end = prefix.substring(0, prefix.length() - 1) + nextLetter;
		return baseMap.subMap(prefix, end);
	}
	return baseMap;
}
 
Example 11
Source Project: j2objc   File: TreeMapTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
 */
public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
    // Test for method java.util.SortedMap
    // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
    SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
            .toString());
    assertEquals("subMap is of incorrect size", 9, subMap.size());
    for (int counter = 100; counter < 109; counter++) {
        assertTrue("SubMap contains incorrect elements", subMap.get(
                objArray[counter].toString()).equals(objArray[counter]));
    }

    try {
        tm.subMap(objArray[9].toString(), objArray[1].toString());
        fail("end key less than start key should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    // Regression for Harmony-1161
    TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
            new MockComparatorNullTolerable());
    treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
    treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
    SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
            "key1"); //$NON-NLS-1$
    assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$

    // Regression test for typo in lastKey method
    SortedMap<String, String> map = new TreeMap<String, String>();
    map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
    map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
    map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
    assertEquals("3", map.lastKey());
    SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
    assertEquals("2", sub.lastKey()); //$NON-NLS-1$

    // NOTE: The contract of this method allows us to throw either
    // an NPE or a class cast exception.
    TreeMap t = new TreeMap();
    try {
        t.subMap(null, new Object());
        fail("Should throw a ClassCastException");
    } catch (ClassCastException cce) {
        // expected
    } catch (NullPointerException npe) {
        // expected
    }
}