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

The following examples show how to use java.util.SortedMap#lastKey() . 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: 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 2
/**
 * @return progress value corresponding to the given {@code pixels} value.
 */
Progress getProgress(int pixels) {
  if (pixels < myMinPx) {
    return new Progress(0, myTask.getDuration());
  }
  SortedMap<Integer, Integer> tailMap = myPixel2progress.tailMap(pixels);
  if (tailMap.isEmpty()) {
    return new Progress(100, myTask.getDuration());
  }
  if (tailMap.firstKey().intValue() == pixels) {
    return new Progress(tailMap.get(pixels), myTask.getDuration());
  }

  SortedMap<Integer, Integer> headMap = myPixel2progress.headMap(pixels);
  int lowerPx = headMap.isEmpty() ? 0 : headMap.lastKey();
  int lowerProgress = headMap.isEmpty() ? 0 : headMap.get(lowerPx);
  int upperPx = tailMap.firstKey();
  int upperProgress = tailMap.get(upperPx);

  float diffProgress = (upperProgress - lowerProgress) * ((float) (pixels - lowerPx) / (float) (upperPx - lowerPx));
  int overallProgress = (int) (lowerProgress + diffProgress);
  return new Progress(overallProgress, myTask.getDuration());
}
 
Example 3
Source Project: codebuff   File: Maps.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public K lastKey() {
  SortedMap<K, V> headMap = sortedMap();
  while (true) {
    // correctly throws NoSuchElementException when filtered map is empty.
    K key = headMap.lastKey();
    if (apply(key, unfiltered.get(key))) {
      return key;
    }
    headMap = sortedMap().headMap(key);
  }
}
 
Example 4
private Double calcScoreDifference(SortedMap<Double, SortedSet<String>> scoredCats)
{
  if (scoredCats.size() < 2)
  {
    return null;
  }

  Double score1 = scoredCats.lastKey();
  Double score2 = scoredCats.headMap(score1).lastKey();

  return score1 - score2;
}
 
Example 5
Source Project: gemfirexd-oss   File: Graph.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add an edge to this graph.
 * @param timestamp
 * @param edgeName
 * @param source
 * @param dest
 * @param isFromPattern 
 */
public void addEdge(long timestamp, String edgeName, String state, String source,
    String dest, boolean isFromPattern) {

  Vertex destVertex = new Vertex(this, dest, state, timestamp);
  SortedMap<Long, Vertex> map  = this.indexedVertices.get(dest);
  if(map == null) {
    map = new TreeMap<Long, Vertex>();
    this.indexedVertices.put(dest, map);
  }
  
  //If this edge is being added by a pattern event, only
  //add the edge if the destination changes state as a result
  //of this edge. This cuts down on noise in the graph.
  if(isFromPattern) {
    SortedMap<Long, Vertex> headMap = map.headMap(timestamp);
    if(headMap != null && !headMap.isEmpty()) {
      Long previousKey = headMap.lastKey();
      Vertex previousVertex = headMap.get(previousKey);
      if(previousVertex.getState().equals(state)) {
        return;
      }
    } else {
      //Super hack here. Don't add a transition from the non existent state to
      //the destroyed state in a lifeline.
      if(state.equals("destroyed")) {
        return;
      }
    }
  }
  map.put(timestamp, destVertex);
  
  edges.add(new Edge(this, timestamp, edgeName, source, destVertex));
}
 
Example 6
Source Project: ghidra   File: Locator.java    License: Apache License 2.0 5 votes vote down vote up
public Location getLocation(int expandedLineNo) {
	SortedMap<Integer, Location> headMap = map.headMap(expandedLineNo + 1);
	Integer key;
	Location location;
	Location correctLocation;
	if (headMap.size() == 0) {
		return null;
	}
	key = headMap.lastKey();
	location = headMap.get(key);
	int actualLineNumber = expandedLineNo - key + location.lineno;
	correctLocation = new Location(location.filename, actualLineNumber);
	return correctLocation;
}
 
Example 7
Source Project: scava   File: ST.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the largest key in the symbol table less than or equal to <tt>key</tt>.
 * @return the largest key in the symbol table less than or equal to <tt>key</tt>
 * @param key the key
 * @throws NoSuchElementException if the symbol table is empty
 * @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
 */
public Key floor(Key key) {
    if (key == null) throw new NullPointerException("called floor() with null key");
    // headMap does not include key if present (!)
    if (st.containsKey(key)) return key;
    SortedMap<Key, Value> head = st.headMap(key);
    if (head.isEmpty()) throw new NoSuchElementException();
    return head.lastKey();
}
 
Example 8
Source Project: gemfirexd-oss   File: Edge.java    License: Apache License 2.0 5 votes vote down vote up
public Vertex getSource() {
  SortedMap<Long, Vertex> sourceMap = graph.getIndexedVertices().get(source);
  if(sourceMap == null) {
    return null;
  }
  SortedMap<Long, Vertex> headMap = sourceMap.headMap(dest.getTimestamp() + 1);
  if(headMap.isEmpty()) {
    return null;
  }
  Long closestTimestamp = headMap.lastKey();
  return headMap.get(closestTimestamp);
}
 
Example 9
private int purge(final RollingFileManager manager) {
    final SortedMap<Integer, Path> eligibleFiles = getEligibleFiles(manager);
    LOGGER.debug("Found {} eligible files, max is  {}", eligibleFiles.size(), maxFiles);
    while (eligibleFiles.size() >= maxFiles) {
        try {
            final Integer key = eligibleFiles.firstKey();
            Files.delete(eligibleFiles.get(key));
            eligibleFiles.remove(key);
        } catch (final IOException ioe) {
            LOGGER.error("Unable to delete {}", eligibleFiles.firstKey(), ioe);
            break;
        }
    }
    return eligibleFiles.size() > 0 ? eligibleFiles.lastKey() : 1;
}
 
Example 10
Source Project: netbeans   File: InputGestureTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testReadALogAndTestInputGestures() throws Exception {
    InputStream is = getClass().getResourceAsStream("NB1216449736.0");
    SortedMap<Integer,InputGesture> expectedGestures = new TreeMap<Integer,InputGesture>();
    expectedGestures.put(35, InputGesture.MENU);
    expectedGestures.put(59, InputGesture.KEYBOARD);
    expectedGestures.put(66, InputGesture.MENU);
    expectedGestures.put(80, InputGesture.MENU);
    expectedGestures.put(81, InputGesture.MENU);
    expectedGestures.put(177, InputGesture.KEYBOARD);
    expectedGestures.put(197, InputGesture.KEYBOARD);
    expectedGestures.put(205, InputGesture.MENU);
    TestHandler records = new TestHandler(is);
    for (int cnt = 0;; cnt++) {
        LOG.log(Level.INFO, "Reading {0}th record", cnt);
        LogRecord r = records.read();
        if (r == null) {
            break;
        }
        if (r.getSequenceNumber() > expectedGestures.lastKey()) {
            break;
        }
        LOG.log(Level.INFO, "Read {0}th record, seq {1}", new Object[] { cnt, r.getSequenceNumber() });

        InputGesture g = InputGesture.valueOf(r);
        InputGesture exp = expectedGestures.get((int)r.getSequenceNumber());
        assertEquals(cnt + ": For: " + r.getSequenceNumber() + " txt:\n`"+ r.getMessage() +
            "\nkey: " + r.getResourceBundleName()
            , exp, g);
    }
    is.close();
}
 
Example 11
@RequestMapping(value = "/previousQuestion")
   private String previousQuestion(@ModelAttribute("surveyForm") AnswerForm surveyForm,
    MultiValueMap<String, String> errorMap, HttpServletRequest request) {
Integer questionSeqID = surveyForm.getQuestionSeqID();
String sessionMapID = surveyForm.getSessionMapID();

SessionMap<String, Object> sessionMap = (SessionMap<String, Object>) request.getSession()
	.getAttribute(sessionMapID);
SortedMap<Integer, AnswerDTO> surveyItemMap = getQuestionList(sessionMap);

if (!errorMap.isEmpty()) {
    request.setAttribute("errorMap", errorMap);
    return "pages/learning/learning";
}

SortedMap<Integer, AnswerDTO> subMap = surveyItemMap.headMap(questionSeqID);
if (subMap.isEmpty()) {
    questionSeqID = surveyItemMap.firstKey();
} else {
    questionSeqID = subMap.lastKey();
}

// get current question index of total questions
int currIdx = new ArrayList<>(surveyItemMap.keySet()).indexOf(questionSeqID) + 1;
surveyForm.setCurrentIdx(currIdx);

if (questionSeqID.equals(surveyItemMap.firstKey())) {
    surveyForm.setPosition(SurveyConstants.POSITION_FIRST);
} else {
    surveyForm.setPosition(SurveyConstants.POSITION_INSIDE);
}
surveyForm.setQuestionSeqID(questionSeqID);
request.setAttribute("surveyForm", surveyForm);
return "pages/learning/learning";
   }
 
Example 12
Source Project: jdk1.8-source-analysis   File: EnvHelp.java    License: Apache License 2.0 4 votes vote down vote up
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}
 
Example 13
Source Project: jdk8u-jdk   File: EnvHelp.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}
 
Example 14
Source Project: jdk8u60   File: EnvHelp.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}
 
Example 15
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}
 
Example 16
Source Project: jdk8u_jdk   File: EnvHelp.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}
 
Example 17
Source Project: pinpoint   File: EagerSamplingHandler.java    License: Apache License 2.0 4 votes vote down vote up
private S reduceSampledPoints(SortedMap<Long, S> sampledPointCandidates) {
    Long lastKey = sampledPointCandidates.lastKey();
    return sampledPointCandidates.get(lastKey);
}
 
Example 18
Source Project: openjdk-8   File: EnvHelp.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}
 
Example 19
Source Project: medialibrary   File: LocalMergeAlbum.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ArrayList<MediaItem> getMediaItem(int start, int count) {

    // First find the nearest mark position <= start.
    SortedMap<Integer, int[]> head = mIndex.headMap(start + 1);
    int markPos = head.lastKey();
    int[] subPos = head.get(markPos).clone();
    MediaItem[] slot = new MediaItem[mSources.length];

    int size = mSources.length;

    // fill all slots
    for (int i = 0; i < size; i++) {
        slot[i] = mFetcher[i].getItem(subPos[i]);
    }

    ArrayList<MediaItem> result = new ArrayList<MediaItem>();

    for (int i = markPos; i < start + count; i++) {
        int k = -1;  // k points to the best slot up to now.
        for (int j = 0; j < size; j++) {
            if (slot[j] != null) {
                if (k == -1 || mComparator.compare(slot[j], slot[k]) < 0) {
                    k = j;
                }
            }
        }

        // If we don't have anything, all streams are exhausted.
        if (k == -1) break;

        // Pick the best slot and refill it.
        subPos[k]++;
        if (i >= start) {
            result.add(slot[k]);
        }
        slot[k] = mFetcher[k].getItem(subPos[k]);

        // Periodically leave a mark in the index, so we can come back later.
        if ((i + 1) % PAGE_SIZE == 0) {
            mIndex.put(i + 1, subPos.clone());
        }
    }

    return result;
}
 
Example 20
private static void hideAttributes(SortedMap<String, ?> map) {
    if (map.isEmpty())
        return;

    final SortedSet<String> hiddenStrings;
    final SortedSet<String> hiddenPrefixes;

    String hide = (String) map.get(HIDDEN_ATTRIBUTES);
    if (hide != null) {
        if (hide.startsWith("="))
            hide = hide.substring(1);
        else
            hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
        hiddenStrings = new TreeSet<String>();
        hiddenPrefixes = new TreeSet<String>();
        parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
    } else {
        hide = DEFAULT_HIDDEN_ATTRIBUTES;
        synchronized (defaultHiddenStrings) {
            if (defaultHiddenStrings.isEmpty()) {
                parseHiddenAttributes(hide,
                                      defaultHiddenStrings,
                                      defaultHiddenPrefixes);
            }
            hiddenStrings = defaultHiddenStrings;
            hiddenPrefixes = defaultHiddenPrefixes;
        }
    }

    /* Construct a string that is greater than any key in the map.
       Setting a string-to-match or a prefix-to-match to this string
       guarantees that we will never call next() on the corresponding
       iterator.  */
    String sentinelKey = map.lastKey() + "X";
    Iterator<String> keyIterator = map.keySet().iterator();
    Iterator<String> stringIterator = hiddenStrings.iterator();
    Iterator<String> prefixIterator = hiddenPrefixes.iterator();

    String nextString;
    if (stringIterator.hasNext())
        nextString = stringIterator.next();
    else
        nextString = sentinelKey;
    String nextPrefix;
    if (prefixIterator.hasNext())
        nextPrefix = prefixIterator.next();
    else
        nextPrefix = sentinelKey;

    /* Read each key in sorted order and, if it matches a string
       or prefix, remove it. */
keys:
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();

        /* Continue through string-match values until we find one
           that is either greater than the current key, or equal
           to it.  In the latter case, remove the key.  */
        int cmp = +1;
        while ((cmp = nextString.compareTo(key)) < 0) {
            if (stringIterator.hasNext())
                nextString = stringIterator.next();
            else
                nextString = sentinelKey;
        }
        if (cmp == 0) {
            keyIterator.remove();
            continue keys;
        }

        /* Continue through the prefix values until we find one
           that is either greater than the current key, or a
           prefix of it.  In the latter case, remove the key.  */
        while (nextPrefix.compareTo(key) <= 0) {
            if (key.startsWith(nextPrefix)) {
                keyIterator.remove();
                continue keys;
            }
            if (prefixIterator.hasNext())
                nextPrefix = prefixIterator.next();
            else
                nextPrefix = sentinelKey;
        }
    }
}