<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:21 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[JAVA-29] facilitate use of &quot;for (X x : collection)&quot; syntax with DBCursor</title>
                <link>https://jira.mongodb.org/browse/JAVA-29</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Just some food for thought. DBCursor implements Iterator&amp;lt;DBObject&amp;gt; but not Iterable&amp;lt;DBObject&amp;gt; so cannot directly be used in a for loop. Consider implementing Iterable&amp;lt;DBObject&amp;gt; as well, and add its one method perhaps as follows:&lt;/p&gt;

&lt;p&gt;@Override public Iterator&amp;lt;DBObject&amp;gt; iterator() {&lt;br/&gt;
	return this.copy();&lt;br/&gt;
	// outgoing copy() might not be necessary (you could just &quot;return this;&quot;) but just to be safe&lt;br/&gt;
}	&lt;/p&gt;

&lt;p&gt;This allows the syntax&lt;/p&gt;

&lt;p&gt;DBCursor cursor = db.find(query);&lt;br/&gt;
for (DBObject object : cursor)&lt;br/&gt;
{&lt;br/&gt;
  // do something with &quot;object&quot;&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;or even&lt;/p&gt;

&lt;p&gt;for (DBObject object : db.find(query))&lt;br/&gt;
{&lt;br/&gt;
  // do something with &quot;object&quot;&lt;br/&gt;
}&lt;/p&gt;


&lt;p&gt;It&apos;s only a minor convenience over&lt;/p&gt;

&lt;p&gt;DBCursor cursor = db.find(query);&lt;br/&gt;
while (cursor.hasNext())&lt;br/&gt;
{&lt;br/&gt;
  DBObject object = cursor.next();&lt;br/&gt;
  // do something with &quot;object&quot;&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;but makes loop code just a bit more clear.&lt;/p&gt;

&lt;p&gt;I&apos;m getting around this by making my own wrapper class:&lt;br/&gt;
	static public class CursorWrapper implements Iterable&amp;lt;DBObject&amp;gt;&lt;br/&gt;
	{&lt;br/&gt;
		private final DBCursor cursor;&lt;br/&gt;
		private CursorWrapper(DBCursor cursor)&lt;/p&gt;
		{
			this.cursor = cursor;
		}
&lt;p&gt;		static public CursorWrapper wrap(DBCursor cursor)&lt;/p&gt;
		{
			return new CursorWrapper(cursor.copy());
			// incoming copy() might not be necessary, but just to be safe
		}
&lt;p&gt;		@Override public Iterator&amp;lt;DBObject&amp;gt; iterator() &lt;/p&gt;
{
			return this.cursor.copy();
			// outgoing copy() might not be necessary, but just to be safe
		}
&lt;p&gt;		&lt;br/&gt;
	}&lt;/p&gt;

&lt;p&gt;and then can do&lt;/p&gt;

&lt;p&gt;	for (DBObject obj : CursorWrapper.wrap(coll.find().limit(100)))&lt;/p&gt;
	{
		System.out.println(obj.get(&quot;metadata&quot;));			
	}</description>
                <environment></environment>
        <key id="10543">JAVA-29</key>
            <summary>facilitate use of &quot;for (X x : collection)&quot; syntax with DBCursor</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</type>
                                            <priority id="4" iconUrl="https://jira.mongodb.org/images/icons/priorities/minor.svg">Minor - P4</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="eliot">Eliot Horowitz</assignee>
                                    <reporter username="jmsachs">Jason Sachs</reporter>
                        <labels>
                    </labels>
                <created>Wed, 2 Sep 2009 16:24:11 +0000</created>
                <updated>Fri, 2 Oct 2009 11:38:55 +0000</updated>
                            <resolved>Fri, 4 Sep 2009 15:55:28 +0000</resolved>
                                                    <fixVersion>0.9</fixVersion>
                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                        <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrhdnb:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>15011</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>