Skip to content

Commit

Permalink
Review comment to allow negation of unrestricted
Browse files Browse the repository at this point in the history
  • Loading branch information
njr-11 committed Jan 27, 2025
1 parent 48e2c3a commit 644165f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
49 changes: 49 additions & 0 deletions api/src/main/java/jakarta/data/metamodel/restrict/Unmatchable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package jakarta.data.metamodel.restrict;

import java.util.List;

class Unmatchable<T> implements CompositeRestriction<T> {
static final Unmatchable<?> INSTANCE = new Unmatchable<>();

// prevent instantiation by others
private Unmatchable() {
}

@Override
public boolean isNegated() {
return false;
}

@Override
@SuppressWarnings("unchecked")
public CompositeRestriction<T> negate() {
return (CompositeRestriction<T>) Unrestricted.INSTANCE;
}

@Override
public List<Restriction<T>> restrictions() {
return List.of();
}

@Override
public Type type() {
return Type.ANY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public boolean isNegated() {
}

@Override
@SuppressWarnings("unchecked")
public CompositeRestriction<T> negate() {
throw new UnsupportedOperationException(
"The absence of restrictions cannot be negated.");
return (CompositeRestriction<T>) Unmatchable.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ void shouldCreateNegatedEndsWithRestriction() {
});
}

@Test
void shouldCreateUnmatchableRestrictionWhenNegatingUnrestricted() {
Restriction<Employee> restriction = Restrict.unrestricted();
Restriction<Employee> negated = restriction.negate();

assertThat(negated).isInstanceOf(Unmatchable.class);

Unmatchable<Employee> unmatchable = (Unmatchable<Employee>) negated;

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(unmatchable.isNegated()).isEqualTo(false);
soft.assertThat(unmatchable.negate()).isEqualTo(restriction);
soft.assertThat(unmatchable.restrictions()).isEmpty();
soft.assertThat(unmatchable.type()).isEqualTo(CompositeRestriction.Type.ANY);
});
}

@Test
void shouldCreateUnrestricted() {
Restriction<Employee> restriction = Restrict.unrestricted();
Expand Down Expand Up @@ -205,12 +222,4 @@ void shouldThrowExceptionForInvalidWildcard() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Cannot use the same character (_) for both types of wildcards.");
}

@Test
void shouldThrowExceptionForNegatingUnrestrictedRestriction() {
Restriction<Employee> unrestricted = Restrict.unrestricted();
assertThatThrownBy(() -> unrestricted.negate())
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("The absence of restrictions cannot be negated.");
}
}

0 comments on commit 644165f

Please sign in to comment.