Skip to content

OptionalInt model: deterministic of/empty/isPresent/isEmpty/getAsInt - #41

Open
tautschnig wants to merge 1 commit into
masterfrom
model-optionalint
Open

OptionalInt model: deterministic of/empty/isPresent/isEmpty/getAsInt#41
tautschnig wants to merge 1 commit into
masterfrom
model-optionalint

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Minimal executable model of java.util.OptionalInt: a (present, value) pair with deterministic of()/empty() constructors, isPresent()/isEmpty() accessors, and getAsInt() throwing NoSuchElementException on the absent value, per the JDK contract.

Copilot AI review requested due to automatic review settings July 28, 2026 23:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a minimal, executable JBMC model for java.util.OptionalInt to support deterministic construction and basic accessors consistent with the JDK contract.

Changes:

  • Introduces java.util.OptionalInt with empty(), of(int), isPresent(), isEmpty(), and getAsInt() (throwing NoSuchElementException when absent).
  • Adds an orElse(int) convenience method (not mentioned in the PR description).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main/java/java/util/OptionalInt.java
Comment thread src/main/java/java/util/OptionalInt.java Outdated
Comment on lines +1 to +7
package java.util;

/**
* JBMC model of java.util.OptionalInt.
* A simple value container: either empty or holds an int.
*/
public final class OptionalInt {
Comment thread src/main/java/java/util/OptionalInt.java
Minimal executable model of java.util.OptionalInt: a (present, value)
pair with deterministic of()/empty() constructors, isPresent()/isEmpty()
accessors, getAsInt() throwing NoSuchElementException on the absent value
per the JDK contract, and orElse(int).

empty() deliberately allocates per call rather than caching a static
singleton as the JDK internals do: OptionalInt's javadoc explicitly
disclaims singleton-ness of empty() and forbids == comparison, so per-call
allocation is contract-conformant -- and a static object field (the first
in this model library) would become nondeterministic under JBMC's
--nondet-static, making empty().isPresent() spuriously satisfiable.

Carries the standard OpenJDK GPLv2+Classpath header, consistent with the
sibling java.util model files.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants