📄 dataset.java
字号:
*/ public void sort( String independentVariable ) { // TODO: check that independentVariable is even defined for this set // Create a new sorted map using the given comparator SortedMap sortedMap = new TreeMap( new Comparator() { public int compare( Object o1, Object o2 ) { return ((Double)o1).compareTo((Double)o2); } } ); // Add each element in the array list to the sorted map. Iterator it = dataPoints.iterator(); while ( it.hasNext() ) { // By putting each DataPoint in the list, it will // automatically sort them by key DataPoint dp = (DataPoint)it.next(); sortedMap.put( new Double(dp.getIndependentValue(independentVariable)), dp ); } // Clear dataPoints dataPoints.clear(); // Add all values from sorted map back into list in sorted order dataPoints.addAll( sortedMap.values() ); } /** * Returns an ordered array of all independent variable names used in this * data set. The array is guaranteed not to contain duplicate names. * @return a sorted array of unique independent variable names for this * data set. */ public String[] getIndependentVariables() { ArrayList variables = new ArrayList(); Iterator it = dataPoints.iterator(); while ( it.hasNext() ) { DataPoint dp = (DataPoint)it.next(); String[] names = dp.getIndependentVariableNames(); for ( int i=0; i<names.length; i++ ) if ( !variables.contains( names[i] ) ) variables.add( names[i] ); } // Sort list Collections.sort( variables ); // Convert the ArrayList to a String[] int count = variables.size(); String names[] = new String[count]; for ( int i=0; i<count; i++ ) names[i] = (String)(variables.get(i)); return names; } /** * Converts the current set of data points to an array of DataPoint * objects. The elements will be in the same order as in the DataSet. * @return an array containing all of the DataPoints in this data set. */ public Object[] toArray() { return dataPoints.toArray(); } /** * Converts the current set of data points to an array of DataPoint * objects; the runtime type of the returned array is that of the * specified array. The elements will be in the same order as in the * DataSet. * @param a the array into which the elements of this data set are to be * stored, if it is big enough; otherwise, a new array of the same * runtime type is allocated for this purpose. * @return an array containing all of the DataPoints in this data set. */ public Object[] toArray( Object[] a ) { return dataPoints.toArray( a ); } /** * Sets the name of the time variable for this data set. If this is not * set, then the data set will be treated as being non time-based. In * addition to setting the time variable for time series data, it is * strongly recommended that you also initialize the number of periods per * year with a call to setPeriodsPerYear. * @param timeVariable the name of the independent variable that represents * the time data component. For example, this may be something like * "t", "month", "period", "year", and so on. * @see #setPeriodsPerYear */ public void setTimeVariable( String timeVariable ) { this.timeVariable = timeVariable; } /** * Returns the time variable associated with this data set, or * <code>null</code> if no time variable has been defined. * @return the time variable associated with this data set. */ public String getTimeVariable() { return timeVariable; } /** * Sets the number of periods - or data points - in a years worth of data * for time-series data. If this is not set, then no seasonality effects * will be considered when forecasting using this data set. * * <p>In addition to setting the number of periods per year, you must also * set the time variable otherwise any forecasting model will not be able * to consider the potential effects of seasonality. * @param periodsPerYear the number of periods in a years worth of data. * @see #setTimeVariable */ public void setPeriodsPerYear( int periodsPerYear ) { if ( periodsPerYear < 1 ) throw new IllegalArgumentException( "periodsPerYear parameter must be at least 1" ); this.periodsPerYear = periodsPerYear; } /** * Returns the number of periods - or data points - in a years worth of * data for time-series data. If this has not been set, then a value of 0 * will be returned. * @return the number of periods in a years worth of data. */ public int getPeriodsPerYear() { return periodsPerYear; } /** * Not currently implemented - always throws UnsupportedOperationException. * Removes all this DataSet's elements that are also contained in the * specified collection of DataPoint objects. After this call returns, * this DataSet will contain no elements in common with the specified * collection. * @param c DataPoint objects to be removed from this collection. * @return true if this DataSet changed as a result of the call. * @throws UnsupportedOperationException if the removeAll method is not * supported by this collection. * @throws ClassCastException if the types of one or more elements in the * specified DataSet are not DataPoint objects. * @throws NullPointerException if the specified collection contains one * or more null elements. */ public boolean removeAll( Collection c ) throws UnsupportedOperationException { // TODO: Implement DataSet.removeAll throw new UnsupportedOperationException("DataSet.removeAll not yet supported"); } /** * Not currently implemented - always throws UnsupportedOperationException. * Retains only the elements in this collection that are contained in the * specified collection (optional operation). In other words, removes from * this collection all of its elements that are not contained in the * specified collection. * @param c elements to be retained in this collection. * @return true if this collection changed as a result of the call. * @throws UnsupportedOperationException if the retainAll method is not * supported by this collection. * @throws ClassCastException if the types of one or more elements in the * specified DataSet are not DataPoint objects. * @throws NullPointerException if the specified collection contains one * or more null elements. */ public boolean retainAll( Collection c ) throws UnsupportedOperationException { // TODO: Implement DataSet.retainAll throw new UnsupportedOperationException("DataSet.retainAll not yet supported"); } /** * Returns the hash code value for this collection, based on the * underlying Collection of DataPoints. * @return the hash code value for this collection. */ public int hashCode() { return dataPoints.size()*100 + getIndependentVariables().length; } /** * Indicates whether some other object, obj, is "equal to" this one. * Returns true if the Object, obj, represents another DataSet for which * {@link #equals(DataSet)} returns true; otherwise false. * @param obj the reference object with which to compare. * @return true if this object is the same as the obj argument; false * otherwise. * @see #equals(DataSet) */ public boolean equals( Object obj ) { if ( obj == null ) return false; if ( !(obj instanceof DataSet) ) return false; return this.equals( (DataSet)obj ); } /** * Indicates whether some other DataSet is "equal to" this one. Returns * true if the DataSet, dataSet, represents another DataSet containing * exactly the same data points as this DataSet. Note that neither the * DataPoint objects, or the DataSet objects have to refer to the same * instance. They just must refer to a collection of DataPoints with the * same values for the independent and dependent variables. * @param dataSet the reference object with which to compare. * @return true if this object is the same as the dataSet argument; false * otherwise. */ public boolean equals( DataSet dataSet ) { if ( dataSet == null ) return false; if ( this.size() != dataSet.size() ) return false; // Iterate through all data points in dataSet, // checking that the same DataPoint exists in this DataSet Iterator it = dataSet.iterator(); while ( it.hasNext() ) { DataPoint dataPoint = (DataPoint)it.next(); if ( !this.contains( dataPoint ) ) return false; } return true; } /** * Overrides the default toString method. Lists all data points in this * data set. Note that if there are a large number of data points in this * data set, then the String returned could be very long. * @return a string representation of this data set. */ public String toString() { String lineSeparator = System.getProperty("line.separator"); String result = "( " + lineSeparator; Iterator it = dataPoints.iterator(); while ( it.hasNext() ) { result += " " + ((DataPoint)it.next()).toString() + lineSeparator; } return result + ")"; }}// Local variables:// tab-width: 4// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -