Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1468 from wind57/more-changes
first commit
- Loading branch information
Showing
5 changed files
with
130 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
util/src/main/java/io/kubernetes/client/util/Preconditions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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. | ||
*/ | ||
package io.kubernetes.client.util; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* support for different invariant checks, like for a example a non-null argument being passed to a | ||
* method that does not support nulls. | ||
* | ||
* @author wind57 | ||
*/ | ||
public final class Preconditions { | ||
|
||
private Preconditions() { | ||
throw new AssertionError("not supported"); | ||
} | ||
|
||
/** | ||
* @param validate argument to be validated | ||
* @param predicate to check against | ||
* @param errorSupplier the error message generator | ||
* @param <T> | ||
* @return T if Predicate does not match | ||
* @throws IllegalArgumentException if predicate returns true | ||
*/ | ||
public static <T> T precondition( | ||
T validate, Predicate<? super T> predicate, Supplier<String> errorSupplier) { | ||
if (predicate.test(validate)) { | ||
throw new IllegalArgumentException(errorSupplier.get()); | ||
} | ||
return validate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
util/src/test/java/io/kubernetes/client/util/PreconditionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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. | ||
*/ | ||
package io.kubernetes.client.util; | ||
|
||
import static io.kubernetes.client.util.Preconditions.precondition; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
public class PreconditionsTest { | ||
|
||
@Test | ||
public void testEmptyString() { | ||
assertThatThrownBy( | ||
() -> precondition("", Strings::isNullOrEmpty, () -> "string can not be empty")) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("string can not be empty"); | ||
} | ||
|
||
@Test | ||
public void testNonEmptyString() { | ||
String abc = precondition("abc", Strings::isNullOrEmpty, () -> "string can not be empty"); | ||
Assertions.assertThat(abc).isEqualTo("abc"); | ||
} | ||
} |