List of usage examples for java.util Deque peekLast
E peekLast();
From source file:org.springframework.integration.support.management.ExponentialMovingAverageRateTests.java
@Test @SuppressWarnings("unchecked") public void testGetTimeSinceLastMeasurement() throws Exception { long sleepTime = 20L; // fill history with the same value. long now = System.nanoTime() - 2 * sleepTime * 1000000; for (int i = 0; i < TestUtils.getPropertyValue(history, "retention", Integer.class); i++) { history.increment(now);/*from w w w . j a va 2 s .c o m*/ } final Deque<Long> times = TestUtils.getPropertyValue(history, "times", Deque.class); assertEquals(Long.valueOf(now), times.peekFirst()); assertEquals(Long.valueOf(now), times.peekLast()); //increment just so we'll have a different value between first and last history.increment(System.nanoTime() - sleepTime * 1000000); assertNotEquals(times.peekFirst(), times.peekLast()); /* * We've called Thread.sleep twice with the same value in quick * succession. If timeSinceLastSend is pulling off the correct end of * the queue, then we should be closer to the sleep time than we are to * 2 x sleepTime, but we should definitely be greater than the sleep * time. */ double timeSinceLastMeasurement = history.getTimeSinceLastMeasurement(); assertTrue(timeSinceLastMeasurement > sleepTime); assertTrue(timeSinceLastMeasurement <= (1.5 * sleepTime)); }
From source file:uniol.apt.adt.automaton.FiniteAutomatonUtility.java
static private List<String> findAcceptedWord(DeterministicFiniteAutomaton dfa) { Set<DFAState> statesSeen = new HashSet<>(); LinkedList<String> word = new LinkedList<>(); Deque<Pair<DFAState, Iterator<Symbol>>> trace = new LinkedList<>(); DFAState initial = dfa.getInitialState(); trace.add(new Pair<>(initial, initial.getDefinedSymbols().iterator())); while (!trace.isEmpty()) { InterrupterRegistry.throwIfInterruptRequestedForCurrentThread(); Pair<DFAState, Iterator<Symbol>> pair = trace.peekLast(); if (!pair.getSecond().hasNext()) { trace.removeLast();/*from www . ja v a2 s . com*/ word.pollLast(); } else { Symbol symbol = pair.getSecond().next(); DFAState nextState = pair.getFirst().getFollowingState(symbol); // Only follow this state if we haven't followed it yet before if (statesSeen.add(nextState)) { trace.add(new Pair<>(nextState, nextState.getDefinedSymbols().iterator())); word.add(symbol.getEvent()); if (nextState.isFinalState()) return word; } } } return null; }
From source file:uniol.apt.adt.automaton.FiniteAutomatonUtility.java
/** * Find a word whose prefixes (including the word) conform to a given predicate and which itself also conforms * to a second predicate.//w w w. j a v a 2 s .c o m * * This method uses a depth-first search. A breath-first search would use more memory. * * @param a The automaton whose accepted words should get checked. * @param prefixPredicate The predicate to check the prefixes. * @param wordPredicate The predicate to check the words. * @return A word which conforms to the predicates. */ static public List<String> findPredicateWord(FiniteAutomaton a, Predicate<List<String>> prefixPredicate, Predicate<List<String>> wordPredicate) { MinimalDeterministicFiniteAutomaton dfa = minimizeInternal(a); Deque<Pair<DFAState, Iterator<Symbol>>> trace = new ArrayDeque<>(); LinkedList<String> word = new LinkedList<>(); DFAState initial = dfa.getInitialState(); DFAState sinkState = findSinkState(dfa); trace.add(new Pair<>(initial, initial.getDefinedSymbols().iterator())); while (!trace.isEmpty()) { Pair<DFAState, Iterator<Symbol>> pair = trace.peekLast(); if (!pair.getSecond().hasNext()) { trace.removeLast(); word.pollLast(); } else { Symbol symbol = pair.getSecond().next(); DFAState nextState = pair.getFirst().getFollowingState(symbol); if (!nextState.equals(sinkState)) { word.add(symbol.getEvent()); List<String> roWord = ListUtils.unmodifiableList(word); if (prefixPredicate.evaluate(roWord)) { trace.addLast(new Pair<>(nextState, nextState.getDefinedSymbols().iterator())); if (nextState.isFinalState() && wordPredicate.evaluate(roWord)) return word; } else { word.removeLast(); } } } } return null; }