title: Check if only one element exist using Guava
body: <p>Recently I had a need to do 'special case' scenario if only one element exist in collection. Checking for <code>...size() == 1</code> and retrieving by using <code>...iterator.next()</code> looked ugly so I've created two methods in home brew Collections class:</p>
  
  <pre><code>public class Collections {
      public static &lt;T&gt; boolean isSingleValue(Collection&lt;T&gt; values) {
          return values.size() == 1;
      }
  
      public static &lt;T&gt; T singleValue(Collection&lt;T&gt; values) {
          Assert.isTrue(isSingleValue(values));
          return values.iterator().next();
      }
  }
  </code></pre>
  
  <p>Few days ago I discovered that Guava has method called <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Iterables.html#getOnlyElement%28java.lang.Iterable%29" rel="nofollow">Iterables.getOnlyElement</a>. It covers my need and replaces <code>singleValue</code>, but I can't find match for <code>isSingleValue</code>. Is that by design? Is it worth to put feature request for having <code>Iterables.isOnlyElement</code> method? </p>
  
  <p><strong>EDIT:</strong>
  Since there were few upvotes I decided to open enhancement on guava - <a href="http://code.google.com/p/guava-libraries/issues/detail?id=957" rel="nofollow">issue 957</a>. Final resolution - 'WontFix'. Arguments are very similar to what Thomas/Xaerxess provided.</p>