Example usage for java.util Queue offer

List of usage examples for java.util Queue offer

Introduction

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

Prototype

boolean offer(E e);

Source Link

Document

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.

Usage

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.offer("First");
    queue.offer("Second");
    queue.offer("Third");
    queue.offer("Fourth");

    System.out.println(queue);/*  w  ww  .j  av  a 2 s .  com*/
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.offer("First");
    queue.offer("Second");
    queue.offer("Third");
    queue.offer("Fourth");

    queue.remove();/*from w w w .j av  a2 s.c  o  m*/

    System.out.println(queue);
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.offer("First");
    queue.offer("Second");
    queue.offer("Third");
    queue.offer("Fourth");

    System.out.println("Size: " + queue.size());

    System.out.println("Queue head using peek   : " + queue.peek());
    System.out.println("Queue head using element: " + queue.element());

    Object data;//from www  .  j  av  a2 s .  c  o m
    while ((data = queue.poll()) != null) {
        System.out.println(data);
    }
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();

    queue.add("A");
    queue.add("B");

    queue.offer("C");
    queue.offer("D");

    System.out.println("remove: " + queue.remove());

    System.out.println("element: " + queue.element());

    System.out.println("poll: " + queue.poll());

    System.out.println("peek: " + queue.peek());
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.add("Java");
    // offer() will work the same as add()
    queue.offer("SQL");
    queue.offer("CSS");
    queue.offer("XML");

    System.out.println("Queue: " + queue);

    // Let's remove elements until the queue is empty
    while (queue.peek() != null) {
        System.out.println("Head  Element: " + queue.peek());
        queue.remove();//from  w w w.  jav a 2  s.  com
        System.out.println("Removed one  element from  Queue");
        System.out.println("Queue: " + queue);
    }
    System.out.println("queue.isEmpty(): " + queue.isEmpty());
    System.out.println("queue.peek(): " + queue.peek());
    System.out.println("queue.poll(): " + queue.poll());
    try {
        String str = queue.element();
        System.out.println("queue.element(): " + str);
        str = queue.remove();
        System.out.println("queue.remove(): " + str);
    } catch (NoSuchElementException e) {
        System.out.println("queue.remove(): Queue is  empty.");
    }
}

From source file:reactor.logback.DurableLogUtility.java

@SuppressWarnings("unchecked")
public static void main(String... args) throws ParseException, JoranException, IOException {
    Parser parser = new BasicParser();
    CommandLine cl = null;//from   w w w. j  a  va 2 s .  com
    try {
        cl = parser.parse(OPTS, args);
    } catch (ParseException e) {
        HelpFormatter help = new HelpFormatter();
        help.printHelp("dlog", OPTS, true);
        System.exit(-1);
    }

    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerContext.reset();

    if (cl.hasOption("config")) {
        // Read Logback configuration
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);

        configurator.doConfigure(cl.getOptionValue("file", "logback.xml"));

        StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
    } else {
        BasicConfigurator.configure(loggerContext);
    }

    Appender appender = null;
    if (cl.hasOption("output")) {
        String outputAppender = cl.getOptionValue("output", "console");
        appender = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).getAppender(outputAppender);
    }

    ChronicleTools.warmup();
    Chronicle chronicle = ChronicleQueueBuilder.indexed(cl.getOptionValue("path")).build();
    ExcerptTailer ex = chronicle.createTailer();

    Level level = Level.valueOf(cl.getOptionValue("level", "TRACE"));

    if (cl.hasOption("head")) {
        int lines = Integer.parseInt(cl.getOptionValue("head", "10"));
        for (int i = 0; i < lines; i++) {
            LoggingEvent evt = readLoggingEvent(ex, loggerContext);
            if (evt.getLevel().isGreaterOrEqual(level)) {
                writeEvent(evt, appender);
            }
        }
    } else if (cl.hasOption("tail")) {
        int lines = Integer.parseInt(cl.getOptionValue("tail", "10"));
        Queue<LoggingEvent> tail = new LinkedBlockingQueue<LoggingEvent>(lines);
        while (ex.nextIndex()) {
            LoggingEvent evt = readLoggingEvent(ex, loggerContext);
            if (!tail.offer(evt)) {
                tail.poll();
                tail.add(evt);
            }
        }
        LoggingEvent evt;
        while (null != (evt = tail.poll())) {
            if (evt.getLevel().isGreaterOrEqual(level)) {
                writeEvent(evt, appender);
            }
        }
    } else if (cl.hasOption("search")) {
        String regex = cl.getOptionValue("search");
        Pattern regexPatt = Pattern.compile(regex);
        while (ex.nextIndex()) {
            LoggingEvent evt = readLoggingEvent(ex, loggerContext);
            if (null != evt && evt.getLevel().isGreaterOrEqual(level)) {
                if (regexPatt.matcher(evt.getFormattedMessage()).matches()) {
                    writeEvent(evt, appender);
                }
            }
        }
    }

    loggerContext.stop();
    chronicle.close();
}

From source file:Main.java

/**
 * Certain types of queues will fail to {@link java.util.Queue#offer(Object)} an item due to many factors
 * depending on the type of queue. <code>offerUntilSuccess</code> will not return until the item has been
 * successfully queued onto the desired queue
 * @param entry item to queue/*w  w  w  .  j a  v  a 2s  .c  o  m*/
 * @param queue queue to add the entry to
 * @param <T>
 */
public static <T> void offerUntilSuccess(T entry, Queue<T> queue) {
    boolean success;
    do {
        success = queue.offer(entry);
        Thread.yield();
    } while (!success);
}

From source file:net.darkmist.clf.Util.java

public static <T> Queue<T> addTo(Queue<T> q, T[] toAdd, int off, int len) {
    int end = Math.min(len + off, toAdd.length);

    for (int c = off; c < end; c++)
        q.offer(toAdd[c]);
    return q;/*from www  .  j a  v a2 s . c om*/
}

From source file:net.darkmist.clf.Util.java

public static <U, T> Queue<T> addTo(Queue<T> q, Converter<U, T> converter, U[] toAdd, int off, int len) {
    int end = Math.min(len + off, toAdd.length);

    for (int c = off; c < end; c++)
        q.offer(converter.convert(toAdd[c]));
    return q;/*from   w w w .java 2  s.  c  o m*/
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

public static String parseMainClassFromJCL(String content) throws IOException {
    String mainClass = null;/*from w w w. j  a va2  s .  c om*/
    BufferedReader reader = new BufferedReader(new StringReader(content));
    String line;
    while ((line = reader.readLine()) != null) {
        if (line.length() > 0) {
            boolean hasJAL = false;
            Queue<String> myQueue = new LinkedList<String>();
            String[] tokens = line.split("\\s"); //$NON-NLS-1$
            for (String token : tokens) {
                if (hasJAL && token.trim().length() > 0) {
                    myQueue.offer(token.trim());
                }
                if ("java".equals(token)) { //$NON-NLS-1$
                    hasJAL = true;
                }
            }
            if (hasJAL) {
                String str;
                boolean needConsume = false;
                while ((str = myQueue.poll()) != null) {
                    if (!str.startsWith("-")) { //$NON-NLS-1$
                        if (needConsume) {
                            needConsume = false;// consume
                        } else {
                            mainClass = str;
                            break;
                        }
                    }
                    if (str.startsWith("-")) { //$NON-NLS-1$
                        str = str.substring(1);
                        if (str.startsWith("-")) { //$NON-NLS-1$
                            str = str.substring(1);
                        }
                        // FIXME is there any more?
                        if ("cp".equals(str) || "classpath".equals(str) || "jar".equals(str)) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                            needConsume = true;
                        }
                    }
                }
            }
        }
    }
    return mainClass;
}