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
// Bucket ID is our Project ID | |
String bucket = "gs://" + projectIdProvider.getProjectId(); | |
// Generate a random file name | |
filename = UUID.randomUUID().toString() + ".jpg"; | |
WritableResource resource = (WritableResource) | |
context.getResource(bucket + "/" + filename); | |
// Write the file to Cloud Storage using WritableResource | |
try (OutputStream os = resource.getOutputStream()) { |
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
private AnnotateImageResponse analyzeImage(String uri) { | |
// After the image was written to GCS, analyze it with the GCS URI. | |
// Note: It's also possible to analyze an image embedded in the | |
// request as a Base64 encoded payload. | |
List<AnnotateImageRequest> requests = new ArrayList<>(); | |
ImageSource imgSrc = ImageSource.newBuilder() | |
.setGcsImageUri(uri).build(); | |
Image img = Image.newBuilder().setSource(imgSrc).build(); | |
Feature feature = Feature.newBuilder() | |
.setType(Feature.Type.LABEL_DETECTION).build(); |
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
// After written to GCS, analyze the image. | |
AnnotateImageResponse response = analyzeImage(bucket + "/" + filename); | |
Map<String, Float> imageLabels = | |
response.getLabelAnnotationsList() | |
.stream() | |
.collect( | |
Collectors.toMap( | |
EntityAnnotation::getDescription, | |
EntityAnnotation::getScore, |
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
runtime: java11 | |
instance_class: F1 |
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
// UnaryOperator | |
Stream<Integer> iterate = Stream.iterate(1, new UnaryOperator<Integer>() { | |
@Override | |
public Integer apply(Integer integer) { | |
return integer + 1; | |
} | |
}); | |
System.out.println("iterate.findFirst().get() = " + iterate.findFirst().get()); |
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
// BinaryOperator | |
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() { | |
@Override | |
public Integer apply(Integer x, Integer y) { | |
return x + y; | |
} | |
}).get()); | |
System.out.println("binaryOpeartor = " + Stream.of(1,2,3,4).reduce((x, y) -> x + y).get()); |
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
// Predicate | |
//Evaluates this predicate on the given argument | |
System.out.println("Predicate = " + Stream.of(1,2,3,4).filter(new Predicate<Integer>() { | |
@Override | |
public boolean test(Integer x) { | |
return x % 2 == 0; | |
} | |
}).findFirst().get()); |
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
// Function, Consumer | |
Stream.of(strings).filter(s -> s.matches("\\d+")).map(new Function<String, Integer>() { | |
/** | |
* Applies this function to the given argument. | |
* | |
* @param s the function argument | |
* @return the function result | |
*/ | |
@Override |
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
//Supplier | |
Optional<Integer> value = Stream.of(1,2,3,4).reduce(new BinaryOperator<Integer>() { | |
@Override | |
public Integer apply(Integer x, Integer y) { | |
return x + y; | |
} | |
}); | |
value.orElseGet(new Supplier<Integer>() { |
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
import java.util.Optional; | |
import java.util.function.*; | |
import java.util.stream.Stream; | |
public class FunctionalInterfaces { | |
@FunctionalInterface | |
public interface foo | |
{ | |
abstract public void food(); |
OlderNewer