You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2021/06/08 11:21:42 UTC

[GitHub] [james-project] jeantil commented on a change in pull request #479: JAMES-2989 Preview should not normalize spaces of the entire body

jeantil commented on a change in pull request #479:
URL: https://github.com/apache/james-project/pull/479#discussion_r647344522



##########
File path: server/data/data-jmap/src/main/java/org/apache/james/jmap/api/model/Preview.java
##########
@@ -88,15 +89,30 @@ public static Preview from(String value) {
     }
 
     public static Preview compute(String textBody) {
+        int previewOffsetEstimate = estimatePreviewOffset(textBody, MAX_LENGTH);
+        String previewPart = textBody.substring(0, previewOffsetEstimate);
         return Preview.from(
             truncateToMaxLength(
-                StringUtils.normalizeSpace(textBody)));
+                StringUtils.normalizeSpace(previewPart)));
     }
 
     private static String truncateToMaxLength(String body) {
         return StringUtils.left(body, MAX_LENGTH);
     }
 
+    private static int estimatePreviewOffset(String body, int charCount) {
+        AtomicInteger position = new AtomicInteger(0);
+
+        body.chars()
+            .mapToObj(i -> (char) i)

Review comment:
       I'm all for stream processing but this code
   - auto-boxes to Character through an explicit cast
   - then auto-unboxes it back to int (see isLetterOrDigit)
   - forces you to use an atomic integer to work around lambda restrictions
   - forces you to declare the useless forEach to trigger stream evaluation
    
   (untested) loop version  
   ```java
        private static int estimatePreviewOffset(String body, int charCount) {
           int position = 0;
           int nonWhitespace = 0;
           int maxPosition = Math.min(charCount, body.length() - 1);
           while (nonWhitespace <= maxPosition) {
               position++;
               if (Character.isLetterOrDigit(body.charAt(position))) {
                   charCount++;
               }
           }
           return position;
       }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org