Coverage Report - org.jtheque.films.view.impl.models.spinner.SpinnerDurationModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SpinnerDurationModel
0 %
0/33
0 %
0/8
1.667
 
 1  
 package org.jtheque.films.view.impl.models.spinner;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.log.ILoggingManager;
 21  
 
 22  
 import javax.swing.AbstractSpinnerModel;
 23  
 import java.util.regex.Pattern;
 24  
 
 25  
 /**
 26  
  * A spinner model for the duration.
 27  
  *
 28  
  * @author Baptiste Wicht
 29  
  */
 30  
 public final class SpinnerDurationModel extends AbstractSpinnerModel {
 31  
     private String value;
 32  
 
 33  
     private static final int MINUTES_IN_A_HOUR = 60;
 34  
 
 35  0
     private static final Pattern SEPARATOR_PATTERN = Pattern.compile(":");
 36  
 
 37  
     /**
 38  
      * Construct a new SpinnerDurationModel.
 39  
      *
 40  
      * @param value The default value.
 41  
      */
 42  
     public SpinnerDurationModel(String value) {
 43  0
         super();
 44  
 
 45  0
         this.value = value;
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Returns the int representation of the current value.
 50  
      *
 51  
      * @return The int value of the date
 52  
      */
 53  
     public int intValue() {
 54  0
         String[] parts = SEPARATOR_PATTERN.split(value);
 55  0
         int hours = Integer.parseInt(parts[0]);
 56  0
         int minutes = Integer.parseInt(parts[1]);
 57  
 
 58  0
         return hours * MINUTES_IN_A_HOUR + minutes;
 59  
     }
 60  
 
 61  
     @Override
 62  
     public Object getValue() {
 63  0
         return value;
 64  
     }
 65  
 
 66  
     @Override
 67  
     public void setValue(Object value) {
 68  0
         this.value = (String) value;
 69  0
         fireStateChanged();
 70  0
     }
 71  
 
 72  
     @Override
 73  
     public Object getNextValue() {
 74  0
         String[] parts = SEPARATOR_PATTERN.split(value);
 75  0
         int hours = Integer.parseInt(parts[0]);
 76  0
         int minutes = Integer.parseInt(parts[1]);
 77  
         String strMinutes;
 78  
 
 79  0
         ++minutes;
 80  
 
 81  0
         if (minutes == MINUTES_IN_A_HOUR) {
 82  0
             ++hours;
 83  0
             minutes = 0;
 84  
         }
 85  
 
 86  0
         strMinutes = minutes < 10 ? "0" + minutes : Integer.toString(minutes);
 87  
 
 88  0
         Managers.getManager(ILoggingManager.class).getLogger(getClass()).debug("Next value is {}:{}", hours, minutes);
 89  
 
 90  0
         return hours + ":" + strMinutes;
 91  
     }
 92  
 
 93  
     @Override
 94  
     public Object getPreviousValue() {
 95  0
         Managers.getManager(ILoggingManager.class).getLogger(getClass()).debug("Get previous value of {}", value);
 96  
 
 97  0
         String[] parts = SEPARATOR_PATTERN.split(value);
 98  0
         int hours = Integer.parseInt(parts[0]);
 99  0
         int minutes = Integer.parseInt(parts[1]);
 100  
         String strMinutes;
 101  
 
 102  0
         --minutes;
 103  
 
 104  0
         if (minutes == -1) {
 105  0
             --hours;
 106  0
             minutes = MINUTES_IN_A_HOUR - 1;
 107  
         }
 108  
 
 109  0
         strMinutes = minutes < 10 ? "0" + minutes : Integer.toString(minutes);
 110  
 
 111  0
         Managers.getManager(ILoggingManager.class).getLogger(getClass()).debug("Previous value is {}:{}", hours, minutes);
 112  
 
 113  0
         return hours + ":" + strMinutes;
 114  
     }
 115  
 }