Coverage Report - com.jcabi.log.ListDecor
 
Classes in this File Line Coverage Branch Coverage Complexity
ListDecor
0%
0/23
0%
0/24
4.5
 
 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.Arrays;
 33  
 import java.util.Collection;
 34  
 import java.util.Formattable;
 35  
 import java.util.Formatter;
 36  
 import lombok.EqualsAndHashCode;
 37  
 import lombok.ToString;
 38  
 
 39  
 /**
 40  
  * Format list.
 41  
  * @author Yegor Bugayenko (yegor@teamed.io)
 42  
  * @version $Id: 6af3dee662a4ca91fc5a29610c93d7187ee0f03c $
 43  
  * @since 0.1
 44  
  */
 45  0
 @ToString
 46  0
 @EqualsAndHashCode(of = "list")
 47  
 final class ListDecor implements Formattable {
 48  
 
 49  
     /**
 50  
      * The list.
 51  
      */
 52  
     private final transient Collection<?> list;
 53  
 
 54  
     /**
 55  
      * Public ctor.
 56  
      * @param obj The object to format
 57  
      * @throws DecorException If some problem with it
 58  
      */
 59  0
     ListDecor(final Object obj) throws DecorException {
 60  0
         if (obj == null || obj instanceof Collection) {
 61  0
             this.list = Collection.class.cast(obj);
 62  0
         } else if (obj instanceof Object[]) {
 63  0
             this.list = Arrays.asList((Object[]) obj);
 64  
         } else {
 65  0
             throw new DecorException(
 66  
                 String.format(
 67  
                     "Collection or array required, while %s provided",
 68  
                     obj.getClass().getName()
 69  
                 )
 70  
             );
 71  
         }
 72  0
     }
 73  
 
 74  
     /**
 75  
      * {@inheritDoc}
 76  
      * @checkstyle ParameterNumber (4 lines)
 77  
      */
 78  
     @Override
 79  
     public void formatTo(final Formatter formatter, final int flags,
 80  
         final int width, final int precision) {
 81  0
         final StringBuilder builder = new StringBuilder(0);
 82  0
         builder.append('[');
 83  0
         if (this.list == null) {
 84  0
             builder.append("NULL");
 85  
         } else {
 86  0
             boolean first = true;
 87  0
             for (final Object item : this.list) {
 88  0
                 if (!first) {
 89  0
                     builder.append(", ");
 90  
                 }
 91  0
                 builder.append(String.format("\"%s\"", item));
 92  0
                 first = false;
 93  0
             }
 94  
         }
 95  0
         builder.append(']');
 96  0
         formatter.format("%s", builder.toString());
 97  0
     }
 98  
 
 99  
 }