Example usage for java.util LinkedList pollLast

List of usage examples for java.util LinkedList pollLast

Introduction

In this page you can find the example usage for java.util LinkedList pollLast.

Prototype

public E pollLast() 

Source Link

Document

Retrieves and removes the last element of this list, or returns null if this list is empty.

Usage

From source file:org.pentaho.repo.controller.RepositoryBrowserController.java

public LinkedList<String> storeRecentSearch(String recentSearch) {
    LinkedList<String> recentSearches = getRecentSearches();
    try {//from  w  w  w.  j a v  a2 s .  c  om
        if (recentSearch == null || recentSearches.contains(recentSearch)) {
            return recentSearches;
        }
        recentSearches.push(recentSearch);
        if (recentSearches.size() > 5) {
            recentSearches.pollLast();
        }

        JSONArray jsonArray = new JSONArray();
        CollectionUtils.addAll(jsonArray, recentSearches.toArray());

        PropsUI props = PropsUI.getInstance();
        String jsonValue = props.getRecentSearches();
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = jsonValue != null ? (JSONObject) jsonParser.parse(jsonValue) : new JSONObject();

        String login = "file_repository_no_login";
        if (Spoon.getInstance().rep.getUserInfo() != null) {
            login = Spoon.getInstance().rep.getUserInfo().getLogin();
        }

        jsonObject.put(login, jsonArray);
        props.setRecentSearches(jsonObject.toJSONString());
    } catch (Exception e) {
        // Log error in console
    }

    return recentSearches;
}

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   w w w .  ja v a2  s .c  o m
            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 av a  2 s.  co 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;
}