Coverage Report - com.jcabi.github.Releases
 
Classes in this File Line Coverage Branch Coverage Complexity
Releases
N/A
N/A
1.462
Releases$Smart
66%
20/30
25%
5/20
1.462
Releases$Smart$AjcClosure1
0%
0/1
N/A
1.462
Releases$Smart$AjcClosure11
100%
1/1
N/A
1.462
Releases$Smart$AjcClosure13
100%
1/1
N/A
1.462
Releases$Smart$AjcClosure3
100%
1/1
N/A
1.462
Releases$Smart$AjcClosure5
0%
0/1
N/A
1.462
Releases$Smart$AjcClosure7
0%
0/1
N/A
1.462
Releases$Smart$AjcClosure9
0%
0/1
N/A
1.462
 
 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 javax.validation.constraints.NotNull;
 36  
 import lombok.EqualsAndHashCode;
 37  
 import lombok.ToString;
 38  
 
 39  
 /**
 40  
  * Github Releases.
 41  
  *
 42  
  * @author Paul Polishchuk (ppol@ua.fm)
 43  
  * @version $Id$
 44  
  * @since 0.8
 45  
  */
 46  
 @Immutable
 47  
 public interface Releases {
 48  
     /**
 49  
      * Owner of them.
 50  
      * @return Repo
 51  
      */
 52  
     @NotNull(message = "repository is never NULL")
 53  
     Repo repo();
 54  
 
 55  
     /**
 56  
      * Iterate them all.
 57  
      * @return Iterator of releases
 58  
      * @see <a href="http://developer.github.com/v3/repos/releases/#list">List</a>
 59  
      */
 60  
     @NotNull(message = "iterable is never NULL")
 61  
     Iterable<Release> iterate();
 62  
 
 63  
     /**
 64  
      * Get a single release.
 65  
      * @param number Release id
 66  
      * @return Release
 67  
      * @see <a href="http://developer.github.com/v3/repos/releases/#get-a-single-release">Get a single release</a>
 68  
      */
 69  
     @NotNull(message = "release is never NULL")
 70  
     Release get(int number);
 71  
 
 72  
     /**
 73  
      * Create new release.
 74  
      * @param tag The name of the tag
 75  
      * @return Release just created
 76  
      * @throws java.io.IOException If there is any I/O problem
 77  
      * @see <a href="http://developer.github.com/v3/repos/releases/#create-a-release">Create an Release</a>
 78  
      */
 79  
     @NotNull(message = "release is never NULL")
 80  
     Release create(
 81  
         @NotNull(message = "release tag is never NULL") String tag)
 82  
         throws IOException;
 83  
 
 84  
     /**
 85  
      * Remove a release.
 86  
      *
 87  
      * @param number ID of the release to remove.
 88  
      * @throws IOException If an IO problem occurs.
 89  
      * @see <a href="http://developer.github.com/v3/repos/releases/#delete-a-release">Delete a release.</a>
 90  
      */
 91  
     void remove(int number) throws IOException;
 92  
 
 93  
     /**
 94  
      * Smart releases.
 95  
      * @since 0.17
 96  
      */
 97  
     @Immutable
 98  0
     @ToString
 99  
     @Loggable(Loggable.DEBUG)
 100  0
     @EqualsAndHashCode(of = "releases")
 101  
     final class Smart implements Releases {
 102  
         /**
 103  
          * Encapsulated releases.
 104  
          */
 105  
         private final transient Releases releases;
 106  
 
 107  
         /**
 108  
          * Public CTOR.
 109  
          * @param original Original releases
 110  
          */
 111  
         public Smart(
 112  
             @NotNull(message = "original can't be NULL") final Releases original
 113  2
         ) {
 114  2
             this.releases = original;
 115  2
         }
 116  
         @Override
 117  
         public Repo repo() {
 118  0
             return this.releases.repo();
 119  
         }
 120  
         @Override
 121  
         public Iterable<Release> iterate() {
 122  4
             return this.releases.iterate();
 123  
         }
 124  
         @Override
 125  
         public Release get(final int number) {
 126  0
             return this.releases.get(number);
 127  
         }
 128  
         @Override
 129  
         public Release create(final String tag) throws IOException {
 130  0
             return this.releases.create(tag);
 131  
         }
 132  
         @Override
 133  
         public void remove(final int number) throws IOException {
 134  0
             this.releases.remove(number);
 135  0
         }
 136  
         /**
 137  
          * This release exists by the tag.
 138  
          * @param tag The tag
 139  
          * @return TRUE if it already exists
 140  
          * @throws IOException If fails
 141  
          */
 142  
         public boolean exists(final String tag) throws IOException {
 143  2
             boolean exists = false;
 144  1
             final Iterable<Release.Smart> rels = new Smarts<Release.Smart>(
 145  
                 this.iterate()
 146  
             );
 147  1
             for (final Release.Smart rel : rels) {
 148  1
                 if (rel.tag().equals(tag)) {
 149  1
                     exists = true;
 150  1
                     break;
 151  
                 }
 152  0
             }
 153  1
             return exists;
 154  
         }
 155  
         /**
 156  
          * Find release by the tag (runtime exception if not found).
 157  
          * @param tag The tag
 158  
          * @return Release found
 159  
          * @throws IOException If fails
 160  
          */
 161  
         public Release find(final String tag) throws IOException {
 162  2
             Release found = null;
 163  1
             final Iterable<Release.Smart> rels = new Smarts<Release.Smart>(
 164  
                 this.iterate()
 165  
             );
 166  1
             for (final Release.Smart rel : rels) {
 167  1
                 if (rel.tag().equals(tag)) {
 168  1
                     found = rel;
 169  1
                     break;
 170  
                 }
 171  0
             }
 172  1
             if (found == null) {
 173  0
                 throw new IllegalArgumentException(
 174  
                     String.format("release not found by tag '%s'", tag)
 175  
                 );
 176  
             }
 177  1
             return found;
 178  
         }
 179  
     }
 180  
 }