Coverage Report - com.jcabi.github.Labels
 
Classes in this File Line Coverage Branch Coverage Complexity
Labels
N/A
N/A
1.286
Labels$Smart
38%
10/26
11%
2/18
1.286
Labels$Smart$AjcClosure1
100%
1/1
N/A
1.286
Labels$Smart$AjcClosure11
0%
0/1
N/A
1.286
Labels$Smart$AjcClosure13
0%
0/1
N/A
1.286
Labels$Smart$AjcClosure15
0%
0/1
N/A
1.286
Labels$Smart$AjcClosure3
0%
0/1
N/A
1.286
Labels$Smart$AjcClosure5
100%
1/1
N/A
1.286
Labels$Smart$AjcClosure7
0%
0/1
N/A
1.286
Labels$Smart$AjcClosure9
0%
0/1
N/A
1.286
 
 1  0
 /**
 2  
  * Copyright (c) 2013-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.github;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import java.io.IOException;
 35  
 import javax.validation.constraints.NotNull;
 36  
 import lombok.EqualsAndHashCode;
 37  
 import lombok.ToString;
 38  
 
 39  
 /**
 40  
  * Github labels.
 41  
  *
 42  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 43  
  * @version $Id$
 44  
  * @since 0.1
 45  
  * @see <a href="http://developer.github.com/v3/issues/labels/">Labels API</a>
 46  
  */
 47  
 @Immutable
 48  
 @SuppressWarnings("PMD.TooManyMethods")
 49  
 public interface Labels {
 50  
 
 51  
     /**
 52  
      * The repo we're in.
 53  
      * @return Repo
 54  
      */
 55  
     @NotNull(message = "repo is never NULL")
 56  
     Repo repo();
 57  
 
 58  
     /**
 59  
      * Create new label.
 60  
      * @param name The name of it
 61  
      * @param color Color of it
 62  
      * @return The label created
 63  
      * @throws IOException If there is any I/O problem
 64  
      * @see <a href="http://developer.github.com/v3/issues/labels/#create-a-label">Create a Label</a>
 65  
      */
 66  
     @NotNull(message = "Label is never NULL")
 67  
     Label create(
 68  
         @NotNull(message = "label name can't be NULL") String name,
 69  
         @NotNull(message = "label color can't be NULL") String color)
 70  
         throws IOException;
 71  
 
 72  
     /**
 73  
      * Get a label by name.
 74  
      * @param name The name of it
 75  
      * @return The label
 76  
      * @see <a href="http://developer.github.com/v3/issues/labels/#get-a-single-label">Get a single label</a>
 77  
      */
 78  
     @NotNull(message = "Label is never NULL")
 79  
     Label get(@NotNull(message = "label name can't be NULL") String name);
 80  
 
 81  
     /**
 82  
      * Iterate them all.
 83  
      * @return Iterator of labels
 84  
      * @see <a href="http://developer.github.com/v3/issues/labels/#list-labels-on-an-issue">List Labels on an Issue</a>
 85  
      */
 86  
     @NotNull(message = "iterable is never NULL")
 87  
     Iterable<Label> iterate();
 88  
 
 89  
     /**
 90  
      * Delete label by name.
 91  
      * @param name Name of the label to remove
 92  
      * @throws IOException If there is any I/O problem
 93  
      * @see <a href="http://developer.github.com/v3/issues/labels/#delete-a-label">Delete a Label</a>
 94  
      */
 95  
     void delete(@NotNull(message = "label name can't be NULL") String name)
 96  
         throws IOException;
 97  
 
 98  
     /**
 99  
      * Smart Labels with extra features.
 100  
      */
 101  
     @Immutable
 102  0
     @ToString
 103  
     @Loggable(Loggable.DEBUG)
 104  0
     @EqualsAndHashCode(of = "labels")
 105  
     final class Smart implements Labels {
 106  
         /**
 107  
          * Encapsulated labels.
 108  
          */
 109  
         private final transient Labels labels;
 110  
         /**
 111  
          * Public ctor.
 112  
          * @param lbl Labels
 113  
          */
 114  
         public Smart(
 115  
             @NotNull(message = "lbl can't be NULL") final Labels lbl
 116  1
         ) {
 117  1
             this.labels = lbl;
 118  1
         }
 119  
         /**
 120  
          * Label exists?
 121  
          * @param name Name of the label
 122  
          * @return TRUE if it exists
 123  
          */
 124  
         public boolean contains(
 125  
             @NotNull(message = "name cannot be NULL") final String name
 126  
         ) {
 127  2
             boolean contains = false;
 128  1
             for (final Label label : this.labels.iterate()) {
 129  0
                 if (label.name().equals(name)) {
 130  0
                     contains = true;
 131  0
                     break;
 132  
                 }
 133  0
             }
 134  1
             return contains;
 135  
         }
 136  
         /**
 137  
          * Create or get label.
 138  
          * @param name Name of the label
 139  
          * @return Label found or created
 140  
          * @throws IOException If there is any I/O problem
 141  
          */
 142  
         @NotNull(message = "label should never be NULL")
 143  
         public Label createOrGet(
 144  
             @NotNull(message = "name is not NULL") final String name
 145  
         ) throws IOException {
 146  0
             return this.createOrGet(name, "c0c0c0");
 147  
         }
 148  
         /**
 149  
          * Create or get label (with this explicit color).
 150  
          * @param name Name of the label
 151  
          * @param color Color to set (or modify)
 152  
          * @return Label found or created
 153  
          * @throws IOException If there is any I/O problem
 154  
          * @since 0.7
 155  
          */
 156  
         @NotNull(message = "label should not be NULL")
 157  
         public Label createOrGet(
 158  
             @NotNull(message = "name shouldn't be NULL") final String name,
 159  
             @NotNull(message = "color can't be NULL") final String color
 160  
         ) throws IOException {
 161  
             final Label.Smart label;
 162  2
             if (this.contains(name)) {
 163  0
                 label = new Label.Smart(this.labels.get(name));
 164  0
                 if (!label.color().equals(color)) {
 165  0
                     label.color(color);
 166  
                 }
 167  
             } else {
 168  1
                 label = new Label.Smart(this.labels.create(name, color));
 169  
             }
 170  1
             return label;
 171  
         }
 172  
         @Override
 173  
         @NotNull(message = "repo is never NULL")
 174  
         public Repo repo() {
 175  0
             return this.labels.repo();
 176  
         }
 177  
         @Override
 178  
         @NotNull(message = "label is never null")
 179  
         public Label create(
 180  
             @NotNull(message = "name can't be NULL") final String name,
 181  
             @NotNull(message = "color can't be NULL") final String color
 182  
         ) throws IOException {
 183  0
             return this.labels.create(name, color);
 184  
         }
 185  
         @Override
 186  
         @NotNull(message = "label is never NULL")
 187  
         public Label get(
 188  
             @NotNull(message = "name can't be NULL") final String name
 189  
         ) {
 190  0
             return this.labels.get(name);
 191  
         }
 192  
         @Override
 193  
         @NotNull(message = "Iterable of labels is never NULL")
 194  
         public Iterable<Label> iterate() {
 195  0
             return this.labels.iterate();
 196  
         }
 197  
         @Override
 198  
         public void delete(
 199  
             @NotNull(message = "name can't be NULL") final String name
 200  
         ) throws IOException {
 201  0
             this.labels.delete(name);
 202  0
         }
 203  
     }
 204  
 
 205  
 }