Coverage Report - com.jcabi.log.SecretDecor
 
Classes in this File Line Coverage Branch Coverage Complexity
SecretDecor
0%
0/29
0%
0/26
3.333
 
 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.FormattableFlags;
 34  
 import java.util.Formatter;
 35  
 import lombok.EqualsAndHashCode;
 36  
 import lombok.ToString;
 37  
 
 38  
 /**
 39  
  * Decorator of a secret text.
 40  
  * @author Marina Kosenko (marina.kosenko@gmail.com)
 41  
  * @author Yegor Bugayenko (yegor@teamed.io)
 42  
  * @version $Id: 98efd0fb4ede139c80e15babbc27d72990c7381b $
 43  
  * @since 0.1
 44  
  */
 45  0
 @ToString
 46  0
 @EqualsAndHashCode(of = "secret")
 47  
 final class SecretDecor implements Formattable {
 48  
 
 49  
     /**
 50  
      * The secret to work with.
 51  
      */
 52  
     private final transient String secret;
 53  
 
 54  
     /**
 55  
      * Public ctor.
 56  
      * @param scrt The secret
 57  
      */
 58  
     @SuppressWarnings("PMD.NullAssignment")
 59  0
     SecretDecor(final Object scrt) {
 60  0
         if (scrt == null) {
 61  0
             this.secret = null;
 62  
         } else {
 63  0
             this.secret = scrt.toString();
 64  
         }
 65  0
     }
 66  
 
 67  
     // @checkstyle ParameterNumber (4 lines)
 68  
     @Override
 69  
     public void formatTo(final Formatter formatter, final int flags,
 70  
         final int width, final int precision) {
 71  0
         if (this.secret == null) {
 72  0
             formatter.format("NULL");
 73  
         } else {
 74  0
             final StringBuilder fmt = new StringBuilder(10);
 75  0
             fmt.append('%');
 76  0
             if ((flags & FormattableFlags.LEFT_JUSTIFY) != 0) {
 77  0
                 fmt.append('-');
 78  
             }
 79  0
             if (width != 0) {
 80  0
                 fmt.append(width);
 81  
             }
 82  0
             if ((flags & FormattableFlags.UPPERCASE) == 0) {
 83  0
                 fmt.append('s');
 84  
             } else {
 85  0
                 fmt.append('S');
 86  
             }
 87  0
             formatter.format(
 88  
                 fmt.toString(),
 89  
                 SecretDecor.scramble(this.secret)
 90  
             );
 91  
         }
 92  0
     }
 93  
 
 94  
     /**
 95  
      * Scramble it and make unreadable.
 96  
      * @param text The text to scramble
 97  
      * @return The result
 98  
      */
 99  
     private static String scramble(final String text) {
 100  0
         final StringBuilder out = new StringBuilder(10);
 101  0
         if (text.isEmpty()) {
 102  0
             out.append('?');
 103  
         } else {
 104  0
             out.append(text.charAt(0));
 105  
         }
 106  0
         out.append("***");
 107  0
         if (text.isEmpty()) {
 108  0
             out.append('?');
 109  
         } else {
 110  0
             out.append(text.charAt(text.length() - 1));
 111  
         }
 112  0
         return out.toString();
 113  
     }
 114  
 
 115  
 }