⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hiloproxy.java

📁 著名的uncle Bob的Agile software development的代码
💻 JAVA
字号:

package persistence;

import java.util.Date;
import java.text.SimpleDateFormat;
import api.PersistentImp;
import wmsdataimp.HiLoDataImp;

public class HiLoProxy implements wmsdata.HiLoData
{
  private HiLoDataImp itsHiLoData;

  private PersistentImp itsPI;

  private String itsType;
  private String itsStorageKey;

  public HiLoProxy(String type, Date aDate) 
             throws wmsdata.NoSuchDateException
  {
    itsType = type;
    itsStorageKey = calculateStorageKey(new Date());

    itsPI = api.Scope.stationToolkit.getPersistentImp();

    try
    {
      itsHiLoData = (HiLoDataImp)itsPI.retrieveObject(itsStorageKey);
    }
    catch(api.RetrieveException re)
    {
      throw new wmsdata.NoSuchDateException(itsStorageKey);
    }
  }

  public HiLoProxy(String type, 
                   Date aDate, 
                   double initialValue) 
  {
    itsType = type;
    itsStorageKey = calculateStorageKey(aDate);

    itsPI = api.Scope.stationToolkit.getPersistentImp();

    try
    {
      itsHiLoData = (HiLoDataImp)itsPI.retrieveObject(itsStorageKey);
      itsHiLoData.currentReading(initialValue, aDate.getTime());
    }
    catch(api.RetrieveException re)
    {
      System.out.println("Could not retrieve");
      itsHiLoData = new HiLoDataImp(initialValue, aDate.getTime());
      store();
      System.exit(1);
    }
  }

  private String calculateStorageKey(Date theDate)
  {
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyyy");

    return (itsType + 
            "HiLo" + 
            dateFormat.format(theDate));
  }

  public boolean currentReading(double value, long time)
  {
    boolean rtnValue = itsHiLoData.currentReading(value, time);
    if(rtnValue == true)
    {
      store();
    }

    return rtnValue;
  }

  private void store()
  {
    try
    {
      itsPI.store(itsStorageKey, itsHiLoData);
    }
    catch(api.StoreException se)
    {
      // log the error somehow
    }
  }

  public void newDay(double initialValue, long time)
  {
    store();
    itsHiLoData.newDay(initialValue, time);
    itsStorageKey = calculateStorageKey(new Date(time));
    store();
  }

  public double getHighValue()
  { return itsHiLoData.getHighValue(); }
  public double getLowValue()
  { return itsHiLoData.getLowValue(); }
  public long getHighTime()
  { return itsHiLoData.getHighTime(); }
  public long getLowTime()
  { return itsHiLoData.getLowTime(); }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -