Android Open Source - nadia Date Time Interval






From Project

Back to project page nadia.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project nadia listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ashokgelal.samaya;
/*w ww  . j a  v  a  2  s .co  m*/
import com.ashokgelal.samaya.DateTime.DayOverflow;
import com.ashokgelal.samaya.DateTime.Unit;


/**
 Helper class for adding intervals of time. 
 The mental model of this class is similar to that of a car's odometer, except
 in reverse. 
 */
final class DateTimeInterval {

  /**  Constructor.  */
  DateTimeInterval(DateTime aFrom, DayOverflow aMonthOverflow){
    fFrom = aFrom;
    checkUnits();
    fYear = fFrom.getYear() == null ? 1 : fFrom.getYear();
    fMonth = fFrom.getMonth() == null ? 1 : fFrom.getMonth();
    fDay = fFrom.getDay() == null ? 1 : fFrom.getDay();
    fHour = fFrom.getHour() == null ? 0 : fFrom.getHour();
    fMinute = fFrom.getMinute() == null ? 0 : fFrom.getMinute();
    fSecond = fFrom.getSecond() == null ? 0 : fFrom.getSecond();
    fDayOverflow = aMonthOverflow;
  }
  
  DateTime plus(int aYear, int aMonth, int aDay, int aHour, int aMinute, int aSecond){
    return plusOrMinus(PLUS, aYear, aMonth, aDay, aHour, aMinute, aSecond);
  }
  
  DateTime minus(int aYear, int aMonth, int aDay, int aHour, int aMinute, int aSecond){
    return plusOrMinus(MINUS, aYear, aMonth, aDay, aHour, aMinute, aSecond);
  }
  
  // PRIVATE 
  private final DateTime fFrom;
  private boolean fIsPlus;
  private DateTime.DayOverflow fDayOverflow;
  
  private int fYearIncr;
  private int fMonthIncr;
  private int  fDayIncr;
  private int fHourIncr;
  private int fMinuteIncr;
  private int fSecondIncr;

  private Integer fYear;
  private Integer fMonth;
  private Integer fDay;
  private Integer fHour;
  private Integer fMinute;
  private Integer fSecond;

  private static final int MIN = 0;
  private static final int MAX = 9999;
  private static final boolean PLUS = true;
  private static final boolean MINUS = false;

  private void checkUnits(){
    boolean success = false;
    if(fFrom.unitsAllPresent(Unit.YEAR, Unit.MONTH, Unit.DAY, Unit.HOUR, Unit.MINUTE, Unit.SECOND) ){
      success = true;
    }
    else if( fFrom.unitsAllPresent(Unit.YEAR, Unit.MONTH, Unit.DAY) &&  fFrom.unitsAllAbsent(Unit.HOUR, Unit.MINUTE, Unit.SECOND) ){
      success = true;
    }
    else if ( fFrom.unitsAllAbsent(Unit.YEAR, Unit.MONTH, Unit.DAY) && fFrom.unitsAllPresent(Unit.HOUR, Unit.MINUTE, Unit.SECOND) ){
      success = true;
    }
    else {
      success = false;
    }
    if(! success ){
      throw new IllegalArgumentException("For interval calculations, DateTime must have year-month-day, or hour-minute-second, or both.");
    }
  }
  
  private DateTime plusOrMinus(boolean aIsPlus, Integer aYear, Integer aMonth, Integer aDay, Integer aHour, Integer aMinute, Integer aSecond){
    fIsPlus = aIsPlus;
    fYearIncr = aYear;
    fMonthIncr = aMonth;
    fDayIncr = aDay;
    fHourIncr = aHour;
    fMinuteIncr = aMinute;
    fSecondIncr = aSecond;
    
    checkRange(fYearIncr, "Year");
    checkRange(fMonthIncr, "Month");
    checkRange(fDayIncr, "Day");
    checkRange(fHourIncr, "Hour");
    checkRange(fMinuteIncr, "Minute");
    checkRange(fSecondIncr, "Second");
    
    changeYear();
    changeMonth();
    handleMonthOverflow();
    changeDay();
    changeHour();
    changeMinute();
    changeSecond();
    
    return new DateTime(fYear, fMonth, fDay, fHour, fMinute, fSecond, fFrom.getNanoseconds());
  }

  private void checkRange(Integer aValue, String aName) {
    if ( aValue <  MIN || aValue > MAX ) { 
      throw new IllegalArgumentException(aName + " is not in the range " + MIN + ".." + MAX); 
    }
  }
  
  private void changeYear(){
    if(fIsPlus){
      fYear = fYear + fYearIncr;
    }
    else {
      fYear = fFrom.getYear() - fYearIncr;
    }
    //the DateTime ctor will check the range of the year 
  }
  
  private void changeMonth(){
    int count = 0;
    while (count < fMonthIncr){
      stepMonth();
      count++;
    }
  }

  private void  changeDay(){
    int count = 0;
    while (count < fDayIncr){
      stepDay();
      count++;
    }
  }

  private  void changeHour(){
    int count = 0;
    while (count < fHourIncr){
      stepHour();
      count++;
    }
  }

  private void changeMinute(){
    int count = 0;
    while (count < fMinuteIncr){
      stepMinute();
      count++;
    }
  }
  
  private void changeSecond(){
    int count = 0;
    while (count < fSecondIncr){
      if(fIsPlus){
        fSecond = fSecond + 1;        
      }
      else {
        fSecond = fSecond - 1;        
      }
      if (fSecond > 59){
        fSecond = 0;
        stepMinute();
      }
      else if (fSecond < 0){
        fSecond = 59;
        stepMinute();
      }
      count++;
    }
  }
  
  private void stepYear() {
    if(fIsPlus) {
      fYear = fYear + 1;
    }
    else {
      fYear = fYear - 1;
    }
  }
  
  private void stepMonth() {
    if(fIsPlus){
      fMonth = fMonth + 1;
    }
    else {
      fMonth = fMonth - 1;
    }
    if(fMonth > 12) { 
      fMonth = 1;
      stepYear();
    }
    else if(fMonth < 1){
      fMonth = 12;
      stepYear();
    }
  }

  private void stepDay() {
    if(fIsPlus){
      fDay = fDay + 1;
    }
    else {
      fDay = fDay - 1;
    }
    if(fDay > numDaysInMonth()){
      fDay = 1;
      stepMonth();
    }
    else if (fDay < 1){
      fDay = numDaysInPreviousMonth();
      stepMonth();
    }
  }
  
  private int numDaysInMonth(){
    return DateTime.getNumDaysInMonth(fYear, fMonth);
  }
  
  private int numDaysInPreviousMonth(){
    int result = 0;
    if(fMonth > 1) {
      result = DateTime.getNumDaysInMonth(fYear, fMonth - 1);
    }
    else {
      result = DateTime.getNumDaysInMonth(fYear - 1 , 12);
    }
    return result;
  }
  
  
  private void stepHour() {
    if(fIsPlus){
      fHour = fHour + 1;      
    }
    else {
      fHour = fHour - 1;      
    }
    if(fHour > 23){
      fHour = 0;
      stepDay();
    }
    else if (fHour < 0){
      fHour = 23;
      stepDay();
    }
  }
  
  private void stepMinute() {
    if(fIsPlus){
      fMinute = fMinute + 1;      
    }
    else {
      fMinute = fMinute - 1;      
    }
    if(fMinute > 59){
      fMinute = 0;
      stepHour();
    }
    else if (fMinute < 0){
      fMinute = 59;
      stepHour();
    }
  }
  
  private void handleMonthOverflow(){
    int daysInMonth = numDaysInMonth();
    if( fDay > daysInMonth ){
      if(DayOverflow.Abort == fDayOverflow) {
        throw new RuntimeException(
          "Day Overflow: Year:" + fYear + " Month:" + fMonth + " has " + daysInMonth + " days, but day has value:" + fDay + 
          " To avoid these exceptions, please specify a different DayOverflow policy."
        );
      }
      else if (DayOverflow.FirstDay == fDayOverflow) {
        fDay = 1;
        stepMonth();
      }
      else if (DayOverflow.LastDay == fDayOverflow) {
        fDay = daysInMonth;
      }
      else if (DayOverflow.Spillover == fDayOverflow) {
        int overflowAmount = fDay - daysInMonth;
        fDay = overflowAmount;
        stepMonth();
      }
    }
  }

}




Java Source Code List

com.ashokgelal.samaya.DateTimeFormatter.java
com.ashokgelal.samaya.DateTimeInterval.java
com.ashokgelal.samaya.DateTimeParser.java
com.ashokgelal.samaya.DateTime.java
com.ashokgelal.samaya.ModelUtil.java
com.ashokgelal.samaya.Samaya.java
com.ashokgelal.samaya.TimeSpan.java
com.ashokgelal.samaya.ToStringUtil.java
com.ashokgelal.samaya.Util.java
com.redarctic.nadia.baseengine.BaseGameState.java
com.redarctic.nadia.baseengine.ColorPallete.java
com.redarctic.nadia.baseengine.DrawableGameComponent.java
com.redarctic.nadia.baseengine.DrawableObject.java
com.redarctic.nadia.baseengine.GameComponent.java
com.redarctic.nadia.baseengine.GameStateManager.java
com.redarctic.nadia.baseengine.GameState.java
com.redarctic.nadia.baseengine.GameSurfaceView.java
com.redarctic.nadia.baseengine.GameSurface.java
com.redarctic.nadia.baseengine.SpriteFont.java
com.redarctic.nadia.baseengine.Sprite.java
com.redarctic.nadia.baseengine.character.BaseModifier.java
com.redarctic.nadia.baseengine.character.Classes.java
com.redarctic.nadia.baseengine.character.FormulaModifierException.java
com.redarctic.nadia.baseengine.character.Player.java
com.redarctic.nadia.baseengine.item.ArmorLocation.java
com.redarctic.nadia.baseengine.item.Armor.java
com.redarctic.nadia.baseengine.item.BaseItem.java
com.redarctic.nadia.baseengine.item.ItemManager.java
com.redarctic.nadia.baseengine.item.ItemType.java
com.redarctic.nadia.baseengine.item.Shield.java
com.redarctic.nadia.baseengine.item.Weapon.java
com.redarctic.nadia.baseengine.item.WeaponsType.java
com.redarctic.nadia.baseengine.sprite.AnimatedSprite.java
com.redarctic.nadia.baseengine.sprite.AnimationException.java
com.redarctic.nadia.baseengine.sprite.AnimationKey.java
com.redarctic.nadia.baseengine.sprite.Animation.java
com.redarctic.nadia.baseengine.tileengine.Camera.java
com.redarctic.nadia.baseengine.tileengine.Engine.java
com.redarctic.nadia.baseengine.tileengine.MapLayer.java
com.redarctic.nadia.baseengine.tileengine.TileException.java
com.redarctic.nadia.baseengine.tileengine.TileMap.java
com.redarctic.nadia.baseengine.tileengine.Tile.java
com.redarctic.nadia.baseengine.tileengine.Tileset.java
com.redarctic.nadia.baseengine.world.Level.java
com.redarctic.nadia.baseengine.world.World.java
com.redarctic.nadia.collision.Circle.java
com.redarctic.nadia.collision.CollisionPolygon.java
com.redarctic.nadia.collision.ConvexPolygonException.java
com.redarctic.nadia.collision.Rectangle.java
com.redarctic.nadia.controls.ButtonPad.java
com.redarctic.nadia.controls.DirectionalPad.java
com.redarctic.nadia.controls.menu.CharacterDialog.java
com.redarctic.nadia.controls.menu.ControlBorder.java
com.redarctic.nadia.controls.menu.ControlManager.java
com.redarctic.nadia.controls.menu.Control.java
com.redarctic.nadia.controls.menu.Label.java
com.redarctic.nadia.controls.menu.LeftRightSelector.java
com.redarctic.nadia.controls.menu.LinkLabel.java
com.redarctic.nadia.controls.menu.Menu.java
com.redarctic.nadia.controls.menu.PictureBox.java
com.redarctic.nadia.controls.menu.SimpleConversation.java
com.redarctic.nadia.ext.MathHelper.java
com.redarctic.nadia.ext.StringHelper.java
com.redarctic.nadia.ext.simplesignalslot.ISignalProvider.java
com.redarctic.nadia.ext.simplesignalslot.ISlotProvider.java
com.redarctic.nadia.ext.simplesignalslot.PrimitiveWrapper.java
com.redarctic.nadia.ext.simplesignalslot.SignalSlotMap.java
com.redarctic.nadia.ext.simplesignalslot.SignalSlotPair.java
com.redarctic.nadia.ext.simplesignalslot.SlotProviderMethodPair.java