Skip to content

DOCSP-33345: Java comments pt. 8 #477

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

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fundamentals.monolightcodec;

// Defines a custom class that contains fields describing a light
// start class
public class Monolight {
private PowerStatus powerStatus = PowerStatus.OFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecRegistry;

// Implements the Codec interface by overriding the encode(), decode(), and getEncoderClass() methods
// start class
public class MonolightCodec implements Codec<Monolight>{

Expand All @@ -19,6 +20,7 @@ public MonolightCodec(CodecRegistry registry) {
this.integerCodec = registry.get(Integer.class);
}

// Defines an encode() method to convert Monolight enum values to BSON values
@Override
public void encode(BsonWriter writer, Monolight value, EncoderContext encoderContext) {
writer.writeStartDocument();
Expand All @@ -29,6 +31,7 @@ public void encode(BsonWriter writer, Monolight value, EncoderContext encoderCon
writer.writeEndDocument();
}

// Defines a decode() method to convert BSON values to Monolight enum values
@Override
public Monolight decode(BsonReader reader, DecoderContext decoderContext) {
Monolight monolight = new Monolight();
Expand All @@ -49,6 +52,7 @@ public Monolight decode(BsonReader reader, DecoderContext decoderContext) {
return monolight;
}

// Returns an instance of the Monolight class, since Java cannot infer the class type
@Override
public Class<Monolight> getEncoderClass() {
return Monolight.class;
Expand Down
15 changes: 15 additions & 0 deletions source/includes/fundamentals/code-snippets/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,51 @@ public static void main(String [] args){
}

private void comparisonFilter(){
// Creates a filter to match documents that have a "qty" value over 7
// begin comparisonFilter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// begin comparisonFilter
// begin comparisonFilter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these for includes reasons? If so, ignore the suggestion. If not, please remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, these are to mark the code to include

Bson filter = Filters.gt("qty", 7);

// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end comparisonFilter
}

private void logicalFilter(){
// Creates a filter to match documents that have a "qty" value less than or equal to 7 and a "color" value of "pink"
// begin logicalFilter
Bson filter = Filters.and(Filters.lte("qty", 5), Filters.ne("color", "pink"));

// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end logicalFilter
}

private void elementFilter(){
// Creates a filter to match documents that contain the "rating" field
// begin elementFilter
Bson filter = Filters.exists("rating");

// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end elementFilter
}

private void evaluationFilter(){
// Creates a filter to match documents that have a "color" value ending with the letter "k"
// begin evaluationFilter
Bson filter = Filters.regex("color", "k$");

// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end evaluationFilter
}

private void arrayFilter(){
// Creates a filter to match documents whose "vendor" values are an array of 3 elements
// begin arrayFilter
Bson filter = Filters.size("vendor", 3);

// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end arrayFilter
}
Expand Down