-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add contour angle to contours report #915
Open
SamCarlberg
wants to merge
4
commits into
WPIRoboticsProjects:master
Choose a base branch
from
SamCarlberg:publish-contour-angles
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package edu.wpi.grip.core.util; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A holder for data that gets lazily initialized. | ||
* | ||
* @param <T> the type of held data | ||
*/ | ||
public class LazyInit<T> { | ||
|
||
private T value = null; | ||
private final Supplier<? extends T> factory; | ||
|
||
/** | ||
* Creates a new lazily initialized data holder. | ||
* | ||
* @param factory the factory to use to create the held value | ||
*/ | ||
public LazyInit(Supplier<? extends T> factory) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't make this expose a constructor, it doesn't tightly couple this to needing to be newed. |
||
this.factory = Objects.requireNonNull(factory, "factory"); | ||
} | ||
|
||
/** | ||
* Gets the value, initializing it if it has not yet been created. | ||
* | ||
* @return the held value | ||
*/ | ||
public T get() { | ||
if (value == null) { | ||
value = factory.get(); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* Clears the held value. The next call to {@link #get()} will re-instantiate the held value. | ||
*/ | ||
public void clear() { | ||
value = null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should you be able to clear a lazy? |
||
|
||
} |
30 changes: 30 additions & 0 deletions
30
core/src/main/java/edu/wpi/grip/core/util/PointerStream.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,30 @@ | ||
package edu.wpi.grip.core.util; | ||
|
||
import java.util.stream.LongStream; | ||
import java.util.stream.Stream; | ||
|
||
import static org.bytedeco.javacpp.opencv_core.Mat; | ||
import static org.bytedeco.javacpp.opencv_core.MatVector; | ||
|
||
/** | ||
* Utility class for streaming native vector wrappers like {@code MatVector} | ||
* ({@code std::vector<T>}) with the Java {@link Stream} API. | ||
*/ | ||
public final class PointerStream { | ||
|
||
private PointerStream() { | ||
throw new UnsupportedOperationException("This is a utility class!"); | ||
} | ||
|
||
/** | ||
* Creates a stream of {@code Mat} objects in a {@code MatVector}. | ||
* | ||
* @param vector the vector of {@code Mats} to stream | ||
* | ||
* @return a new stream object for the contents of the vector | ||
*/ | ||
public static Stream<Mat> ofMatVector(MatVector vector) { | ||
return LongStream.range(0, vector.size()) | ||
.mapToObj(vector::get); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
core/src/test/java/edu/wpi/grip/core/util/LazyInitTest.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 @@ | ||
package edu.wpi.grip.core.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class LazyInitTest { | ||
|
||
@Test | ||
public void testFactoryIsOnlyCalledOnce() { | ||
final String output = "foo"; | ||
final int[] count = {0}; | ||
final Supplier<String> factory = () -> { | ||
count[0]++; | ||
return output; | ||
}; | ||
|
||
LazyInit<String> lazyInit = new LazyInit<>(factory); | ||
lazyInit.get(); | ||
assertEquals(1, count[0]); | ||
|
||
lazyInit.get(); | ||
assertEquals("Calling get() more than once should only call the factory once", 1, count[0]); | ||
} | ||
|
||
@Test | ||
public void testClear() { | ||
final String output = "foo"; | ||
final int[] count = {0}; | ||
final Supplier<String> factory = () -> { | ||
count[0]++; | ||
return output; | ||
}; | ||
LazyInit<String> lazyInit = new LazyInit<>(factory); | ||
lazyInit.get(); | ||
assertEquals(1, count[0]); | ||
|
||
lazyInit.clear(); | ||
lazyInit.get(); | ||
assertEquals(2, count[0]); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So there are no thread safety guarantees offered? Is this intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping!