Skip to main content

Lang Package

The org.apache.juneau.commons.lang package provides language utilities including value wrappers, version management, string formatting, state enums, and other common language constructs.

Key Classes

Value

A generic mutable value wrapper.

// Basic usage
Value<String> name = Value.of("John");
name.set("Jane");
String result = name.get(); // Returns "Jane"

// Use in lambda (effectively final variable)
Value<String> result = Value.empty();
list.forEach(x -> {
if (x.matches(criteria)) {
result.set(x);
}
});

// With default values
Value<String> optional = Value.empty();
String value = optional.orElse("default"); // Returns "default"

// Mapping values
Value<Integer> number = Value.of(5);
Value<String> text = number.map(Object::toString); // Value of "5"

// With listener for change notification
Value<String> monitored = Value.of("initial")
.listener(newValue -> log("Value changed to: " + newValue));
monitored.set("updated"); // Triggers listener

Specialized Value Classes

IntegerValue

Mutable integer with additional convenience methods.

IntegerValue counter = IntegerValue.of(0);
counter.increment(); // 1
counter.decrement(); // 0
int value = counter.getAndIncrement(); // Returns 0, then sets to 1

LongValue

Mutable long with additional convenience methods.

LongValue counter = LongValue.of(0L);
counter.increment(); // 1L
counter.decrement(); // 0L
long value = counter.getAndIncrement(); // Returns 0L, then sets to 1L

BooleanValue

Nullable boolean with convenience methods.

BooleanValue flag = BooleanValue.of(true);
flag.isTrue(); // true
flag.isNotTrue(); // false

BooleanValue nullable = BooleanValue.empty();
nullable.isTrue(); // false (null is not true)
nullable.isNotTrue(); // true (null is not true)

Flag

Non-nullable boolean flag (true/false only, no null state).

Flag flag = Flag.of(true);
flag.isTrue(); // true
flag.isFalse(); // false
flag.toggle(); // Switches to false

StringValue

Mutable string with convenience methods for equality testing.

StringValue name = StringValue.of("John");

// Check equality
if (name.is("John")) {
System.out.println("Name is John");
}

// Check multiple values
if (name.isAny("John", "Jane", "Bob")) {
System.out.println("Name found");
}

StringFormat

Unified string formatter supporting both MessageFormat-style and printf-style formatting.

// Mixed format styles
StringFormat fmt = StringFormat.of("Hello {0}, you have %d items");
String result = fmt.format("John", 5);
// Returns: "Hello John, you have 5 items"

// MessageFormat with explicit indices, printf with sequential
StringFormat fmt2 = StringFormat.of("User {0} has %s and {1} items");
String result2 = fmt2.format("Alice", 10, "admin");
// Returns: "User Alice has admin and 10 items"
// {0} -> "Alice", {1} -> 10, %s -> "admin"

// Printf with explicit indices
StringFormat fmt3 = StringFormat.of("%1$s loves %2$s, and {0} also loves %3$s");
String result3 = fmt3.format("Alice", "Bob", "Charlie");
// Returns: "Alice loves Bob, and Alice also loves Charlie"

HashCode

Utility class for generating integer hash codes.

int hashCode = HashCode.create()
.add("foobar")
.add(myObject)
.add(123)
.get();

Version

Version management utility.

Version version = Version.of("1.2.3");
version.getMajor(); // 1
version.getMinor(); // 2
version.getPatch(); // 3

// Compare versions
Version v1 = Version.of("1.2.3");
Version v2 = Version.of("1.2.4");
int comparison = v1.compareTo(v2); // -1 (v1 < v2)

VersionRange

Version range utility for checking if a version falls within a range.

VersionRange range = VersionRange.of("1.0.0", "2.0.0");
boolean inRange = range.contains(Version.of("1.5.0")); // true

StateEnum

Enumeration of state machine states for use in parsing operations.

import static org.apache.juneau.commons.lang.StateEnum.*;

// Use in state machine
var state = S1;
if (state == S1) {
// Handle state S1
} else if (state == S2) {
// Handle state S2
}

TriState

Three-state enum (TRUE, FALSE, UNKNOWN).

TriState state = TriState.TRUE;
state.isTrue(); // true
state.isFalse(); // false
state.isUnknown(); // false

TriState unknown = TriState.UNKNOWN;
unknown.isTrue(); // false
unknown.isFalse(); // false
unknown.isUnknown(); // true

AsciiSet and AsciiMap

Utilities for working with ASCII character sets and maps.

AsciiSet asciiSet = AsciiSet.of("abc");
boolean contains = asciiSet.contains('a'); // true

AsciiMap<String> asciiMap = AsciiMap.of();
asciiMap.put('a', "alpha");
String value = asciiMap.get('a'); // "alpha"

Common Patterns

Mutable Values in Lambdas

// Use Value to create effectively final mutable variables
Value<String> result = Value.empty();
list.stream()
.filter(x -> x.matches(pattern))
.forEach(x -> result.set(x));

String found = result.orElse("not found");

Version Checking

Version current = Version.of("1.2.3");
VersionRange supported = VersionRange.of("1.0.0", "2.0.0");

if (supported.contains(current)) {
// Version is supported
}

State Machine

import static org.apache.juneau.commons.lang.StateEnum.*;

StateEnum state = S1;
while (state != null) {
switch (state) {
case S1:
// Process S1
state = S2;
break;
case S2:
// Process S2
state = S3;
break;
// ...
}
}

See Also

Discussion

Share feedback or follow-up questions for this page directly through GitHub.