Coverage Report - com.jcabi.log.TextDecor
 
Classes in this File Line Coverage Branch Coverage Complexity
TextDecor
0%
0/18
0%
0/16
1.667
 
 1  
 /**
 2  
  * Copyright (c) 2012-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.log;
 31  
 
 32  
 import java.util.Formattable;
 33  
 import java.util.Formatter;
 34  
 import lombok.EqualsAndHashCode;
 35  
 import lombok.ToString;
 36  
 
 37  
 /**
 38  
  * Decorator of a text.
 39  
  *
 40  
  * <p>For example:
 41  
  *
 42  
  * <pre>public void func(Object input) {
 43  
  *   Logger.debug("Long input '%[text]s' provided", input);
 44  
  * }</pre>
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@teamed.io)
 47  
  * @version $Id: 307370af3001b8c390c82d5b4557d93eb44617a0 $
 48  
  * @since 0.1.5
 49  
  */
 50  0
 @ToString
 51  0
 @EqualsAndHashCode(of = "object")
 52  
 final class TextDecor implements Formattable {
 53  
 
 54  
     /**
 55  
      * Maximum length to show.
 56  
      */
 57  
     public static final int MAX = 100;
 58  
 
 59  
     /**
 60  
      * The object.
 61  
      */
 62  
     private final transient Object object;
 63  
 
 64  
     /**
 65  
      * Public ctor.
 66  
      * @param obj The object
 67  
      */
 68  0
     TextDecor(final Object obj) {
 69  0
         this.object = obj;
 70  0
     }
 71  
 
 72  
     /**
 73  
      * {@inheritDoc}
 74  
      * @checkstyle ParameterNumber (4 lines)
 75  
      */
 76  
     @Override
 77  
     public void formatTo(final Formatter formatter, final int flags,
 78  
         final int width, final int precision) {
 79  0
         if (this.object == null) {
 80  0
             formatter.format("NULL");
 81  
         } else {
 82  0
             formatter.format("%s", TextDecor.pretty(this.object.toString()));
 83  
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Make it look pretty.
 88  
      * @param text The text to prettify
 89  
      * @return The result
 90  
      */
 91  
     @SuppressWarnings("PMD.ConsecutiveAppendsShouldReuse")
 92  
     private static String pretty(final String text) {
 93  
         final String result;
 94  0
         if (text.length() < TextDecor.MAX) {
 95  0
             result = text;
 96  
         } else {
 97  0
             final int skip = text.length() - TextDecor.MAX;
 98  0
             final StringBuilder output = new StringBuilder(text.length());
 99  0
             output.append(text.substring(0, (text.length() - skip) / 2));
 100  
             // @checkstyle MultipleStringLiterals (1 line)
 101  0
             output.append("..").append(Integer.toString(skip)).append("..");
 102  0
             output.append(
 103  
                 text.substring(text.length() - TextDecor.MAX + output.length())
 104  
             );
 105  0
             result = output.toString();
 106  
         }
 107  0
         return result.replace("\n", "\\n");
 108  
     }
 109  
 
 110  
 }