Add isNotEmpty, isNotBlank methods (#1396)

Adds convenience methods `isNotEmpty` and `isNotBlank`. This borrows the
same methods from Kotlin.

This helps users write more fluent constraints, for example,
`foo.isNotEmpty.implies(bar)`.

Adds:

* List#isNotEmpty
* Map#isNotEmpty
* Set#isNotEmpty
* Mapping#isNotEmpty
* Listing#isNotEmpty
* String#isNotEmpty
* String#isNotBlank
This commit is contained in:
Daniel Chao
2026-01-08 13:22:43 -08:00
committed by GitHub
parent 14d58a17b0
commit ac4f2fd9a6
23 changed files with 1133 additions and 19 deletions

View File

@@ -181,4 +181,8 @@ public final class VmMapping extends VmListingOrMapping {
cachedLength = count.get();
return cachedLength;
}
public boolean isEmpty() {
return getLength() == 0;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,6 +56,13 @@ public final class ListNodes {
}
}
public abstract static class isNotEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(VmList self) {
return !self.isEmpty();
}
}
public abstract static class lastIndex extends ExternalPropertyNode {
@Specialization
protected long eval(VmList self) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,6 +46,13 @@ public final class ListingNodes {
}
}
public abstract static class isNotEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(VmListing self) {
return !self.isEmpty();
}
}
public abstract static class lastIndex extends ExternalPropertyNode {
@Specialization
protected long eval(VmListing self) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,6 +52,13 @@ public final class MapNodes {
}
}
public abstract static class isNotEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(VmMap self) {
return !self.isEmpty();
}
}
public abstract static class keys extends ExternalPropertyNode {
@Specialization
protected VmSet eval(VmMap self) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
*/
package org.pkl.core.stdlib.base;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import org.pkl.core.ast.lambda.ApplyVmFunction1Node;
@@ -28,7 +27,6 @@ import org.pkl.core.stdlib.ExternalMethod0Node;
import org.pkl.core.stdlib.ExternalMethod1Node;
import org.pkl.core.stdlib.ExternalMethod2Node;
import org.pkl.core.stdlib.ExternalPropertyNode;
import org.pkl.core.util.EconomicMaps;
import org.pkl.core.util.MutableBoolean;
import org.pkl.core.util.MutableReference;
@@ -37,15 +35,15 @@ public final class MappingNodes {
public abstract static class isEmpty extends ExternalPropertyNode {
@Specialization
@TruffleBoundary
protected boolean eval(VmMapping self) {
for (VmObjectLike curr = self; curr != null; curr = curr.getParent()) {
var cursor = EconomicMaps.getEntries(curr.getMembers());
while (cursor.advance()) {
if (!(cursor.getKey() instanceof Identifier)) return false;
}
}
return true;
return self.isEmpty();
}
}
public abstract static class isNotEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(VmMapping self) {
return !self.isEmpty();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,6 +46,13 @@ public final class SetNodes {
}
}
public abstract static class isNotEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(VmSet self) {
return !self.isEmpty();
}
}
public abstract static class isEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(VmSet self) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -131,6 +131,13 @@ public final class StringNodes {
}
}
public abstract static class isNotEmpty extends ExternalPropertyNode {
@Specialization
protected boolean eval(String self) {
return !self.isEmpty();
}
}
public abstract static class isBlank extends ExternalPropertyNode {
@Specialization
protected boolean eval(String self) {
@@ -138,6 +145,13 @@ public final class StringNodes {
}
}
public abstract static class isNotBlank extends ExternalPropertyNode {
@Specialization
protected boolean eval(String self) {
return !StringUtils.isBlank(self);
}
}
public abstract static class isRegex extends ExternalPropertyNode {
@Specialization
@TruffleBoundary