-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ArbitraryBuilder set value lazily (#307)
- Loading branch information
1 parent
60c3c6d
commit 722dab2
Showing
3 changed files
with
116 additions
and
0 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
80 changes: 80 additions & 0 deletions
80
...ure-monkey/src/main/java/com/navercorp/fixturemonkey/arbitrary/ArbitrarySetLazyValue.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,80 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* 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 com.navercorp.fixturemonkey.arbitrary; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
import net.jqwik.api.Arbitraries; | ||
import net.jqwik.api.Arbitrary; | ||
|
||
public final class ArbitrarySetLazyValue<T> extends AbstractArbitrarySet<T> { | ||
private final Supplier<T> supplier; | ||
private long limit; | ||
|
||
public ArbitrarySetLazyValue(ArbitraryExpression arbitraryExpression, Supplier<T> supplier, long limit) { | ||
super(arbitraryExpression); | ||
this.supplier = supplier; | ||
this.limit = limit; | ||
} | ||
|
||
public ArbitrarySetLazyValue(ArbitraryExpression arbitraryExpression, Supplier<T> supplier) { | ||
this(arbitraryExpression, supplier, Long.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public T getValue() { | ||
return supplier.get(); | ||
} | ||
|
||
@Override | ||
public Arbitrary<T> apply(Arbitrary<T> from) { | ||
if (this.limit > 0) { | ||
limit--; | ||
return Arbitraries.just(getValue()); | ||
} else { | ||
return from; | ||
} | ||
} | ||
|
||
@Override | ||
public ArbitrarySetLazyValue<T> copy() { | ||
return new ArbitrarySetLazyValue<>(this.getArbitraryExpression(), this.supplier, this.limit); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
if (!super.equals(obj)) { | ||
return false; | ||
} | ||
ArbitrarySetLazyValue<?> that = (ArbitrarySetLazyValue<?>)obj; | ||
return getValue().equals(that.getValue()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), getValue()); | ||
} | ||
} |
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