001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.issue.internal;
021
022import java.io.Serializable;
023
024public class WorkDayDuration implements Serializable {
025
026  private static final int DAY = 10000;
027  private static final int HOUR = 100;
028  private static final int MINUTE = 1;
029
030  private int days;
031  private int hours;
032  private int minutes;
033
034  private WorkDayDuration(int minutes, int hours, int days) {
035    this.minutes = minutes;
036    this.hours = hours;
037    this.days = days;
038  }
039
040  private WorkDayDuration(long durationInLong) {
041    long time = durationInLong;
042    Long currentTime = time / DAY;
043    if (currentTime > 0) {
044      this.days = currentTime.intValue();
045      time = time - (currentTime * DAY);
046    }
047
048    currentTime = time / HOUR;
049    if (currentTime > 0) {
050      this.hours = currentTime.intValue();
051      time = time - (currentTime * HOUR);
052    }
053
054    currentTime = time / MINUTE;
055    if (currentTime > 0) {
056      this.minutes = currentTime.intValue();
057    }
058  }
059
060  public static WorkDayDuration of(int minutes, int hours, int days) {
061    return new WorkDayDuration(minutes, hours, days);
062  }
063
064  public static WorkDayDuration fromLong(long durationInLong) {
065    return new WorkDayDuration(durationInLong);
066  }
067
068  public long toLong() {
069    return days * DAY + hours * HOUR + minutes * MINUTE;
070  }
071
072  public int days() {
073    return days;
074  }
075
076  public int hours() {
077    return hours;
078  }
079
080  public int minutes() {
081    return minutes;
082  }
083
084  @Override
085  public boolean equals(Object o) {
086    if (this == o) {
087      return true;
088    }
089    if (o == null || getClass() != o.getClass()) {
090      return false;
091    }
092    WorkDayDuration workDayDuration = (WorkDayDuration) o;
093    if (days != workDayDuration.days) {
094      return false;
095    }
096    if (hours != workDayDuration.hours) {
097      return false;
098    }
099    if (minutes != workDayDuration.minutes) {
100      return false;
101    }
102    return true;
103  }
104
105  @Override
106  public int hashCode() {
107    int result = Integer.valueOf(days).hashCode();
108    result = 29 * result + Integer.valueOf(hours).hashCode();
109    result = 27 * result + Integer.valueOf(minutes).hashCode();
110    return result;
111  }
112
113}