Coverage Report - com.jcabi.github.IssueLabels
 
Classes in this File Line Coverage Branch Coverage Complexity
IssueLabels
N/A
N/A
1.789
IssueLabels$Smart
52%
36/69
42%
16/38
1.789
IssueLabels$Smart$AjcClosure1
100%
1/1
N/A
1.789
IssueLabels$Smart$AjcClosure11
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure13
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure15
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure17
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure19
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure21
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure23
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure3
100%
1/1
N/A
1.789
IssueLabels$Smart$AjcClosure5
0%
0/1
N/A
1.789
IssueLabels$Smart$AjcClosure7
100%
1/1
N/A
1.789
IssueLabels$Smart$AjcClosure9
100%
1/1
N/A
1.789
 
 1  2
 /**
 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 java.util.Collection;
 36  
 import java.util.Collections;
 37  
 import java.util.LinkedList;
 38  
 import javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import lombok.ToString;
 41  
 
 42  
 /**
 43  
  * Github labels of an issue.
 44  
  *
 45  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 46  
  * @version $Id$
 47  
  * @since 0.1
 48  
  * @see <a href="http://developer.github.com/v3/issues/labels/">Labels API</a>
 49  
  */
 50  
 @Immutable
 51  
 @SuppressWarnings("PMD.TooManyMethods")
 52  
 public interface IssueLabels {
 53  
 
 54  
     /**
 55  
      * The issue we're in.
 56  
      * @return Issue
 57  
      */
 58  
     @NotNull(message = "issue is never NULL")
 59  
     Issue issue();
 60  
 
 61  
     /**
 62  
      * Add new labels.
 63  
      * @param labels The labels to add
 64  
      * @throws IOException If there is any I/O problem
 65  
      * @see <a href="http://developer.github.com/v3/issues/labels/#add-labels-to-an-issue">Add labels to an issue</a>
 66  
      */
 67  
     void add(@NotNull(message = "iterable of label names can't be NULL")
 68  
         Iterable<String> labels) throws IOException;
 69  
 
 70  
     /**
 71  
      * Replace all labels.
 72  
      * @param labels The labels to save
 73  
      * @throws IOException If there is any I/O problem
 74  
      * @see <a href="http://developer.github.com/v3/issues/labels/#replace-all-labels-for-an-issue">Replace all labels for an issue</a>
 75  
      */
 76  
     void replace(@NotNull(message = "iterable of label names can't be NULL")
 77  
         Iterable<String> labels) throws IOException;
 78  
 
 79  
     /**
 80  
      * Iterate them all.
 81  
      * @return Iterator of labels
 82  
      * @see <a href="http://developer.github.com/v3/issues/labels/#list-labels-on-an-issue">List Labels on an Issue</a>
 83  
      */
 84  
     @NotNull(message = "iterable is never NULL")
 85  
     Iterable<Label> iterate();
 86  
 
 87  
     /**
 88  
      * Remove label by name.
 89  
      * @param name Name of the label to remove
 90  
      * @throws IOException If there is any I/O problem
 91  
      * @see <a href="http://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue">Remove a Label from an Issue</a>
 92  
      */
 93  
     void remove(@NotNull(message = "label name can't be NULL") String name)
 94  
         throws IOException;
 95  
 
 96  
     /**
 97  
      * Remove all labels.
 98  
      * @throws IOException If there is any I/O problem
 99  
      * @see <a href="http://developer.github.com/v3/issues/labels/#remove-all-labels-from-an-issue">Remove all labels from an issue</a>
 100  
      */
 101  
     void clear() throws IOException;
 102  
 
 103  
     /**
 104  
      * Smart IssueLabels with extra features.
 105  
      */
 106  
     @Immutable
 107  0
     @ToString
 108  
     @Loggable(Loggable.DEBUG)
 109  0
     @EqualsAndHashCode(of = "labels")
 110  
     final class Smart implements IssueLabels {
 111  
         /**
 112  
          * Encapsulated labels.
 113  
          */
 114  
         private final transient IssueLabels labels;
 115  
         /**
 116  
          * Public ctor.
 117  
          * @param lbl Labels
 118  
          */
 119  
         public Smart(
 120  
             @NotNull(message = "lbl can't be NULL") final IssueLabels lbl
 121  6
         ) {
 122  6
             this.labels = lbl;
 123  6
         }
 124  
         /**
 125  
          * Label exists?
 126  
          * @param name Name of the label
 127  
          * @return TRUE if it exists
 128  
          */
 129  
         public boolean contains(
 130  
             @NotNull(message = "name can't be NULL") final String name
 131  
         ) {
 132  4
             boolean contains = false;
 133  2
             for (final Label label : this.labels.iterate()) {
 134  3
                 if (label.name().equals(name)) {
 135  1
                     contains = true;
 136  1
                     break;
 137  
                 }
 138  2
             }
 139  2
             return contains;
 140  
         }
 141  
         /**
 142  
          * Get label by name (runtime exception if absent).
 143  
          * @param name Name of the label
 144  
          * @return Label found (exception if not found)
 145  
          * @since 0.7
 146  
          */
 147  
         @NotNull(message = "label is never NULL")
 148  
         public Label get(
 149  
             @NotNull(message = "name cant be NULL") final String name
 150  
         ) {
 151  4
             Label label = null;
 152  2
             int count = 0;
 153  2
             for (final Label opt : this.labels.iterate()) {
 154  1
                 if (opt.name().equals(name)) {
 155  1
                     label = opt;
 156  1
                     break;
 157  
                 }
 158  0
                 ++count;
 159  0
             }
 160  2
             if (label == null) {
 161  1
                 throw new IllegalArgumentException(
 162  
                     String.format(
 163  
                         // @checkstyle LineLength (1 line)
 164  
                         "label '%s' not found among %d others, use #contains() first",
 165  
                         name, count
 166  
                     )
 167  
                 );
 168  
             }
 169  1
             return label;
 170  
         }
 171  
         /**
 172  
          * Add label if it is absent, don't touch its color if exists.
 173  
          * @param name Name of the label
 174  
          * @return TRUE if it was added
 175  
          * @throws IOException If there is any I/O problem
 176  
          */
 177  
         public boolean addIfAbsent(
 178  
             @NotNull(message = "name should not be NULL") final String name
 179  
         ) throws IOException {
 180  
             final boolean added;
 181  0
             if (this.contains(name)) {
 182  0
                 added = false;
 183  
             } else {
 184  0
                 new Labels.Smart(this.labels.issue().repo().labels())
 185  
                     .createOrGet(name);
 186  0
                 this.labels.add(Collections.singletonList(name));
 187  0
                 added = true;
 188  
             }
 189  0
             return added;
 190  
         }
 191  
         /**
 192  
          * Add label if it is absent, and set its color in any case.
 193  
          * @param name Name of the label
 194  
          * @param color Color to set
 195  
          * @return TRUE if it was added
 196  
          * @throws IOException If there is any I/O problem
 197  
          * @since 0.7
 198  
          */
 199  
         public boolean addIfAbsent(
 200  
             @NotNull(message = "name is never NULL") final String name,
 201  
             @NotNull(message = "color can't be NULL") final String color
 202  
         )
 203  
             throws IOException {
 204  2
             Label label = null;
 205  1
             for (final Label opt : new Bulk<Label>(this.labels.iterate())) {
 206  0
                 if (opt.name().equals(name)) {
 207  0
                     label = opt;
 208  0
                     break;
 209  
                 }
 210  0
             }
 211  1
             boolean added = false;
 212  1
             if (label == null) {
 213  1
                 added = true;
 214  1
                 label = new Labels.Smart(this.labels.issue().repo().labels())
 215  
                     .createOrGet(name, color);
 216  1
                 this.labels.add(Collections.singletonList(name));
 217  
             }
 218  1
             final Label.Smart smart = new Label.Smart(label);
 219  1
             if (!smart.color().equals(color)) {
 220  0
                 smart.color(color);
 221  
             }
 222  1
             return added;
 223  
         }
 224  
         /**
 225  
          * Select all labels with the given color.
 226  
          * @param color Color
 227  
          * @return Collection of labels with the provided color
 228  
          * @throws IOException If there is any I/O problem
 229  
          * @since 0.7
 230  
          */
 231  
         @NotNull(message = "collection is never NULL")
 232  
         public Collection<Label> findByColor(
 233  
             @NotNull(message = "color can't be NULL") final String color
 234  
         )
 235  
             throws IOException {
 236  2
             final Collection<Label> found = new LinkedList<Label>();
 237  1
             for (final Label label : this.labels.iterate()) {
 238  2
                 if (new Label.Smart(label).color().equals(color)) {
 239  1
                     found.add(label);
 240  
                 }
 241  2
             }
 242  1
             return found;
 243  
         }
 244  
         /**
 245  
          * Remove label if it exists (do nothing otherwise).
 246  
          * @param name Label to remove
 247  
          * @return TRUE if it was removed, FALSE otherwise
 248  
          * @throws IOException If there is any I/O problem
 249  
          * @since 0.7
 250  
          */
 251  
         public boolean removeIfExists(
 252  
             @NotNull(message = "name cannot be NULL") final String name
 253  
         )
 254  
             throws IOException {
 255  0
             boolean removed = false;
 256  0
             for (final Label label : this.labels.iterate()) {
 257  0
                 if (label.name().equals(name)) {
 258  0
                     this.remove(name);
 259  0
                     removed = true;
 260  0
                     break;
 261  
                 }
 262  0
             }
 263  0
             return removed;
 264  
         }
 265  
         @Override
 266  
         @NotNull(message = "issue is never NULL")
 267  
         public Issue issue() {
 268  0
             return this.labels.issue();
 269  
         }
 270  
         @Override
 271  
         public void add(
 272  
             @NotNull(message = "names can't be NULL")
 273  
             final Iterable<String> names
 274  
         ) throws IOException {
 275  0
             this.labels.add(names);
 276  0
         }
 277  
         @Override
 278  
         public void replace(
 279  
             @NotNull(message = "names can't be NULL")
 280  
             final Iterable<String> names
 281  
         ) throws IOException {
 282  0
             this.labels.replace(names);
 283  0
         }
 284  
         @Override
 285  
         @NotNull(message = "Iterable of labels is never NULL")
 286  
         public Iterable<Label> iterate() {
 287  0
             return this.labels.iterate();
 288  
         }
 289  
         @Override
 290  
         public void remove(
 291  
             @NotNull(message = "name can't be NULL") final String name
 292  
         ) throws IOException {
 293  0
             this.labels.remove(name);
 294  0
         }
 295  
         @Override
 296  
         public void clear() throws IOException {
 297  0
             this.labels.clear();
 298  0
         }
 299  
     }
 300  
 }