/** Set up a quarter equal to Q1 1900. Request the previous quarter, it should be null. */
  public void testClone() {

    TimePeriodValues series = new TimePeriodValues("Test Series");

    RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
    try {
      series.add(jan1st2002, new Integer(42));
    } catch (SeriesException e) {
      System.err.println("Problem adding to collection.");
    }

    TimePeriodValues clone = null;
    try {
      clone = (TimePeriodValues) series.clone();
      clone.setKey("Clone Series");
      try {
        clone.update(0, new Integer(10));
      } catch (SeriesException e) {
        System.err.println("Problem updating series.");
      }
    } catch (CloneNotSupportedException e) {
      assertTrue(false);
    }

    int seriesValue = series.getValue(0).intValue();
    int cloneValue = clone.getValue(0).intValue();

    assertEquals(42, seriesValue);
    assertEquals(10, cloneValue);
    assertEquals("Test Series", series.getKey());
    assertEquals("Clone Series", clone.getKey());
  }
 /**
  * Tests the series for equality with another object.
  *
  * @param obj the object (<code>null</code> permitted).
  * @return <code>true</code> or <code>false</code>.
  */
 @Override
 public boolean equals(Object obj) {
   if (obj == this) {
     return true;
   }
   if (!(obj instanceof TimePeriodValues)) {
     return false;
   }
   if (!super.equals(obj)) {
     return false;
   }
   TimePeriodValues that = (TimePeriodValues) obj;
   if (!ObjectUtilities.equal(this.getDomainDescription(), that.getDomainDescription())) {
     return false;
   }
   if (!ObjectUtilities.equal(this.getRangeDescription(), that.getRangeDescription())) {
     return false;
   }
   int count = getItemCount();
   if (count != that.getItemCount()) {
     return false;
   }
   for (int i = 0; i < count; i++) {
     if (!getDataItem(i).equals(that.getDataItem(i))) {
       return false;
     }
   }
   return true;
 }
 /**
  * Returns the x-value for the specified series and item.
  *
  * @param series the series (zero-based index).
  * @param item the item (zero-based index).
  * @return The x-value for the specified series and item.
  */
 @Override
 public Number getX(int series, int item) {
   TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
   TimePeriodValue dp = ts.getDataItem(item);
   TimePeriod period = dp.getPeriod();
   return new Long(getX(period));
 }
  /** Add a value to series A for 1999. It should be added at index 0. */
  public void testAddValue() {

    TimePeriodValues tpvs = new TimePeriodValues("Test");
    try {
      tpvs.add(new Year(1999), new Integer(1));
    } catch (SeriesException e) {
      System.err.println("Problem adding to series.");
    }

    int value = tpvs.getValue(0).intValue();
    assertEquals(1, value);
  }
  /** Some checks for the add() methods. */
  public void testAdd() {
    TimePeriodValues tpv = new TimePeriodValues("Test");
    MySeriesChangeListener listener = new MySeriesChangeListener();
    tpv.addChangeListener(listener);
    tpv.add(new TimePeriodValue(new SimpleTimePeriod(new Date(1L), new Date(3L)), 99.0));
    assertEquals(99.0, tpv.getValue(0).doubleValue(), EPSILON);
    assertEquals(tpv, listener.getLastEvent().getSource());

    // a null item should throw an IllegalArgumentException
    boolean pass = false;
    try {
      tpv.add((TimePeriodValue) null);
    } catch (IllegalArgumentException e) {
      pass = true;
    }
    assertTrue(pass);
  }
  /**
   * Creates a new instance by copying a subset of the data in this collection.
   *
   * @param start the index of the first item to copy.
   * @param end the index of the last item to copy.
   * @return A copy of a subset of the items.
   * @throws CloneNotSupportedException if there is a cloning problem.
   */
  public TimePeriodValues createCopy(int start, int end) throws CloneNotSupportedException {

    TimePeriodValues copy = (TimePeriodValues) super.clone();

    copy.data = new ArrayList();
    if (this.data.size() > 0) {
      for (int index = start; index <= end; index++) {
        TimePeriodValue item = (TimePeriodValue) this.data.get(index);
        TimePeriodValue clone = (TimePeriodValue) item.clone();
        try {
          copy.add(clone);
        } catch (SeriesException e) {
          System.err.println("Failed to add cloned item.");
        }
      }
    }
    return copy;
  }
 /**
  * Constructs a dataset containing a single series. Additional series can be added.
  *
  * @param series the series (<code>null</code> ignored).
  */
 public TimePeriodValuesCollection(TimePeriodValues series) {
   this.data = new java.util.ArrayList();
   this.xPosition = TimePeriodAnchor.MIDDLE;
   this.domainIsPointsInTime = false;
   if (series != null) {
     this.data.add(series);
     series.addChangeListener(this);
   }
 }
 /** A test for bug report 1161329. */
 public void test1161329() {
   TimePeriodValues tpv = new TimePeriodValues("Test");
   RegularTimePeriod t = new Day();
   tpv.add(t, 1.0);
   t = t.next();
   tpv.add(t, 2.0);
   tpv.delete(0, 1);
   assertEquals(0, tpv.getItemCount());
   tpv.add(t, 2.0);
   assertEquals(1, tpv.getItemCount());
 }
  /** Serialize an instance, restore it, and check for equality. */
  public void testSerialization() {

    TimePeriodValues s1 = new TimePeriodValues("A test");
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.add(new Year(2005), 19.32);
    s1.add(new Year(2007), 16.89);
    TimePeriodValues s2 = null;

    try {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      ObjectOutput out = new ObjectOutputStream(buffer);
      out.writeObject(s1);
      out.close();

      ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
      s2 = (TimePeriodValues) in.readObject();
      in.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    assertTrue(s1.equals(s2));
  }
 /**
  * Returns the range of the values in this dataset's domain.
  *
  * @param includeInterval a flag that determines whether or not the x-interval is taken into
  *     account.
  * @return The range.
  */
 @Override
 public Range getDomainBounds(boolean includeInterval) {
   boolean interval = includeInterval || this.domainIsPointsInTime;
   Range result = null;
   Range temp = null;
   Iterator iterator = this.data.iterator();
   while (iterator.hasNext()) {
     TimePeriodValues series = (TimePeriodValues) iterator.next();
     int count = series.getItemCount();
     if (count > 0) {
       TimePeriod start = series.getTimePeriod(series.getMinStartIndex());
       TimePeriod end = series.getTimePeriod(series.getMaxEndIndex());
       if (!interval) {
         if (this.xPosition == TimePeriodAnchor.START) {
           TimePeriod maxStart = series.getTimePeriod(series.getMaxStartIndex());
           temp = new Range(start.getStart().getTime(), maxStart.getStart().getTime());
         } else if (this.xPosition == TimePeriodAnchor.MIDDLE) {
           TimePeriod minMiddle = series.getTimePeriod(series.getMinMiddleIndex());
           long s1 = minMiddle.getStart().getTime();
           long e1 = minMiddle.getEnd().getTime();
           TimePeriod maxMiddle = series.getTimePeriod(series.getMaxMiddleIndex());
           long s2 = maxMiddle.getStart().getTime();
           long e2 = maxMiddle.getEnd().getTime();
           temp = new Range(s1 + (e1 - s1) / 2, s2 + (e2 - s2) / 2);
         } else if (this.xPosition == TimePeriodAnchor.END) {
           TimePeriod minEnd = series.getTimePeriod(series.getMinEndIndex());
           temp = new Range(minEnd.getEnd().getTime(), end.getEnd().getTime());
         }
       } else {
         temp = new Range(start.getStart().getTime(), end.getEnd().getTime());
       }
       result = Range.combine(result, temp);
     }
   }
   return result;
 }
 /**
  * Returns the y-value for the specified series and item.
  *
  * @param series the series (zero-based index).
  * @param item the item (zero-based index).
  * @return The y-value for the specified series and item.
  */
 @Override
 public Number getY(int series, int item) {
   TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
   TimePeriodValue dp = ts.getDataItem(item);
   return dp.getValue();
 }
 /**
  * Returns the ending X value for the specified series and item.
  *
  * @param series the series (zero-based index).
  * @param item the item (zero-based index).
  * @return The ending X value for the specified series and item.
  */
 @Override
 public Number getEndX(int series, int item) {
   TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
   TimePeriodValue dp = ts.getDataItem(item);
   return new Long(dp.getPeriod().getEnd().getTime());
 }
 /**
  * Removes the specified series from the collection.
  *
  * @param series the series to remove (<code>null</code> not permitted).
  */
 public void removeSeries(TimePeriodValues series) {
   ParamChecks.nullNotPermitted(series, "series");
   this.data.remove(series);
   series.removeChangeListener(this);
   fireDatasetChanged();
 }
  /** Tests the equals method. */
  public void testEquals() {
    TimePeriodValues s1 = new TimePeriodValues("Time Series 1");
    TimePeriodValues s2 = new TimePeriodValues("Time Series 2");
    boolean b1 = s1.equals(s2);
    assertFalse("b1", b1);

    s2.setKey("Time Series 1");
    boolean b2 = s1.equals(s2);
    assertTrue("b2", b2);

    // domain description
    s1.setDomainDescription("XYZ");
    assertFalse(s1.equals(s2));
    s2.setDomainDescription("XYZ");
    assertTrue(s1.equals(s2));

    // domain description - null
    s1.setDomainDescription(null);
    assertFalse(s1.equals(s2));
    s2.setDomainDescription(null);
    assertTrue(s1.equals(s2));

    // range description
    s1.setRangeDescription("XYZ");
    assertFalse(s1.equals(s2));
    s2.setRangeDescription("XYZ");
    assertTrue(s1.equals(s2));

    // range description - null
    s1.setRangeDescription(null);
    assertFalse(s1.equals(s2));
    s2.setRangeDescription(null);
    assertTrue(s1.equals(s2));

    RegularTimePeriod p1 = new Day();
    RegularTimePeriod p2 = p1.next();
    s1.add(p1, 100.0);
    s1.add(p2, 200.0);
    boolean b3 = s1.equals(s2);
    assertFalse("b3", b3);

    s2.add(p1, 100.0);
    s2.add(p2, 200.0);
    boolean b4 = s1.equals(s2);
    assertTrue("b4", b4);
  }