This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
The following commit(s) were added to refs/heads/master by this push: new 0013d81d6 Use java Stream instead. 0013d81d6 is described below commit 0013d81d626174ee8e72b851ab8c58091670e402 Author: Arturo Bernal <arturobern...@gmail.com> AuthorDate: Thu Oct 20 21:58:06 2022 +0200 Use java Stream instead. --- .../src/main/java/org/apache/wiki/WikiSession.java | 15 ++--------- .../apache/wiki/auth/AuthenticationManager.java | 7 +---- .../wiki/auth/DefaultAuthorizationManager.java | 6 +---- .../java/org/apache/wiki/auth/SessionMonitor.java | 7 +++-- .../org/apache/wiki/auth/acl/AclEntryImpl.java | 21 +++------------ .../java/org/apache/wiki/auth/acl/AclImpl.java | 7 +---- .../wiki/auth/authorize/DefaultGroupManager.java | 8 ++---- .../java/org/apache/wiki/auth/authorize/Group.java | 21 +++------------ .../auth/authorize/WebContainerAuthorizer.java | 30 +++++----------------- .../wiki/auth/permissions/GroupPermission.java | 8 +----- .../org/apache/wiki/auth/user/XMLUserDatabase.java | 22 +++++----------- .../apache/wiki/diff/ContextualDiffProvider.java | 24 +++++------------ .../org/apache/wiki/filters/ProfanityFilter.java | 10 ++------ .../java/org/apache/wiki/parser/LinkParser.java | 7 ++--- .../apache/wiki/plugin/AbstractReferralPlugin.java | 16 ++++-------- .../main/java/org/apache/wiki/plugin/Denounce.java | 7 +---- .../java/org/apache/wiki/plugin/IndexPlugin.java | 12 +++------ .../wiki/plugin/ReferringUndefinedPagesPlugin.java | 14 ++++------ .../wiki/providers/CachingAttachmentProvider.java | 6 +---- .../wiki/references/DefaultReferenceManager.java | 19 +++----------- .../org/apache/wiki/render/CreoleRenderer.java | 5 ++-- .../apache/wiki/search/LuceneSearchProvider.java | 17 ++++-------- .../org/apache/wiki/tags/AdminBeanIteratorTag.java | 8 ++---- .../org/apache/wiki/tags/EditorIteratorTag.java | 7 +++-- .../java/org/apache/wiki/tags/UserProfileTag.java | 18 +++++-------- .../main/java/org/apache/wiki/ui/Installer.java | 9 +++---- .../org/apache/wiki/ui/WikiRequestWrapper.java | 11 ++------ .../wiki/ui/admin/DefaultAdminBeanManager.java | 7 +---- .../wiki/variables/DefaultVariableManager.java | 22 +++------------- .../wiki/workflow/DefaultWorkflowManager.java | 7 ++--- .../org/apache/wiki/xmlrpc/AbstractRPCHandler.java | 9 +++---- .../java/org/apache/wiki/xmlrpc/RPCHandler.java | 10 ++------ .../org/apache/wiki/xmlrpc/RPCHandlerUTF8.java | 10 ++------ .../main/java/org/apache/wiki/util/TextUtil.java | 15 +++-------- .../org/apache/wiki/util/TimedCounterList.java | 10 ++------ .../main/java/org/apache/wiki/util/XmlUtil.java | 11 +++----- .../htmltowiki/XHtmlElementToWikiTranslator.java | 7 ++--- 37 files changed, 110 insertions(+), 340 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiSession.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiSession.java index 318b2156b..c161cf6e7 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiSession.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiSession.java @@ -91,12 +91,7 @@ public class WikiSession implements Session { * @return the result */ protected boolean isInGroup( final Group group ) { - for( final Principal principal : getPrincipals() ) { - if( isAuthenticated() && group.isMember( principal ) ) { - return true; - } - } - return false; + return Arrays.stream(getPrincipals()).anyMatch(principal -> isAuthenticated() && group.isMember(principal)); } /** @@ -211,16 +206,10 @@ public class WikiSession implements Session { /** {@inheritDoc} */ @Override public Principal[] getPrincipals() { - final ArrayList< Principal > principals = new ArrayList<>(); // Take the first non Role as the main Principal - for( final Principal principal : m_subject.getPrincipals() ) { - if ( AuthenticationManager.isUserPrincipal( principal ) ) { - principals.add( principal ); - } - } - return principals.toArray( new Principal[0] ); + return m_subject.getPrincipals().stream().filter(AuthenticationManager::isUserPrincipal).toArray(Principal[]::new); } /** {@inheritDoc} */ diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/AuthenticationManager.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/AuthenticationManager.java index 72a93f92a..3657473ca 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/AuthenticationManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/AuthenticationManager.java @@ -187,12 +187,7 @@ public interface AuthenticationManager extends Initializable { * @return the login principal */ default Principal getLoginPrincipal( final Set< Principal > principals ) { - for( final Principal principal : principals ) { - if ( isUserPrincipal( principal ) ) { - return principal; - } - } - return null; + return principals.stream().filter(AuthenticationManager::isUserPrincipal).findFirst().orElse(null); } // events processing ....................................................... diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/DefaultAuthorizationManager.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/DefaultAuthorizationManager.java index 09b0bc296..f39f65cdc 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/DefaultAuthorizationManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/DefaultAuthorizationManager.java @@ -189,11 +189,7 @@ public class DefaultAuthorizationManager implements AuthorizationManager { if( session.isAuthenticated() && AuthenticationManager.isUserPrincipal( principal ) ) { final String principalName = principal.getName(); final Principal[] userPrincipals = session.getPrincipals(); - for( final Principal userPrincipal : userPrincipals ) { - if( userPrincipal.getName().equals( principalName ) ) { - return true; - } - } + return Arrays.stream(userPrincipals).anyMatch(userPrincipal -> userPrincipal.getName().equals(principalName)); } return false; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/SessionMonitor.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/SessionMonitor.java index ddd8dd81e..dee79f4ad 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/SessionMonitor.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/SessionMonitor.java @@ -39,6 +39,7 @@ import java.util.Collection; import java.util.Map; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** * <p>Manages Sessions for different Engines.</p> @@ -220,11 +221,9 @@ public class SessionMonitor implements HttpSessionListener { * @return the array of user principals */ public final Principal[] userPrincipals() { - final Collection<Principal> principals = new ArrayList<>(); + final Collection<Principal> principals; synchronized ( m_sessions ) { - for ( final Session session : m_sessions.values()) { - principals.add( session.getUserPrincipal() ); - } + principals = m_sessions.values().stream().map(Session::getUserPrincipal).collect(Collectors.toList()); } final Principal[] p = principals.toArray( new Principal[0] ); Arrays.sort( p, m_comparator ); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclEntryImpl.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclEntryImpl.java index 87cdac7b1..cc7464a45 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclEntryImpl.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclEntryImpl.java @@ -25,6 +25,7 @@ import java.security.Permission; import java.security.Principal; import java.util.Enumeration; import java.util.Vector; +import java.util.stream.Collectors; /** @@ -135,19 +136,8 @@ public class AclEntryImpl implements AclEntry, Serializable { */ public String toString() { final Principal p = getPrincipal(); - final StringBuilder sb = new StringBuilder(); - sb.append( "[AclEntry ALLOW " ) - .append( p != null ? p.getName() : "null" ) - .append( " " ); - - for( final Permission pp : m_permissions ) { - sb.append( pp.toString() ); - sb.append( "," ); - } - - sb.append( "]" ); - return sb.toString(); + return m_permissions.stream().map(pp -> pp.toString() + ",").collect(Collectors.joining("", "[AclEntry ALLOW " + (p != null ? p.getName() : "null") + " ", "]")); } /** @@ -155,12 +145,7 @@ public class AclEntryImpl implements AclEntry, Serializable { * permission. */ private Permission findPermission( final Permission p ) { - for( final Permission pp : m_permissions ) { - if( pp.implies( p ) ) { - return pp; - } - } - return null; + return m_permissions.stream().filter(pp -> pp.implies(p)).findFirst().orElse(null); } } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclImpl.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclImpl.java index b5388e70d..11af72c27 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclImpl.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/AclImpl.java @@ -115,13 +115,8 @@ public class AclImpl implements Acl, Serializable { /** {@inheritDoc} */ @Override public AclEntry getAclEntry( final Principal principal ) { - for( final AclEntry entry : m_entries ) { - if( entry.getPrincipal().getName().equals( principal.getName() ) ) { - return entry; - } - } + return m_entries.stream().filter(entry -> entry.getPrincipal().getName().equals(principal.getName())).findFirst().orElse(null); - return null; } /** {@inheritDoc} */ diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/DefaultGroupManager.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/DefaultGroupManager.java index cb7f45b8d..3d6c2ed97 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/DefaultGroupManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/DefaultGroupManager.java @@ -42,6 +42,7 @@ import org.apache.wiki.ui.InputValidator; import org.apache.wiki.util.ClassUtil; import java.security.Principal; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -181,12 +182,7 @@ public class DefaultGroupManager implements GroupManager, Authorizer, WikiEventL } // Check each user principal to see if it belongs to the group - for( final Principal principal : session.getPrincipals() ) { - if( AuthenticationManager.isUserPrincipal( principal ) && group.isMember( principal ) ) { - return true; - } - } - return false; + return Arrays.stream(session.getPrincipals()).anyMatch(principal -> AuthenticationManager.isUserPrincipal(principal) && group.isMember(principal)); } /** {@inheritDoc} */ diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/Group.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/Group.java index cba072f15..fc0b733ab 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/Group.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/Group.java @@ -138,13 +138,7 @@ public class Group { return false; } - for( final Principal principal : m_members ) { - if( !g.isMember( principal ) ) { - return false; - } - } - - return true; + return m_members.stream().allMatch(g::isMember); } /** @@ -154,11 +148,7 @@ public class Group { */ @Override public int hashCode() { - int hc = 0; - for( final Principal member : m_members ) { - hc ^= member.hashCode(); - } - return hc; + return m_members.stream().mapToInt(Principal::hashCode).reduce(0, (a, b) -> a ^ b); } /** @@ -307,13 +297,8 @@ public class Group { } private Principal findMember( final String name ) { - for( final Principal member : m_members ) { - if( member.getName().equals( name ) ) { - return member; - } - } + return m_members.stream().filter(member -> member.getName().equals(name)).findFirst().orElse(null); - return null; } } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/WebContainerAuthorizer.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/WebContainerAuthorizer.java index 7018c67f7..5a7724ddb 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/WebContainerAuthorizer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/authorize/WebContainerAuthorizer.java @@ -39,10 +39,12 @@ import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.net.URL; import java.security.Principal; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.Set; +import java.util.stream.Collectors; /** @@ -118,10 +120,7 @@ public class WebContainerAuthorizer implements WebAuthorizer { } if( m_containerRoles.length > 0 ) { - final StringBuilder roles = new StringBuilder(); - for( final Role containerRole : m_containerRoles ) { - roles.append(containerRole).append(" "); - } + final String roles = Arrays.stream(m_containerRoles).map(containerRole -> containerRole + " ").collect(Collectors.joining()); LOG.info( " JSPWiki determined the web container manages these roles: " + roles ); } LOG.info( "Authorizer WebContainerAuthorizer initialized successfully." ); @@ -181,12 +180,7 @@ public class WebContainerAuthorizer implements WebAuthorizer { */ @Override public Principal findRole( final String role ) { - for( final Role containerRole : m_containerRoles ) { - if ( containerRole.getName().equals( role ) ) { - return containerRole; - } - } - return null; + return Arrays.stream(m_containerRoles).filter(containerRole -> containerRole.getName().equals(role)).findFirst().map(containerRole -> containerRole).orElse(null); } /** @@ -243,14 +237,7 @@ public class WebContainerAuthorizer implements WebAuthorizer { } // If a constraint is contained in both lists, we must be constrained - for( final Element constraint : constraints ) { - for( final Element roleConstraint : roles ) { - if( constraint.equals( roleConstraint ) ) { - return true; - } - } - } - return false; + return constraints.stream().anyMatch(constraint -> roles.stream().anyMatch(constraint::equals)); } /** @@ -297,7 +284,7 @@ public class WebContainerAuthorizer implements WebAuthorizer { * @return an array of Role objects */ protected Role[] getRoles( final Document webxml ) { - final Set<Role> roles = new HashSet<>(); + final Set<Role> roles; final Element root = webxml.getRootElement(); final Namespace jeeNs = Namespace.getNamespace( "j", J2EE_SCHEMA_25_NAMESPACE ); @@ -306,10 +293,7 @@ public class WebContainerAuthorizer implements WebAuthorizer { final List< Element > constraints = XPathFactory.instance() .compile( constrainsSelector, Filters.element(), null, jeeNs ) .evaluate( root ); - for( final Element constraint : constraints ) { - final String role = constraint.getTextTrim(); - roles.add( new Role( role ) ); - } + roles = constraints.stream().map(Element::getTextTrim).map(Role::new).collect(Collectors.toSet()); // Get all defined roles final String rolesSelector = "//j:web-app/j:security-role/j:role-name"; diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/GroupPermission.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/GroupPermission.java index cef601ab1..42352ee2b 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/GroupPermission.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/permissions/GroupPermission.java @@ -488,13 +488,7 @@ public final class GroupPermission extends Permission implements Serializable // GroupPrincipal with same name as target final Subject subject = ( (SubjectDomainCombiner) dc ).getSubject(); final Set<GroupPrincipal> principals = subject.getPrincipals( GroupPrincipal.class ); - for( final Principal principal : principals ) - { - if ( principal.getName().equals( gp.m_group ) ) - { - return true; - } - } + return principals.stream().anyMatch(principal -> principal.getName().equals(gp.m_group)); } return false; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/XMLUserDatabase.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/XMLUserDatabase.java index 069bc3d31..026237427 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/user/XMLUserDatabase.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/user/XMLUserDatabase.java @@ -52,6 +52,8 @@ import java.util.Map; import java.util.Properties; import java.util.SortedSet; import java.util.TreeSet; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * <p>Manages {@link DefaultUserProfile} objects using XML files for persistence. Passwords are hashed using SHA1. User entries are simple @@ -372,14 +374,7 @@ public class XMLUserDatabase extends AbstractUserDatabase { final DateFormat c_format = new SimpleDateFormat( DATE_FORMAT ); final String index = profile.getLoginName(); final NodeList users = c_dom.getElementsByTagName( USER_TAG ); - Element user = null; - for( int i = 0; i < users.getLength(); i++ ) { - final Element currentUser = ( Element )users.item( i ); - if( currentUser.getAttribute( LOGIN_NAME ).equals( index ) ) { - user = currentUser; - break; - } - } + Element user = IntStream.range(0, users.getLength()).mapToObj(i -> (Element) users.item(i)).filter(currentUser -> currentUser.getAttribute(LOGIN_NAME).equals(index)).findFirst().orElse(null); boolean isNew = false; @@ -523,17 +518,12 @@ public class XMLUserDatabase extends AbstractUserDatabase { * @return the text nodes that are immediate children of the base element, concatenated together */ private String extractText( final Element element ) { - final StringBuilder text = new StringBuilder(); + final String text = ""; if( element.getChildNodes().getLength() > 0 ) { final NodeList children = element.getChildNodes(); - for( int k = 0; k < children.getLength(); k++ ) { - final Node child = children.item( k ); - if( child.getNodeType() == Node.TEXT_NODE ) { - text.append(((Text) child).getData()); - } - } + text = IntStream.range(0, children.getLength()).mapToObj(children::item).filter(child -> child.getNodeType() == Node.TEXT_NODE).map(child -> ((Text) child).getData()).collect(Collectors.joining()); } - return text.toString(); + return text; } /** diff --git a/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java index 9075116d2..32d96bce1 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/diff/ContextualDiffProvider.java @@ -38,9 +38,11 @@ import org.suigeneris.jrcs.diff.myers.MyersDiff; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.StringTokenizer; +import java.util.stream.Collectors; /** @@ -253,25 +255,18 @@ public class ContextualDiffProvider implements DiffProvider { if (m_firstElem > 0) { final int endIndex = Math.min( m_firstElem + m_unchangedContextLimit, m_origStrings.length -1 ); - for( int j = m_firstElem; j < endIndex; j++ ) { - m_sb.append( m_origStrings[ j ] ); - } + m_sb.append(Arrays.stream(m_origStrings, m_firstElem, endIndex).collect(Collectors.joining("", "", ELIDED_TAIL_INDICATOR_HTML))); - m_sb.append( ELIDED_TAIL_INDICATOR_HTML ); } m_sb.append( ELIDED_HEAD_INDICATOR_HTML ); final int startIndex = Math.max(orig.first() - m_unchangedContextLimit, 0); - for (int j = startIndex; j < orig.first(); j++) { - m_sb.append( m_origStrings[ j ] ); - } + m_sb.append(Arrays.stream(m_origStrings, startIndex, orig.first()).collect(Collectors.joining())); } else { // No need to skip anything, just output the whole range... - for( int j = m_firstElem; j < orig.first(); j++ ) { - m_sb.append( m_origStrings[ j ] ); - } + m_sb.append(Arrays.stream(m_origStrings, m_firstElem, orig.first()).collect(Collectors.joining())); } } m_firstElem = orig.last() + 1; @@ -346,16 +341,11 @@ public class ContextualDiffProvider implements DiffProvider { // If there's more than the limit of the orginal left just emit limit and elided... if( ( m_origStrings.length - m_firstElem ) > m_unchangedContextLimit ) { final int endIndex = Math.min( m_firstElem + m_unchangedContextLimit, m_origStrings.length -1 ); - for (int j = m_firstElem; j < endIndex; j++) { - m_sb.append( m_origStrings[ j ] ); - } + m_sb.append(Arrays.stream(m_origStrings, m_firstElem, endIndex).collect(Collectors.joining("", "", ELIDED_TAIL_INDICATOR_HTML))); - m_sb.append( ELIDED_TAIL_INDICATOR_HTML ); } else { // emit entire tail of original... - for( int j = m_firstElem; j < m_origStrings.length; j++ ) { - m_sb.append( m_origStrings[ j ] ); - } + m_sb.append(Arrays.stream(m_origStrings, m_firstElem, m_origStrings.length).collect(Collectors.joining())); } } } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/filters/ProfanityFilter.java b/jspwiki-main/src/main/java/org/apache/wiki/filters/ProfanityFilter.java index 240aefd5d..77ed0d7e8 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/filters/ProfanityFilter.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/filters/ProfanityFilter.java @@ -52,15 +52,9 @@ public class ProfanityFilter extends BasePageFilter { throw new IOException( "No property file found! (Check the installation, it should be there.)" ); } try( final BufferedReader br = new BufferedReader( new InputStreamReader( in ) ) ) { - final List< String > profs = new ArrayList<>(); - String str; - while ( ( str = br.readLine() ) != null ) { - if( !str.isEmpty() && !str.startsWith( "#" ) ) { // allow comments on profanities file - profs.add( str ); - } - } - c_profanities = profs.toArray( new String[0] ); + // allow comments on profanities file + c_profanities = br.lines().filter(str -> !str.isEmpty() && !str.startsWith("#")).toArray(String[]::new); } } catch( final IOException e ) { LOG.error( "Unable to load profanities from " + PROPERTYFILE, e ); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/parser/LinkParser.java b/jspwiki-main/src/main/java/org/apache/wiki/parser/LinkParser.java index 5484a9595..42e0bd55f 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/parser/LinkParser.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/parser/LinkParser.java @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.StringTokenizer; +import java.util.stream.IntStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -335,11 +336,7 @@ public class LinkParser */ public static final boolean isSpace(final String s ) { - for( int i = 0 ; i < s.length() ; i++ ) - { - if( !isSpace( s.charAt(i)) ) return false; - } - return true; + return IntStream.range(0, s.length()).allMatch(i -> isSpace(s.charAt(i))); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/AbstractReferralPlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/AbstractReferralPlugin.java index 4eb422847..b878b0e5a 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/AbstractReferralPlugin.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/AbstractReferralPlugin.java @@ -52,6 +52,7 @@ import java.text.ParseException; import java.text.RuleBasedCollator; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Iterator; @@ -260,20 +261,13 @@ public abstract class AbstractReferralPlugin implements Plugin { boolean includeThis = m_include == null; if( m_include != null ) { - for( final Pattern pattern : m_include ) { - if( pm.matches( pageName, pattern ) ) { - includeThis = true; - break; - } - } + includeThis = Arrays.stream(m_include).anyMatch(pattern -> pm.matches(pageName, pattern)) ? true : m_include == null; } if( m_exclude != null ) { - for( final Pattern pattern : m_exclude ) { - if( pm.matches( pageName, pattern ) ) { - includeThis = false; - break; // The inner loop, continue on the next item - } + // The inner loop, continue on the next item + if (Arrays.stream(m_exclude).anyMatch(pattern -> pm.matches(pageName, pattern))) { + includeThis = false; } } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/Denounce.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/Denounce.java index 7e84792f2..95aa8b24c 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/Denounce.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/Denounce.java @@ -167,12 +167,7 @@ public class Denounce implements Plugin { */ private boolean matchPattern( final List< Pattern > list, final String path ) { final PatternMatcher matcher = new Perl5Matcher(); - for( final Pattern pattern : list ) { - if( matcher.matches( path, pattern ) ) { - return true; - } - } - return false; + return list.stream().anyMatch(pattern -> matcher.matches(path, pattern)); } private boolean matchHeaders( final HttpServletRequest request ) { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/IndexPlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/IndexPlugin.java index 7f7faf285..16aea1466 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/IndexPlugin.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/IndexPlugin.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * A Plugin that creates an index of pages according to a certain pattern. @@ -138,16 +139,9 @@ public class IndexPlugin extends AbstractReferralPlugin implements Plugin { private List<String> listPages( final Context context, final String include, final String exclude ) throws ProviderException { final Pattern includePtrn = include != null ? Pattern.compile( include ) : Pattern.compile(".*"); final Pattern excludePtrn = exclude != null ? Pattern.compile( exclude ) : Pattern.compile("\\p{Cntrl}"); // there are no control characters in page names - final List< String > result = new ArrayList<>(); + final List< String > result; final Set< String > pages = context.getEngine().getManager( ReferenceManager.class ).findCreated(); - for( final String pageName : pages ) { - if( excludePtrn.matcher( pageName ).matches() ) { - continue; - } - if( includePtrn.matcher( pageName ).matches() ) { - result.add( pageName ); - } - } + result = pages.stream().filter(pageName -> !excludePtrn.matcher(pageName).matches()).filter(pageName -> includePtrn.matcher(pageName).matches()).collect(Collectors.toList()); return result; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/ReferringUndefinedPagesPlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/ReferringUndefinedPagesPlugin.java index 0cd11de56..07323c040 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/ReferringUndefinedPagesPlugin.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/ReferringUndefinedPagesPlugin.java @@ -28,8 +28,11 @@ import org.apache.wiki.util.TextUtil; import java.text.MessageFormat; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.ResourceBundle; import java.util.TreeMap; +import java.util.stream.Collectors; + /** * <p>Lists all pages containing links to Undefined Pages (pages containing dead links).</p> * @@ -60,16 +63,9 @@ public class ReferringUndefinedPagesPlugin extends AbstractReferralPlugin { super.initialize( context, params ); Collection< String > result = null; - final TreeMap< String, String > sortedMap = new TreeMap<>(); + final TreeMap< String, String > sortedMap; if( uncreatedPages != null ) { - for( final String uncreatedPageName : uncreatedPages ) { - final Collection< String > referrers = referenceManager.findReferrers( uncreatedPageName ); - if( referrers != null ) { - for( final String referringPage : referrers ) { - sortedMap.put( referringPage, "" ); - } - } - } + sortedMap = uncreatedPages.stream().map(referenceManager::findReferrers).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toMap(referringPage -> referringPage, referringPage -> "", (a, b) -> b, TreeMap::new)); result = sortedMap.keySet(); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java index 9d1d264de..e8523d071 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java @@ -180,11 +180,7 @@ public class CachingAttachmentProvider implements AttachmentProvider { */ private Attachment findAttachmentFromCollection( final Collection< Attachment > c, final String name ) { if( c != null ) { - for( final Attachment att : c ) { - if( name.equals( att.getFileName() ) ) { - return att; - } - } + return c.stream().filter(att -> name.equals(att.getFileName())).findFirst().orElse(null); } return null; diff --git a/jspwiki-main/src/main/java/org/apache/wiki/references/DefaultReferenceManager.java b/jspwiki-main/src/main/java/org/apache/wiki/references/DefaultReferenceManager.java index ce95d4ca6..1c5b2b6a0 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/references/DefaultReferenceManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/references/DefaultReferenceManager.java @@ -47,6 +47,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /* BUGS @@ -530,11 +531,7 @@ public class DefaultReferenceManager extends BasePageFilter implements Reference final Collection< String > oldRefTo = m_refersTo.get( page ); m_refersTo.remove( page ); - final TreeSet< String > cleanedRefs = new TreeSet<>(); - for( final String ref : references ) { - final String reference = getFinalPageName( ref ); - cleanedRefs.add( reference ); - } + final TreeSet< String > cleanedRefs = references.stream().map(this::getFinalPageName).collect(Collectors.toCollection(TreeSet::new)); m_refersTo.put( page, cleanedRefs ); @@ -713,20 +710,12 @@ public class DefaultReferenceManager extends BasePageFilter implements Reference */ @Override public Collection< String > findUncreated() { - final TreeSet< String > uncreated = new TreeSet<>(); + final TreeSet< String > uncreated; // Go through m_refersTo values and check that m_refersTo has the corresponding keys. // We want to reread the code to make sure our HashMaps are in sync... final Collection< Collection< String > > allReferences = m_refersTo.values(); - for( final Collection<String> refs : allReferences ) { - if( refs != null ) { - for( final String aReference : refs ) { - if( !m_engine.getManager( PageManager.class ).wikiPageExists( aReference ) ) { - uncreated.add( aReference ); - } - } - } - } + uncreated = allReferences.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(aReference -> !m_engine.getManager(PageManager.class).wikiPageExists(aReference)).collect(Collectors.toCollection(TreeSet::new)); return uncreated; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java b/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java index 263295204..67dca3e72 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/CreoleRenderer.java @@ -27,6 +27,8 @@ import org.jdom2.Text; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * Implements DOM-to-Creole rendering. @@ -105,8 +107,7 @@ public class CreoleRenderer extends WikiRenderer { m_listCount++; m_listChar = '#'; } else if( LI.equals(ce.getName()) ) { - for(int i = 0; i < m_listCount; i++ ) sb.append( m_listChar ); - sb.append( ONE_SPACE ); + sb.append(IntStream.range(0, m_listCount).mapToObj(i -> String.valueOf(m_listChar)).collect(Collectors.joining("", "", ONE_SPACE))); } else if( A.equals( ce.getName() ) ) { final String href = ce.getAttributeValue( HREF_ATTRIBUTE ); final String text = ce.getText(); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java index c93b33b83..fabbef1d2 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java @@ -76,6 +76,7 @@ import java.io.InputStreamReader; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -83,6 +84,7 @@ import java.util.List; import java.util.Properties; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.stream.Collectors; /** @@ -280,13 +282,7 @@ public class LuceneSearchProvider implements SearchProvider { final String filename = att.getFileName(); - boolean searchSuffix = false; - for( final String suffix : SEARCHABLE_FILE_SUFFIXES ) { - if( filename.endsWith( suffix ) ) { - searchSuffix = true; - break; - } - } + boolean searchSuffix = Arrays.stream(SEARCHABLE_FILE_SUFFIXES).anyMatch(filename::endsWith); String out = filename; if( searchSuffix ) { @@ -378,12 +374,9 @@ public class LuceneSearchProvider implements SearchProvider { // Now add the names of the attachments of this page try { final List< Attachment > attachments = m_engine.getManager( AttachmentManager.class ).listAttachments( page ); - final StringBuilder attachmentNames = new StringBuilder(); + final String attachmentNames = attachments.stream().map(att -> att.getName() + ";").collect(Collectors.joining()); - for( final Attachment att : attachments ) { - attachmentNames.append( att.getName() ).append( ";" ); - } - field = new Field( LUCENE_ATTACHMENTS, attachmentNames.toString(), TextField.TYPE_STORED ); + field = new Field( LUCENE_ATTACHMENTS, attachmentNames, TextField.TYPE_STORED ); doc.add( field ); } catch( final ProviderException e ) { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java index 4913aea92..991cef02b 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/AdminBeanIteratorTag.java @@ -24,6 +24,7 @@ import org.apache.wiki.ui.admin.AdminBeanManager; import java.util.ArrayList; import java.util.Collection; +import java.util.stream.Collectors; /** * Provides an iterator for all AdminBeans of a given type. @@ -54,12 +55,7 @@ public class AdminBeanIteratorTag extends IteratorTag { public void resetIterator() { final AdminBeanManager mgr = m_wikiContext.getEngine().getManager( AdminBeanManager.class ); final Collection< AdminBean > beans = mgr.getAllBeans(); - final ArrayList< AdminBean > typedBeans = new ArrayList<>(); - for( final AdminBean ab : beans ) { - if( ab.getType() == m_type ) { - typedBeans.add( ab ); - } - } + final ArrayList< AdminBean > typedBeans = beans.stream().filter(ab -> ab.getType() == m_type).collect(Collectors.toCollection(ArrayList::new)); setList( typedBeans ); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java index cea885360..3f10b9b44 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/EditorIteratorTag.java @@ -24,7 +24,9 @@ import org.apache.wiki.ui.Editor; import org.apache.wiki.ui.EditorManager; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.stream.Collectors; /** * Iterates through editors. @@ -43,11 +45,8 @@ public class EditorIteratorTag extends IteratorTag { final Engine engine = m_wikiContext.getEngine(); final EditorManager mgr = engine.getManager( EditorManager.class ); final String[] editorList = mgr.getEditorList(); - final Collection< Editor > editors = new ArrayList<>(); + final Collection< Editor > editors = Arrays.stream(editorList).map(editor -> new Editor(m_wikiContext, editor)).collect(Collectors.toList()); - for( final String editor : editorList ) { - editors.add( new Editor( m_wikiContext, editor ) ); - } setList( editors ); return super.doStartTag(); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/tags/UserProfileTag.java b/jspwiki-main/src/main/java/org/apache/wiki/tags/UserProfileTag.java index 01c0f6b12..8facc0ffe 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/tags/UserProfileTag.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/tags/UserProfileTag.java @@ -35,8 +35,10 @@ import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.security.Principal; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.ResourceBundle; +import java.util.stream.Collectors; /** * <p> @@ -170,14 +172,10 @@ public class UserProfileTag extends WikiTagBase { */ public static String printGroups( final Context context ) { final Principal[] roles = context.getWikiSession().getRoles(); - final List< String > tempRoles = new ArrayList<>(); + final List< String > tempRoles; final ResourceBundle rb = Preferences.getBundle( context, InternationalizationManager.CORE_BUNDLE ); - for( final Principal role : roles ) { - if( role instanceof GroupPrincipal ) { - tempRoles.add( role.getName() ); - } - } + tempRoles = Arrays.stream(roles).filter(role -> role instanceof GroupPrincipal).map(Principal::getName).collect(Collectors.toList()); if( tempRoles.size() == 0 ) { return rb.getString( "userprofile.nogroups" ); } @@ -205,14 +203,10 @@ public class UserProfileTag extends WikiTagBase { */ public static String printRoles( final Context context ) { final Principal[] roles = context.getWikiSession().getRoles(); - final List< String > tempRoles = new ArrayList<>(); + final List< String > tempRoles; final ResourceBundle rb = Preferences.getBundle( context, InternationalizationManager.CORE_BUNDLE ); - for( final Principal role : roles ) { - if( role instanceof Role ) { - tempRoles.add( role.getName() ); - } - } + tempRoles = Arrays.stream(roles).filter(role -> role instanceof Role).map(Principal::getName).collect(Collectors.toList()); if( tempRoles.size() == 0 ) { return rb.getString( "userprofile.noroles" ); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/Installer.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/Installer.java index 50b5900de..c1c4f5c95 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ui/Installer.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/Installer.java @@ -45,6 +45,7 @@ import java.text.MessageFormat; import java.util.Properties; import java.util.ResourceBundle; import java.util.Set; +import java.util.stream.Collectors; /** * Manages JSPWiki installation on behalf of <code>admin/Install.jsp</code>. The contents of this class were previously part of @@ -154,12 +155,10 @@ public class Installer { * @return the string */ public String getPropertiesList() { - final StringBuilder result = new StringBuilder(); + final String result; final Set< String > keys = m_props.stringPropertyNames(); - for( final String key : keys ) { - result.append(key ).append( " = " ).append( m_props.getProperty( key ) ).append( "\n" ); - } - return result.toString(); + result = keys.stream().map(key -> key + " = " + m_props.getProperty(key) + "\n").collect(Collectors.joining()); + return result; } public String getPropertiesPath() { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiRequestWrapper.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiRequestWrapper.java index 64104ec92..19c63984d 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiRequestWrapper.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiRequestWrapper.java @@ -26,6 +26,7 @@ import org.apache.wiki.auth.authorize.Role; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.security.Principal; +import java.util.Arrays; /** * Servlet request wrapper that encapsulates an incoming HTTP request and overrides its security methods so that the request returns @@ -101,17 +102,9 @@ public class WikiRequestWrapper extends HttpServletRequestWrapper { // Iterate through all of the built-in roles and look for a match final Principal[] principals = m_session.getRoles(); - for( final Principal value : principals ) { - if( value instanceof Role ) { - final Role principal = ( Role )value; - if( Role.isBuiltInRole( principal ) && principal.getName().equals( role ) ) { - return true; - } - } - } // None of the built-in roles match, so no luck - return false; + return Arrays.stream(principals).filter(value -> value instanceof Role).map(value -> (Role) value).anyMatch(principal -> Role.isBuiltInRole(principal) && principal.getName().equals(role)); } } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/admin/DefaultAdminBeanManager.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/admin/DefaultAdminBeanManager.java index dc86a052e..fac49b109 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ui/admin/DefaultAdminBeanManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/admin/DefaultAdminBeanManager.java @@ -190,13 +190,8 @@ public class DefaultAdminBeanManager implements WikiEventListener, AdminBeanMana */ @Override public AdminBean findBean( final String id ) { - for( final AdminBean ab : m_allBeans ) { - if( ab.getId().equals( id ) ) { - return ab; - } - } + return m_allBeans.stream().filter(ab -> ab.getId().equals(id)).findFirst().orElse(null); - return null; } /** diff --git a/jspwiki-main/src/main/java/org/apache/wiki/variables/DefaultVariableManager.java b/jspwiki-main/src/main/java/org/apache/wiki/variables/DefaultVariableManager.java index c654eea18..adce1b51e 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/variables/DefaultVariableManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/variables/DefaultVariableManager.java @@ -42,6 +42,7 @@ import java.util.Date; import java.util.List; import java.util.Properties; import java.util.ResourceBundle; +import java.util.stream.Collectors; /** @@ -323,30 +324,13 @@ public class DefaultVariableManager implements VariableManager { } public String getInterwikilinks() { - final StringBuilder res = new StringBuilder(); - for( final String link : m_context.getEngine().getAllInterWikiLinks() ) { - if( res.length() > 0 ) { - res.append( ", " ); - } - res.append( link ); - res.append( " --> " ); - res.append( m_context.getEngine().getInterWikiURL( link ) ); - } - return res.toString(); + return m_context.getEngine().getAllInterWikiLinks().stream().map(link -> link + " --> " + m_context.getEngine().getInterWikiURL(link)).collect(Collectors.joining(", ")); } public String getInlinedimages() { - final StringBuilder res = new StringBuilder(); - for( final String ptrn : m_context.getEngine().getAllInlinedImagePatterns() ) { - if( res.length() > 0 ) { - res.append( ", " ); - } - - res.append( ptrn ); - } - return res.toString(); + return m_context.getEngine().getAllInlinedImagePatterns().stream().collect(Collectors.joining(", ")); } public String getPluginpath() { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java b/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java index f5a7e9109..473abe866 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/workflow/DefaultWorkflowManager.java @@ -241,11 +241,8 @@ public class DefaultWorkflowManager implements WorkflowManager { final Principal[] sessionPrincipals = session.getPrincipals(); for( final Workflow w : m_workflows ) { final Principal owner = w.getOwner(); - for ( final Principal sessionPrincipal : sessionPrincipals ) { - if ( sessionPrincipal.equals( owner ) ) { - workflows.add( w ); - break; - } + if (Arrays.stream(sessionPrincipals).anyMatch(sessionPrincipal -> sessionPrincipal.equals(owner))) { + workflows.add(w); } } } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/AbstractRPCHandler.java b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/AbstractRPCHandler.java index ec7f88c05..28dbfbff4 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/AbstractRPCHandler.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/AbstractRPCHandler.java @@ -33,6 +33,7 @@ import java.util.Date; import java.util.Hashtable; import java.util.Set; import java.util.Vector; +import java.util.stream.Collectors; /** * Provides definitions for RPC handler routines. @@ -72,7 +73,7 @@ public abstract class AbstractRPCHandler implements WikiRPCHandler { public Vector getRecentChanges( final Date since ) { checkPermission( PagePermission.VIEW ); final Set< Page > pages = m_engine.getManager( PageManager.class ).getRecentChanges(); - final Vector< Hashtable< ?, ? > > result = new Vector<>(); + final Vector< Hashtable< ?, ? > > result; // Transform UTC into local time. final Calendar cal = Calendar.getInstance(); @@ -80,11 +81,7 @@ public abstract class AbstractRPCHandler implements WikiRPCHandler { cal.add( Calendar.MILLISECOND, cal.get( Calendar.ZONE_OFFSET ) + (cal.getTimeZone().inDaylightTime( since ) ? cal.get( Calendar.DST_OFFSET ) : 0 ) ); - for( final Page page : pages ) { - if( page.getLastModified().after( cal.getTime() ) ) { - result.add( encodeWikiPage( page ) ); - } - } + result = pages.stream().filter(page -> page.getLastModified().after(cal.getTime())).map(this::encodeWikiPage).collect(Collectors.toCollection(Vector::new)); return result; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandler.java b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandler.java index 4be6ff807..670d229e2 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandler.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandler.java @@ -38,6 +38,7 @@ import java.util.Date; import java.util.Hashtable; import java.util.Set; import java.util.Vector; +import java.util.stream.Collectors; /** * Provides handlers for all RPC routines. @@ -79,15 +80,8 @@ public class RPCHandler extends AbstractRPCHandler { public Vector< String > getAllPages() { checkPermission( PagePermission.VIEW ); final Collection< Page > pages = m_engine.getManager( PageManager.class ).getRecentChanges(); - final Vector< String > result = new Vector<>(); - for( final Page p : pages ) { - if( !( p instanceof Attachment ) ) { - result.add( toRPCString( p.getName() ) ); - } - } - - return result; + return pages.stream().filter(p -> !(p instanceof Attachment)).map(p -> toRPCString(p.getName())).collect(Collectors.toCollection(Vector::new)); } /** diff --git a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandlerUTF8.java b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandlerUTF8.java index 67870d5ce..4e5c399b9 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandlerUTF8.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/xmlrpc/RPCHandlerUTF8.java @@ -35,6 +35,7 @@ import java.util.Date; import java.util.Hashtable; import java.util.Set; import java.util.Vector; +import java.util.stream.Collectors; /** * Provides handlers for all RPC routines. These routines are used by @@ -54,15 +55,8 @@ public class RPCHandlerUTF8 extends AbstractRPCHandler { checkPermission( PagePermission.VIEW ); final Set< Page > pages = m_engine.getManager( PageManager.class ).getRecentChanges(); - final Vector< String > result = new Vector<>(); - for( final Page p : pages ) { - if( !( p instanceof Attachment ) ) { - result.add( p.getName() ); - } - } - - return result; + return pages.stream().filter(p -> !(p instanceof Attachment)).map(Page::getName).collect(Collectors.toCollection(Vector::new)); } /** diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java b/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java index f6c4809a6..9b7df6c36 100644 --- a/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java +++ b/jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java @@ -30,6 +30,8 @@ import java.security.SecureRandom; import java.util.NoSuchElementException; import java.util.Properties; import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** @@ -777,12 +779,8 @@ public final class TextUtil { * @since 2.1.98. */ public static String repeatString( final String what, final int times ) { - final StringBuilder sb = new StringBuilder(); - for( int i = 0; i < times; i++ ) { - sb.append( what ); - } - return sb.toString(); + return IntStream.range(0, times).mapToObj(i -> what).collect(Collectors.joining()); } /** @@ -869,12 +867,7 @@ public final class TextUtil { * @since 2.4 */ public static String generateRandomPassword() { - final StringBuilder pw = new StringBuilder(); - for( int i = 0; i < PASSWORD_LENGTH; i++ ) { - final int index = ( int )( RANDOM.nextDouble() * PWD_BASE.length() ); - pw.append(PWD_BASE.charAt( index )); - } - return pw.toString(); + return IntStream.range(0, PASSWORD_LENGTH).map(i -> (int) (RANDOM.nextDouble() * PWD_BASE.length())).mapToObj(index -> String.valueOf(PWD_BASE.charAt(index))).collect(Collectors.joining()); } } diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/TimedCounterList.java b/jspwiki-util/src/main/java/org/apache/wiki/util/TimedCounterList.java index 4e115521f..0c23cd30d 100644 --- a/jspwiki-util/src/main/java/org/apache/wiki/util/TimedCounterList.java +++ b/jspwiki-util/src/main/java/org/apache/wiki/util/TimedCounterList.java @@ -152,18 +152,12 @@ public class TimedCounterList<T> extends AbstractList<T> */ public int count(final T obj ) { - int c = 0; + int c; m_lock.readLock().lock(); try { - for( final CounterItem< T > i : m_list ) - { - if( i.m_obj.equals( obj ) ) - { - c++; - } - } + c = (int) m_list.stream().filter(i -> i.m_obj.equals(obj)).count(); } finally { diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java b/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java index cf9258fa6..4d7d03eff 100644 --- a/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java +++ b/jspwiki-util/src/main/java/org/apache/wiki/util/XmlUtil.java @@ -40,6 +40,7 @@ import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; /** * Utility class to parse XML files. @@ -122,15 +123,11 @@ public final class XmlUtil { if( doc == null ) { return ""; } - final StringBuilder sb = new StringBuilder(); + final String sb; final List< ? > nodes = XPathFactory.instance().compile( ALL_TEXT_NODES ).evaluate( doc ); - for( final Object el : nodes ) { - if( el instanceof Text ) { - sb.append( ( ( Text )el ).getValue() ); - } - } + sb = nodes.stream().filter(el -> el instanceof Text).map(el -> ((Text) el).getValue()).collect(Collectors.joining()); - return sb.toString(); + return sb; } public static Element getXPathElement( final Element base, final String expression ) { diff --git a/jspwiki-wysiwyg/src/main/java/org/apache/wiki/htmltowiki/XHtmlElementToWikiTranslator.java b/jspwiki-wysiwyg/src/main/java/org/apache/wiki/htmltowiki/XHtmlElementToWikiTranslator.java index b61fe131f..18e1ed9c7 100644 --- a/jspwiki-wysiwyg/src/main/java/org/apache/wiki/htmltowiki/XHtmlElementToWikiTranslator.java +++ b/jspwiki-wysiwyg/src/main/java/org/apache/wiki/htmltowiki/XHtmlElementToWikiTranslator.java @@ -38,6 +38,7 @@ import java.util.Arrays; import java.util.Deque; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Collectors; /** @@ -230,11 +231,7 @@ public class XHtmlElementToWikiTranslator { } private String propsToStyleString( final Map< Object, Object > styleProps ) { - final StringBuilder style = new StringBuilder(); - for( final Map.Entry< Object, Object > entry : styleProps.entrySet() ) { - style.append( " " ).append( entry.getKey() ).append( ": " ).append( entry.getValue() ).append( ";" ); - } - return style.toString(); + return styleProps.entrySet().stream().map(entry -> " " + entry.getKey() + ": " + entry.getValue() + ";").collect(Collectors.joining()); } public void translateChildren( final Element base ) throws JDOMException {