Java Code Examples for java.util.Deque#element()

The following examples show how to use java.util.Deque#element() . 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: Solution.java    From DailyCodingProblem with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean check(int[] arr) {
    int replace = 0;
    Deque<Integer> stack = new LinkedList<>();

    for (int val : arr) {
        while (!stack.isEmpty() && val < stack.element()) {
            stack.pop();
            replace++;
        }

        if (replace > 1)
            return false;

        stack.push(val);
    }

    return true;
}
 
Example 2
Source File: Profiler.java    From copybara with Apache License 2.0 5 votes vote down vote up
/**
 * Overloaded method for {@link #start(String)}, that allows adding {@code fields} to the context
 * of this task.
 */
public ProfilerTask start(String description, ImmutableMap<String, String> fields) {
  if (stopped || listeners.isEmpty()) {
    return nullProfilerTask;
  }
  Deque<Task> tasks = taskQueue.get();
  Preconditions.checkState(!tasks.isEmpty());
  Task parent = tasks.element();
  Task child = new Task(parent.getDescription() + "/" + description, fields, ticker.read());
  tasks.push(child);
  for (Listener listener : listeners) {
    listener.taskStarted(child);
  }
  return new ProfilerTask(child);
}
 
Example 3
Source File: Profiler.java    From copybara with Apache License 2.0 5 votes vote down vote up
/**
 * Record a simple task metric. The user is in charge of providing its own time.
 */
public void simpleTask(String description, long startNanos, long endNanos) {
  if (stopped || listeners.isEmpty()) {
    return;
  }
  Deque<Task> tasks = taskQueue.get();
  Preconditions.checkState(!tasks.isEmpty());
  Task parent = tasks.element();
  Task child  = new Task(parent.getDescription() + "/" + description, startNanos);
  Task finishedChild = child.finish(endNanos);
  for (Listener listener : listeners) {
    listener.taskStarted(child);
    listener.taskFinished(finishedChild);
  }
}
 
Example 4
Source File: FormHandler.java    From mangooio with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the form parameter from a request
 *
 * @param exchange The Undertow HttpServerExchange
 *
 * @throws IOException
 */
@SuppressWarnings("rawtypes")
protected Form getForm(HttpServerExchange exchange) throws IOException {
    final Form form = Application.getInstance(Form.class);
    if (RequestUtils.isPostPutPatch(exchange)) {
        final Builder builder = FormParserFactory.builder();
        builder.setDefaultCharset(StandardCharsets.UTF_8.name());
        try (final FormDataParser formDataParser = builder.build().createParser(exchange)) {
            if (formDataParser != null) {
                exchange.startBlocking();
                final FormData formData = formDataParser.parseBlocking();
                for (String data : formData) {
                    Deque<FormValue> deque = formData.get(data);
                    if (deque != null) {
                        FormValue formValue = deque.element();
                        if (formValue != null) {
                            if (formValue.isFileItem() && formValue.getFileItem().getFile() != null) {
                                form.addFile(Files.newInputStream(formValue.getFileItem().getFile()));
                            } else {
                                if (data.contains("[]")) {
                                    String key = StringUtils.replace(data, "[]", "");
                                    for (Iterator iterator = deque.iterator(); iterator.hasNext();)  {
                                        form.addValueList(new HttpString(key).toString(), ((FormValue) iterator.next()).getValue());
                                    }
                                } else {
                                    form.addValue(new HttpString(data).toString(), formValue.getValue());
                                }
                            }    
                        }
                    }
                }
            }
        }
        
        form.setSubmitted(true);
    }

    return form;
}
 
Example 5
Source File: LinkedDequeTest.java    From concurrentlinkedhashmap with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "emptyDeque", expectedExceptions = NoSuchElementException.class)
public void element_whenEmpty(Deque<SimpleLinkedValue> deque) {
  deque.element();
}
 
Example 6
Source File: LinkedDequeTest.java    From multiway-pool with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "emptyDeque", expectedExceptions = NoSuchElementException.class)
public void element_whenEmpty(Deque<SimpleLinkedValue> deque) {
  deque.element();
}