[JAVA-3380] Default values for Document#getLong and Document#getDouble Created: 08/Aug/19  Updated: 27/Oct/23  Resolved: 09/Aug/19

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Shea Cartwright Assignee: Jeffrey Yemin
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

N/A



 Description   

Would it be possible to add default values to Document#getLong and Document#getDouble, you can already do it through Document#getEmbedded but that seems impractical. Pretty much all the getX's have an option for a default value so I don't know if there's a reason you haven't done it for double and long. 



 Comments   
Comment by Jeffrey Yemin [ 09/Aug/19 ]

I suspect the getX's preceded this method.

Comment by Shea Cartwright [ 09/Aug/19 ]

I see, didn't know this was a thing thank you. Just out of interest why do you have the getX's if you can just do this for every type?

Comment by Jeffrey Yemin [ 09/Aug/19 ]

Sorry, I totally messed up that example. Try this:

        Document d = new Document("x", 1L);
        long x = d.get("x", 0L);
        long y = d.get("y", 0L);
        System.out.println(x);
        System.out.println(y);

It will print

1
0

Comment by Shea Cartwright [ 09/Aug/19 ]

Wouldn't you need to provide it with a key in some way?

Comment by Jeffrey Yemin [ 08/Aug/19 ]

You can also use `<T> T Document#get(java.lang.Object, T)`, as in:

        Document d = new Document();
        long l = d.get(Long.class, 1);
        System.out.println(l);

Generated at Thu Feb 08 08:59:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.