You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lh...@apache.org on 2009/09/18 18:14:16 UTC

svn commit: r816697 - in /incubator/shiro/trunk/core/src/main/java/org/apache/shiro: authc/pam/FirstSuccessfulStrategy.java util/CollectionUtils.java

Author: lhazlewood
Date: Fri Sep 18 16:14:16 2009
New Revision: 816697

URL: http://svn.apache.org/viewvc?rev=816697&view=rev
Log:
SHIRO-104 - JavaDoc and minor cleanup

Modified:
    incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authc/pam/FirstSuccessfulStrategy.java
    incubator/shiro/trunk/core/src/main/java/org/apache/shiro/util/CollectionUtils.java

Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authc/pam/FirstSuccessfulStrategy.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authc/pam/FirstSuccessfulStrategy.java?rev=816697&r1=816696&r2=816697&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authc/pam/FirstSuccessfulStrategy.java (original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/authc/pam/FirstSuccessfulStrategy.java Fri Sep 18 16:14:16 2009
@@ -18,19 +18,19 @@
  */
 package org.apache.shiro.authc.pam;
 
-import java.util.Collection;
-
 import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.authc.AuthenticationInfo;
 import org.apache.shiro.authc.AuthenticationToken;
 import org.apache.shiro.realm.Realm;
+import org.apache.shiro.util.CollectionUtils;
+
+import java.util.Collection;
 
 /**
  * {@link AuthenticationStrategy} implementation that only accepts the account data from
  * the first successfully consulted Realm and ignores all subsequent realms.  This is slightly
- * different behavior than
- * {@link AtLeastOneSuccessfulStrategy AtLeastOneSuccessfulAuthenticationStrategy},
- * so please review both to see which one meets your needs better.
+ * different behavior than {@link AtLeastOneSuccessfulStrategy}, so please review both to see
+ * which one meets your needs better.
  *
  * @author Les Hazlewood
  * @see AtLeastOneSuccessfulStrategy AtLeastOneSuccessfulAuthenticationStrategy
@@ -39,22 +39,22 @@
 public class FirstSuccessfulStrategy extends AbstractAuthenticationStrategy {
 
     /**
-     * Returns <code>null</code> immediately, relying on this class's {@link #merge merge} implementation to return
-     * only the first <code>info</code> object it encounters, ignoring all subsequent ones.
+     * Returns {@code null} immediately, relying on this class's {@link #merge merge} implementation to return
+     * only the first {@code info} object it encounters, ignoring all subsequent ones.
      */
     public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException {
         return null;
     }
 
     /**
-     * Returns the specified <code>aggregate</code> instance if is non null and valid (that is, has principals and they are
-     * not empty) immediately, or, if it is null or not valid, the <code>info</code> argument is returned instead.
+     * Returns the specified {@code aggregate} instance if is non null and valid (that is, has principals and they are
+     * not empty) immediately, or, if it is null or not valid, the {@code info} argument is returned instead.
      * <p/>
      * This logic ensures that the first valid info encountered is the one retained and all subsequent ones are ignored,
      * since this strategy mandates that only the info from the first successfully authenticated realm be used.
      */
     protected AuthenticationInfo merge(AuthenticationInfo info, AuthenticationInfo aggregate) {
-        if (aggregate != null && aggregate.getPrincipals() != null && !aggregate.getPrincipals().isEmpty()) {
+        if (aggregate != null && !CollectionUtils.isEmpty(aggregate.getPrincipals())) {
             return aggregate;
         }
         return info != null ? info : aggregate;

Modified: incubator/shiro/trunk/core/src/main/java/org/apache/shiro/util/CollectionUtils.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/main/java/org/apache/shiro/util/CollectionUtils.java?rev=816697&r1=816696&r2=816697&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/main/java/org/apache/shiro/util/CollectionUtils.java (original)
+++ incubator/shiro/trunk/core/src/main/java/org/apache/shiro/util/CollectionUtils.java Fri Sep 18 16:14:16 2009
@@ -18,10 +18,12 @@
  */
 package org.apache.shiro.util;
 
+import org.apache.shiro.subject.PrincipalCollection;
+
 import java.util.*;
 
 /**
- * Static helper class for use dealing with Arrays.
+ * Static helper class for use dealing with Collections.
  *
  * @author Jeremy Haile
  * @author Les Hazlewood
@@ -32,14 +34,14 @@
     //TODO - complete JavaDoc
 
     /**
-     * Simple method that just returns <code>Collections.EMPTY_SET</code>.
+     * Simple method that just returns {@link Collections#EMPTY_SET}.
      * This exists to enable type-safe empty collections so other locations in Shiro code
      * do not need to worry about suppressing warnings.
      *
      * @param clazz the class of the collection type to return
      * @return an empty collection
      */
-    @SuppressWarnings({"unchecked"})
+    @SuppressWarnings({"unchecked", "UnusedDeclaration"})
     public static <E> Collection<E> emptyCollection(Class<E> clazz) {
         return Collections.EMPTY_SET;
     }
@@ -55,8 +57,12 @@
     }
 
     /**
-     * @param c
-     * @return
+     * Returns {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
+     * {@code false} otherwise.
+     *
+     * @param c the collection to check
+     * @return {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
+     *         {@code false} otherwise.
      * @since 1.0
      */
     public static boolean isEmpty(Collection c) {
@@ -64,14 +70,31 @@
     }
 
     /**
-     * @param m
-     * @return
+     * Returns {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
+     * {@code false} otherwise.
+     *
+     * @param m the {@code Map} to check
+     * @return {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
+     *         {@code false} otherwise.
      * @since 1.0
      */
     public static boolean isEmpty(Map m) {
         return m == null || m.isEmpty();
     }
 
+    /**
+     * Returns {@code true} if the specified {@code PrincipalCollection} is {@code null} or
+     * {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise.
+     *
+     * @param principals the principals to check.
+     * @return {@code true} if the specified {@code PrincipalCollection} is {@code null} or
+     *         {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise.
+     * @since 1.0
+     */
+    public static boolean isEmpty(PrincipalCollection principals) {
+        return principals == null || principals.isEmpty();
+    }
+
     @SuppressWarnings({"unchecked"})
     public static <E> List<E> asList(E... elements) {
         if (elements == null || elements.length == 0) {
@@ -84,6 +107,17 @@
         return list;
     }
 
+    public static <E> Deque<E> asDeque(E... elements) {
+        if (elements == null || elements.length == 0) {
+            return new ArrayDeque<E>();
+        }
+        // Avoid integer overflow when a large array is passed in
+        int capacity = computeListCapacity(elements.length);
+        ArrayDeque<E> deque = new ArrayDeque<E>(capacity);
+        Collections.addAll(deque, elements);
+        return deque;
+    }
+
     static int computeListCapacity(int arraySize) {
         return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE);
     }