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

The following examples show how to use java.util.SortedMap#lastKey() . 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 File: ChangeTaskProgressRuler.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @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 2
Source File: TreeMapTest.java    From j2objc with 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 File: AbstractMachineLearningIntentMatcher.java    From Mutters with Apache License 2.0 5 votes vote down vote up
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 4
Source File: Maps.java    From codebuff with 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 5
Source File: Graph.java    From gemfirexd-oss with 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 File: Locator.java    From ghidra with 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 File: ST.java    From scava with 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 File: Edge.java    From gemfirexd-oss with 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
Source File: LearningController.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 10
Source File: DirectWriteRolloverStrategy.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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 11
Source File: InputGestureTest.java    From netbeans with 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 12
Source File: EnvHelp.java    From openjdk-jdk9 with 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 13
Source File: LocalMergeAlbum.java    From medialibrary with 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 14
Source File: EnvHelp.java    From openjdk-8 with 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
Source File: EagerSamplingHandler.java    From pinpoint with 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 16
Source File: EnvHelp.java    From jdk8u_jdk with 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 File: EnvHelp.java    From openjdk-8-source with 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 18
Source File: EnvHelp.java    From jdk8u60 with 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 File: EnvHelp.java    From jdk8u-jdk with 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 20
Source File: EnvHelp.java    From jdk1.8-source-analysis with 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;
        }
    }
}