Example usage for java.util LinkedList offer

List of usage examples for java.util LinkedList offer

Introduction

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

Prototype

public boolean offer(E e) 

Source Link

Document

Adds the specified element as the tail (last element) of this list.

Usage

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // offer a new element
    list.offer("Element");

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:de.undercouch.citeproc.helper.Levenshtein.java

/**
 * Searches the given collection of strings and returns a collection of at
 * most <code>n</code> strings that have the lowest Levenshtein distance
 * to a given string <code>t</code>. The returned collection will be
 * sorted according to the distance with the string with the lowest
 * distance at the first position./*from w  w w . j  a va2s . co  m*/
 * @param <T> the type of the strings in the given collection
 * @param ss the collection to search
 * @param t the string to compare to
 * @param n the maximum number of strings to return
 * @param threshold a threshold for individual item distances. Only items
 * with a distance below this threshold will be included in the result.
 * @return the strings with the lowest Levenshtein distance
 */
public static <T extends CharSequence> Collection<T> findMinimum(Collection<T> ss, CharSequence t, int n,
        int threshold) {
    LinkedList<Item<T>> result = new LinkedList<Item<T>>();
    for (T s : ss) {
        int d = StringUtils.getLevenshteinDistance(s, t);
        if (d < threshold) {
            result.offer(new Item<T>(s, d));

            if (result.size() > n + 10) {
                //resort, but not too often
                Collections.sort(result);
                while (result.size() > n)
                    result.removeLast();
            }
        }
    }

    Collections.sort(result);
    while (result.size() > n)
        result.removeLast();

    List<T> arr = new ArrayList<T>(n);
    for (Item<T> i : result) {
        arr.add(i.str);
    }
    return arr;
}

From source file:de._13ducks.cor.game.server.movement.SectorPathfinder.java

/**
 * Der Start und Zielknoten sind von weit mehr als nur den Knoten ihres Polygons erreichbar.
 * Dies muss bereits whrend der Basic-Berechnung beachtet werden, das kann die Pfadoptimierung nachtrglich nichtmehr leisten.
 * Also alle Nodes suchen, die ohne Hinderniss direkt erreichbar sind
 * @param from alle vollstndig im Mesh liegenden Kanten von hier zu Nachbarknoten suchen
 * @param basicPolygon Der Polygon, in dem der Node drinliegt.
 * @return alle direkt erreichbaren Knoten (natrlich sind die des basicPolygons auch dabei)
 *//*from www.j av  a 2 s. co m*/
private static Node[] computeDirectReachable(Node from, FreePolygon basicPolygon) {
    // Das ist eine modifizierte Breitensuche:
    LinkedList<FreePolygon> open = new LinkedList<FreePolygon>(); // Queue fr zu untersuchende Polygone
    LinkedHashSet<FreePolygon> openContains = new LinkedHashSet<FreePolygon>(); // Welche Elemente die open enthlt (schnellerer Test)
    LinkedHashSet<FreePolygon> closed = new LinkedHashSet<FreePolygon>();
    LinkedHashSet<Node> testedNodes = new LinkedHashSet<Node>();
    LinkedList<Node> result = new LinkedList<Node>();
    open.offer(basicPolygon); // Start-Polygon
    openContains.add(basicPolygon);

    while (!open.isEmpty()) {
        // Diesen hier bearbeiten wir jetzt
        FreePolygon poly = open.poll();
        openContains.remove(poly);
        closed.add(poly);

        boolean containsreachableNodes = false;
        // Alle Nodes dieses Knotens untersuchen
        for (Node node : poly.getNodes()) {
            // Schon bekannt?
            if (result.contains(node)) {
                // Bekannt und ok
                containsreachableNodes = true;
            } else {
                if (testedNodes.contains(node)) {
                    // Der geht nicht
                } else {
                    // Testen!
                    FreePolygon currentPoly = basicPolygon;
                    // Testweise Kante zwischen from und node erstellen
                    Edge edge = new Edge(from, node);
                    // Im Folgenden wird untersucht, ob der neue Weg "edge" passierbar ist.
                    // Damit wir beim Dreieckwechsel nicht wieder zurck gehen:
                    Node lastNode = null;

                    boolean routeAllowed = true;

                    // Jetzt so lange weiter laufen, bis wir im Ziel-Polygon sind
                    while (!node.getPolygons().contains(currentPoly)) {
                        // Untersuchen, ob es eine Seite des currentPolygons gibt, die sich mit der alternativRoute schneidet
                        List<Edge> edges = currentPoly.calcEdges();
                        Edge intersecting = null;
                        SimplePosition intersection = null;
                        for (Edge testedge : edges) {
                            // Gibts da einen Schnitt?
                            intersection = edge.intersectionWithEndsNotAllowed(testedge);
                            if (intersection != null && !intersection.equals(lastNode)) {
                                intersecting = testedge;
                                break;
                            }
                        }
                        // Kandidat fr den nchsten Polygon
                        FreePolygon nextPoly = null;
                        // Kante gefunden
                        if (intersecting != null) {
                            // Von dieser Kante die Enden suchen
                            nextPoly = getOtherPoly(intersecting.getStart(), intersecting.getEnd(),
                                    currentPoly);
                        }
                        if (intersecting != null && nextPoly != null) {
                            // Wir haben einen Schnittpunkt und eine Kante gefunden, sind jetzt also in einem neuen Polygon
                            // Extra Node bentigt
                            Node extraNode = intersection.toNode();

                            extraNode.addPolygon(nextPoly);
                            extraNode.addPolygon(currentPoly);
                            lastNode = extraNode;
                            currentPoly = nextPoly;
                            // Der nchste Schleifendurchlauf wird den nchsten Polygon untersuchen
                        } else {
                            // Es gab leider keinen betretbaren Polygon hier.
                            // Das bedeutet, dass wir die Suche abbrechen knnen, es gibt hier keinen direkten Weg
                            routeAllowed = false;
                            break;
                        }

                    }

                    // Wenn der neue Weg gltig war, einbauen. Sonst weiter mit dem nchsten Knoten
                    if (routeAllowed) {
                        // In die erlaubt-Liste:
                        result.add(node);
                        testedNodes.add(node);
                        containsreachableNodes = true;
                    } else {
                        testedNodes.add(node);
                    }
                }
            }
        }

        // Nur weiter in die Tiefe gehen, wenn mindestens einer erreichbar war
        if (containsreachableNodes) {
            // Alle Nachbarn untersuchen:
            for (FreePolygon n : poly.getNeighbors()) {
                // Schon bekannt/bearbeitet?
                if (!openContains.contains(n) && !closed.contains(n)) {
                    // Nein, also auch zur Bearbeitung vorsehen
                    open.add(n);
                    openContains.add(n);
                }
            }
        }
    }
    return result.toArray(new Node[0]);
}

From source file:org.apache.axis2.jaxws.util.WSDLExtensionUtils.java

/**
 * This method will search for all wsdl extensibility elements marked as required=true in wsdl:bindings
 * As per the wsdl 2.2 specification section 2.5 here is how a wsdl:binding is defined:
 * <wsdl:definitions .... >/*  www . j a  va  2s  . c o  m*/
 *      <wsdl:binding name="nmtoken" type="qname"> *
 *       <-- extensibility element (1) --> *
 *       <wsdl:operation name="nmtoken"> *
 *          <-- extensibility element (2) --> *
 *          <wsdl:input name="nmtoken"? > ?
 *              <-- extensibility element (3) --> 
 *          </wsdl:input>
 *          <wsdl:output name="nmtoken"? > ?
 *              <-- extensibility element (4) --> *
 *          </wsdl:output>
 *          <wsdl:fault name="nmtoken"> *
 *              <-- extensibility element (5) --> *
 *          </wsdl:fault>
 *       </wsdl:operation>
 *   </wsdl:binding>
 * </wsdl:definitions>
 * we will look for wsdl extensions in binding root, wsdl:operation, wsdl:input, wsdl:output and wsdl:fault.
 * If the extensibility element is defines outside of these sections it will not be picked up by this method.
 * 
 * @param wsdlBinding - WSDLBinding Object read from WSDL Definition.
 * @param set - Set that will be filled with list of required=true extension elements.
 * @return
 */
public static void search(WSDLElement element, Set<WSDLValidatorElement> set, List<QName> unusedExtensions) {
    if (log.isDebugEnabled()) {
        log.debug("Start Searching for WSDLExtensions");
    }
    if (element == null) {
        return;
    }
    //This search method uses a simple BFS technique to search for Extension elements in WSDLBindings.
    //I will Queue all available WSDLElements starting in wsdl:binding and traverse them looking for 
    //extensions. Queue will be empty when I have processed everything.
    //NOTE:Binding, Operation, OperationInput, OperationOutput and OperationFault are all WSDLElements.
    LinkedList<WSDLElement> queue = new LinkedList<WSDLElement>();
    queue.offer(element);

    while (!queue.isEmpty()) {
        WSDLElement wsdlElement = queue.remove();
        //WSDLElement in Queue could be wsdl Binding, BindingOperations, Input, Output or Fault
        //Find Extensibility Elements in wsdlElement.
        processWSDLElement(wsdlElement, set, unusedExtensions);
        //check if we are dealing with wsdlBinding;
        //store all BindingOpeations from wsdlBindings
        if (wsdlElement instanceof Binding) {
            //lets get all operations and add to queue
            //TODO: WSDLDef API's don't use generics, hence we use Iterator below and type cast.
            List operations = ((Binding) wsdlElement).getBindingOperations();
            Iterator iter = operations.iterator();
            while (iter.hasNext()) {
                BindingOperation op = (BindingOperation) iter.next();
                queue.offer(op);
            }
        }
        //check if we are dealing with Bindingoperations
        //Store all input, output and faults.
        if (wsdlElement instanceof BindingOperation) {
            BindingInput bi = ((BindingOperation) wsdlElement).getBindingInput();
            queue.offer(bi);
            BindingOutput bo = ((BindingOperation) wsdlElement).getBindingOutput();
            queue.offer(bo);
            Map map = ((BindingOperation) wsdlElement).getBindingFaults();
            Collection c = map.values();
            Iterator iter = c.iterator();
            while (iter.hasNext()) {
                Object o = iter.next();
                if (o instanceof BindingFault) {
                    BindingFault bf = (BindingFault) o;
                    queue.offer(bf);
                }
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("End Searching for WSDLExtensions");
    }
}

From source file:org.broadinstitute.gatk.utils.io.IOUtils.java

/**
 * Returns the last lines of the file./*from   w w w .j  a  v a 2  s  .c om*/
 * NOTE: This is only safe to run on smaller files!
 *
 * @param file  File to read.
 * @param count Maximum number of lines to return.
 * @return The last count lines from file.
 * @throws IOException When unable to read the file.
 */
public static List<String> tail(File file, int count) throws IOException {
    LinkedList<String> tailLines = new LinkedList<String>();
    FileReader reader = new FileReader(file);
    try {
        LineIterator iterator = org.apache.commons.io.IOUtils.lineIterator(reader);
        int lineCount = 0;
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            lineCount++;
            if (lineCount > count)
                tailLines.removeFirst();
            tailLines.offer(line);
        }
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(reader);
    }
    return tailLines;
}