下面列出了java.util.Stack#removeElementAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Determines if the request rate is below or has exceeded the the maximum requests per second
* for the given time period. If exceeded, a HTTP status code of 429 (too many requests) will
* be send and no further processing of the request will be done. If the request has not exceeded
* the limit, the request will continue on as normal.
*
* @param request a ServletRequest
* @param response a ServletResponse
* @param chain a FilterChain
* @throws IOException a IOException
* @throws ServletException a ServletException
*/
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
final HttpSession session = httpRequest.getSession(true);
synchronized (session.getId().intern()) {
Stack<Date> times = HttpUtil.getSessionAttribute(session, "times");
if (times == null) {
times = new Stack<>();
times.push(new Date(0));
session.setAttribute("times", times);
}
times.push(new Date());
if (times.size() >= maximumRequestsPerPeriod) {
times.removeElementAt(0);
}
final Date newest = times.get(times.size() - 1);
final Date oldest = times.get(0);
final long elapsed = newest.getTime() - oldest.getTime();
if (elapsed < timePeriodSeconds * 1000) {
httpResponse.sendError(429);
return;
}
}
chain.doFilter(request, response);
}
@Test
public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
Stack<Integer> intStack = new Stack<>();
intStack.push(5);
intStack.push(7);
intStack.removeElementAt(1);
assertEquals(-1, intStack.search(7));
}
private void printHierarchy(final String clazzName, final Stack<Boolean> moreClassesInHierarchy) {
if (!moreClassesInHierarchy.empty()) {
for (final Boolean hasColumn : moreClassesInHierarchy.subList(0, moreClassesInHierarchy.size() - 1)) {
System.out.print(hasColumn.booleanValue() ? PADDING_WITH_COLUMN : PADDING);
}
}
if (!moreClassesInHierarchy.empty()) {
System.out.print(PADDING_WITH_ENTRY);
}
System.out.println(clazzName);
if (subClazzEntries.containsKey(clazzName)) {
final List<String> list = subClazzEntries.get(clazzName);
for (int i = 0; i < list.size(); i++) {
// if there is another class that comes beneath the next class,
// flag this level
moreClassesInHierarchy.push(new Boolean(i < list.size() - 1));
printHierarchy(list.get(i), moreClassesInHierarchy);
moreClassesInHierarchy.removeElementAt(moreClassesInHierarchy.size() - 1);
}
}
}