Coverage Report - org.jtheque.utils.bean.Duration
 
Classes in this File Line Coverage Branch Coverage Complexity
Duration
95%
23/24
70%
7/10
1.7
 
 1  
 package org.jtheque.utils.bean;
 2  
 
 3  
 import org.jtheque.utils.Constants;
 4  
 
 5  
 /*
 6  
  * This file is part of JTheque.
 7  
  *
 8  
  * JTheque is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU General Public License as published by
 10  
  * the Free Software Foundation, either version 3 of the License.
 11  
  *
 12  
  * JTheque is distributed in the hope that it will be useful,
 13  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  
  * GNU General Public License for more details.
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License
 18  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 19  
  */
 20  
 
 21  
 /**
 22  
  * This class represent a duration of time. A Duration is a period of hours and minutes.
 23  
  *
 24  
  * @author Baptiste Wicht
 25  
  */
 26  
 public final class Duration {
 27  
     private int minutes;
 28  
     private int hours;
 29  
 
 30  
     /**
 31  
      * Construct a new duration with a certain number of hours and a certain number of minutes.
 32  
      *
 33  
      * @param hours   The hours of the duration
 34  
      * @param minutes The minutes of the duration
 35  
      */
 36  
     private Duration(int hours, int minutes) {
 37  22
         super();
 38  
 
 39  22
         this.hours = hours;
 40  22
         this.minutes = minutes;
 41  22
     }
 42  
 
 43  
     /**
 44  
      * Construct a new duration with only minutes. If there is more than 60 minutes, we dispatch the
 45  
      * time to the hours.
 46  
      *
 47  
      * @param minutes The minutes of the duration
 48  
      */
 49  
     public Duration(int minutes) {
 50  16
         this(minutes / Constants.MINUTES_IN_AN_HOUR, minutes % Constants.MINUTES_IN_AN_HOUR);
 51  16
     }
 52  
 
 53  
     /**
 54  
      * Construct a new empty duration.
 55  
      */
 56  
     public Duration() {
 57  6
         this(0, 0);
 58  6
     }
 59  
 
 60  
     /**
 61  
      * Returns the hours of the duration.
 62  
      *
 63  
      * @return The number of hours in the duration
 64  
      */
 65  
     public int getHours() {
 66  14
         return hours;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Set the hours of the duration.
 71  
      *
 72  
      * @param hours The new number of hours in the duration
 73  
      */
 74  
     public void setHours(int hours) {
 75  4
         this.hours = hours;
 76  4
     }
 77  
 
 78  
     /**
 79  
      * Returns the minutes of the duration.
 80  
      *
 81  
      * @return The number of minutes in the duration
 82  
      */
 83  
     public int getMinutes() {
 84  14
         return minutes;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Set the minutes of the duration.
 89  
      *
 90  
      * @param minutes The new number of minutes
 91  
      */
 92  
     public void setMinutes(int minutes) {
 93  4
         this.minutes = minutes;
 94  4
     }
 95  
 
 96  
     @Override
 97  
     public String toString() {
 98  4
         return hours + ":" + minutes;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public boolean equals(Object o) {
 103  14
         if (this == o) {
 104  2
             return true;
 105  
         }
 106  
 
 107  12
         if (o == null || getClass() != o.getClass()) {
 108  0
             return false;
 109  
         }
 110  
 
 111  12
         Duration duration = (Duration) o;
 112  
 
 113  12
         if (hours != duration.hours) {
 114  8
             return false;
 115  
         }
 116  
 
 117  4
         return minutes == duration.minutes;
 118  
 
 119  
     }
 120  
 
 121  
     @Override
 122  
     public int hashCode() {
 123  12
         return Constants.HASH_CODE_START +
 124  
                 Constants.HASH_CODE_PRIME * minutes +
 125  
                 Constants.HASH_CODE_PRIME * hours;
 126  
     }
 127  
 }