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