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

The following examples show how to use java.util.SortedMap#headMap() . 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: SortedJobSet.java    From swift-k with Apache License 2.0 6 votes vote down vote up
/**
   Remove and return largest job with a walltime smaller than the
   given walltime and less than or equal to the given cpus
   Could be cleaned up using Java 1.6 functionality
 */
public synchronized Job removeOne(TimeInterval walltime, int cpus) {
    Job result = null;
    SortedMap<TimeInterval, LinkedList<Job>> smaller = sm.headMap(walltime);

    while (! smaller.isEmpty()) {
        TimeInterval key = smaller.lastKey();
        List<Job> jobs = smaller.get(key);
        result = removeOne(key, jobs, cpus);
        if (result != null) {
            jsize -= metric.getSize(result);
            if (--size == 0) {
                jsize = 0;
            }
            return result;
        }
        smaller = smaller.headMap(key);
    }
    return null;
}
 
Example 2
Source File: DualTreeBidiMap.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
public Object previousKey(Object key) {
    if (isEmpty()) {
        return null;
    }
    if (maps[0] instanceof OrderedMap) {
        return ((OrderedMap) maps[0]).previousKey(key);
    }
    SortedMap sm = (SortedMap) maps[0];
    SortedMap hm = sm.headMap(key);
    if (hm.isEmpty()) {
        return null;
    }
    return hm.lastKey();
}
 
Example 3
Source File: PendingTxCacheV1.java    From aion with MIT License 5 votes vote down vote up
/**
 * @implNote remove the cached transactions base on the account nonce updated by the block import.
 * @param nonceMap The account with the latest nonce.
 * @return The transaction has been removed in the pending tx cache.
 */
public List<AionTransaction> removeSealedTransactions(Map<AionAddress, BigInteger> nonceMap) {
    Objects.requireNonNull(nonceMap);
    Objects.requireNonNull(nonceMap.entrySet());

    lock.lock();
    try {
        List<AionTransaction> txList = new ArrayList<>();
        for (Entry<AionAddress, BigInteger> e : nonceMap.entrySet()) {
            AionAddress address = e.getKey();
            SortedMap<BigInteger, AionTransaction> accountCachedTx = cacheTxMap.get(address);
            if (accountCachedTx != null) {
                BigInteger nonce = e.getValue();
                Map<BigInteger, AionTransaction> flushedTxMap = accountCachedTx.headMap(nonce);
                if (!flushedTxMap.isEmpty()) {
                    for (AionTransaction tx : flushedTxMap.values()) {
                        removeTxInTimeoutMap(tx);
                        addTransactionToRemovedTransactionForPoolBackup(tx);
                    }

                    txList.addAll(flushedTxMap.values());
                }

                cacheTxMap.get(address).headMap(nonce).clear();
                if (cacheTxMap.get(address).isEmpty()) {
                    cacheTxMap.remove(address);
                }
            }
        }

        // Update the timeout cached Tx
        txList.addAll(flushTimeoutTx(false));
        LOG.info("cacheTx.flush cacheTx# {}", cacheTxSize());

        return txList;
    } finally {
        lock.unlock();
    }
}
 
Example 4
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 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: 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 7
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 8
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 9
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 10
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 11
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 12
Source File: FinallyDuplicatesInfoFactory.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private int getOffset(SortedMap<Integer, Integer> duplicates, int i) {
    SortedMap<Integer, Integer> headMap = duplicates.headMap(i + 1);
    if (headMap.isEmpty()) {
        return -1;
    }
    int end = headMap.get(headMap.lastKey());
    if (end <= i) {
        return -1;
    }
    return getInstructionNumber(positions, i) - getInstructionNumber(positions, headMap.lastKey());
}
 
Example 13
Source File: Filter.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
static private long getAppropriateSeq(SortedMap<Long, AppVersion> timeStamps, long when, boolean roundToLaterVersion) {
    if (roundToLaterVersion) {
        SortedMap<Long, AppVersion> geq = timeStamps.tailMap(when);
        if (geq.isEmpty()) {
            return Long.MAX_VALUE;
        }
        return geq.get(geq.firstKey()).getSequenceNumber();
    } else {
        SortedMap<Long, AppVersion> leq = timeStamps.headMap(when);
        if (leq.isEmpty()) {
            return Long.MIN_VALUE;
        }
        return leq.get(leq.lastKey()).getSequenceNumber();
    }
}
 
Example 14
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 15
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 16
Source File: AMLBlockProcessor.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
private static void insertIncludeBlocks(AMLTestCase block, List<AMLTestCase> blocks, Set<String> invalidBlockReferences, AlertCollector alertCollector, IActionManager actionManager,
        Map<String, SortedMap<Long, String>> definedServiceNames) {
    List<AMLAction> actions = block.getActions();
    int actionsSize = actions.size();

    for(int i = 0; i < actionsSize; i++) {
        AMLAction action = actions.get(i);

        if(JavaStatement.value(action.getActionURI()) == JavaStatement.INCLUDE_BLOCK) {
            String blockRef = action.getTemplate();

            if(invalidBlockReferences.contains(blockRef)) {
                actions.remove(i--);
                actionsSize--;

                continue;
            }

            AMLTestCase includeBlock = getBlockByReference(blockRef, blocks);

            if(includeBlock == null) {
                actions.remove(i--);
                actionsSize--;

                alertCollector.add(new Alert(action.getLine(), action.getUID(), action.getReference(), Column.Template.getName(), "Reference to unknown block: " + blockRef));

                continue;
            }

            action.setActionURI(AMLLangConst.INIT_BLOCK_PARAMETERS_MAP_URI);
            action.setActionInfo(actionManager.getActionInfo(AMLLangConst.INIT_BLOCK_PARAMETERS_MAP_URI));

            String actionRef = action.getReference();
            List<AMLAction> blockActions = includeBlock.clone().getActions();

            for(AMLAction blockAction : blockActions) {
                blockAction.setIncludeBlockReference(actionRef);

                if(!block.isAddToReport()) {
                    blockAction.setAddToReport(false);
                }

                if (blockAction.hasServiceName()) {
                    Value serviceName = blockAction.getServiceName();
                    SortedMap<Long, String> availableServiceNames = definedServiceNames.get(serviceName.getValue());

                    if (availableServiceNames != null) {
                        availableServiceNames = availableServiceNames.headMap(action.getLine());

                        if (!availableServiceNames.isEmpty()) {
                            String name = availableServiceNames.get(availableServiceNames.lastKey());
                            serviceName.setValue(name);
                            serviceName.setReference(AMLLangUtil.isExpression(name));
                        }
                    }
                }
            }

            AMLAction refAction = action.clone();

            refAction.setActionURI(AMLLangConst.INIT_BLOCK_RESULTS_MAP_URI);
            refAction.setActionInfo(actionManager.getActionInfo(AMLLangConst.INIT_BLOCK_RESULTS_MAP_URI));
            refAction.getParameters().clear();
            refAction.setTemplate(null);
            refAction.setTag(null);

            action.setIncludeBlockReference(actionRef);
            action.setReference(blockRef);
            action.setTemplate(null);

            actions.add(i, refAction);
            actions.addAll(i + 2, blockActions);

            actionsSize += blockActions.size() + 1;
        }
    }
}