Coverage Report - com.jcabi.log.MsDecor
 
Classes in this File Line Coverage Branch Coverage Complexity
MsDecor
0%
0/41
0%
0/34
4.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.FormattableFlags;
 34  
 import java.util.Formatter;
 35  
 import lombok.EqualsAndHashCode;
 36  
 import lombok.ToString;
 37  
 
 38  
 /**
 39  
  * Decorate time interval in milliseconds.
 40  
  *
 41  
  * <p>For example:
 42  
  *
 43  
  * <pre>
 44  
  * final long start = System.currentTimeMillis();
 45  
  * // some operations
 46  
  * Logger.debug("completed in %[ms]s", System.currentTimeMillis() - start);
 47  
  * </pre>
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@teamed.io)
 50  
  * @version $Id: 0ba7757213228d4c1ae4cc44f8338ae18346a588 $
 51  
  * @since 0.1
 52  
  */
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = "millis")
 55  
 final class MsDecor implements Formattable {
 56  
 
 57  
     /**
 58  
      * The period to work with, in milliseconds.
 59  
      */
 60  
     private final transient Double millis;
 61  
 
 62  
     /**
 63  
      * Public ctor.
 64  
      * @param msec The interval in milliseconds
 65  
      */
 66  
     @SuppressWarnings("PMD.NullAssignment")
 67  0
     MsDecor(final Long msec) {
 68  0
         if (msec == null) {
 69  0
             this.millis = null;
 70  
         } else {
 71  0
             this.millis = Double.valueOf(msec);
 72  
         }
 73  0
     }
 74  
 
 75  
     /**
 76  
      * {@inheritDoc}
 77  
      * @checkstyle ParameterNumber (4 lines)
 78  
      */
 79  
     @Override
 80  
     public void formatTo(final Formatter formatter, final int flags,
 81  
         final int width, final int precision) {
 82  0
         if (this.millis == null) {
 83  0
             formatter.format("NULL");
 84  
         } else {
 85  0
             final StringBuilder format = new StringBuilder(0);
 86  0
             format.append('%');
 87  0
             if ((flags & FormattableFlags.LEFT_JUSTIFY) == FormattableFlags
 88  
                 .LEFT_JUSTIFY) {
 89  0
                 format.append('-');
 90  
             }
 91  0
             if (width > 0) {
 92  0
                 format.append(Integer.toString(width));
 93  
             }
 94  0
             if ((flags & FormattableFlags.UPPERCASE) == FormattableFlags
 95  
                 .UPPERCASE) {
 96  0
                 format.append('S');
 97  
             } else {
 98  0
                 format.append('s');
 99  
             }
 100  0
             formatter.format(format.toString(), this.toText(precision));
 101  
         }
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Create text.
 106  
      * @param precision The precision
 107  
      * @return The text
 108  
      * @checkstyle MagicNumber (50 lines)
 109  
      */
 110  
     private String toText(final int precision) {
 111  
         double number;
 112  
         String title;
 113  0
         if (this.millis < 1000L) {
 114  0
             number = this.millis;
 115  0
             title = "ms";
 116  0
         } else if (this.millis < 1000L * 60) {
 117  0
             number = this.millis / 1000L;
 118  0
             title = "s";
 119  0
         } else if (this.millis < 1000L * 60 * 60) {
 120  0
             number = this.millis / (1000L * 60);
 121  0
             title = "min";
 122  0
         } else if (this.millis < 1000L * 60 * 60 * 24) {
 123  0
             number = this.millis / (1000L * 60 * 60);
 124  0
             title = "hr";
 125  0
         } else if (this.millis < 1000L * 60 * 60 * 24 * 30) {
 126  0
             number = this.millis / (1000L * 60 * 60 * 24);
 127  0
             title = "days";
 128  
         } else {
 129  0
             number = this.millis / (1000L * 60 * 60 * 24 * 30);
 130  0
             title = "mon";
 131  
         }
 132  
         final String format;
 133  0
         if (precision >= 0) {
 134  0
             format = String.format("%%.%df%%s", precision);
 135  
         } else {
 136  0
             format = "%.0f%s";
 137  
         }
 138  0
         return String.format(format, number, title);
 139  
     }
 140  
 
 141  
 }