Java Code Examples for java.util.Stack#removeElementAt()

The following examples show how to use java.util.Stack#removeElementAt() . 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: RequestRateThrottleFilter.java    From Alpine with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: StackUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(7);

    intStack.removeElementAt(1);

    assertEquals(-1, intStack.search(7));
}
 
Example 3
Source File: PrintClassHierarchy.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
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);
		}
	}
}