File: NwsForecastAPI.java

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (121 lines) | stat: -rwxr-xr-x 3,691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package edu.ucsb.cs.nws.nwsprotocol;

import java.io.*;
import java.net.*;


/**
 * The NwsForecastAPI implements the FORECAST_API calls. It defines the data
 * structures too (measurement and forecast and the like).
 */
public class NwsForecastAPI {
	/** A single forecast generated from a list of Measurements. */
	public static class Forecast {
		/** The forecast of the next value in the series. */
		public double forecast;
		/** An estimation of the accuracy of the forecast. */
		public double error;
		/** The index of the method that
		 * produced the forecast */
		public int methodUsed;

		/** Produces a Forecast with the specified field values. */
		public Forecast(double forecast,
				double error,
				int methodUsed) {
			this.forecast = forecast;
			this.error = error;
			this.methodUsed = methodUsed;
		}

		/** Produces a Forecast initialized by reading <i>stream</i>. */
		public Forecast(DataInputStream stream) throws Exception {
			forecast = stream.readDouble();
			error = stream.readDouble();
			methodUsed = stream.readInt();
		}

		/** Returns the fields of the Forecast converted to a
		 * byte array. */
		public byte[] toBytes() {
			byte[][] allBytes = {NwsMessage.toBytes(forecast),
					NwsMessage.toBytes(error),
					NwsMessage.toBytes(methodUsed)};
			return NwsMessage.concatenateBytes(allBytes);
		}

	}


	/** A set of forecast information produced from a single measurement. */
	public static class ForecastCollection {
		public static final int FORECAST_TYPE_COUNT = 2;
		public static final int MAE_FORECAST = 0;
		public static final int MSE_FORECAST = 1;

		/** The measurement from which the forecasts were produced. */
		public Measurement measurement;
		/** A pair of forecasts based on the methods which
		 * produce the lowest mean absolute error and mean square
		 * error.  */
		public Forecast[] forecasts;

		/** Produces a ForecastCollection with the specified
		 * field values. */
		public ForecastCollection(Measurement measurement,
					Forecast[] forecasts) {
			this.measurement = measurement;
			this.forecasts = forecasts;
		}

		/** Produces a ForecastCollection initialized by reading
		 * <i>stream</i>. */
		public ForecastCollection(DataInputStream stream) throws Exception {
			measurement = new Measurement(stream);
			forecasts = new Forecast[FORECAST_TYPE_COUNT];
			for(int i = 0; i < FORECAST_TYPE_COUNT; i++)
				forecasts[i] = new Forecast(stream);
		}

		/** Returns the fields of the ForecastCollection
		 * converted to a byte array.  */
		public byte[] toBytes() {
			byte[][] allBytes = {measurement.toBytes(),
				forecasts[MAE_FORECAST].toBytes(),
				forecasts[MSE_FORECAST].toBytes()};
			return NwsMessage.concatenateBytes(allBytes);
		}
	}


	/** A single measurement of resource availability. */
	public static class Measurement {
		/** The time (seconds since midnight 1/1/1970) the
		 * measurement was taken. */
		public double timeStamp;
		/** The observed availability at that time. */
		public double measurement;

		/** Produces a Measurement with the specified field values. */
		public Measurement(	double timeStamp,
					double measurement) {
			this.timeStamp = timeStamp;
			this.measurement = measurement;
		}

		/** Produces a Measurement initialized by reading
		 * <i>stream</i>. */
		public Measurement(DataInputStream stream) throws Exception {
			timeStamp = stream.readDouble();
			measurement = stream.readDouble();
		}

		/** Returns the fields of the Measurement converted to a
		 * byte array. */
		public byte[] toBytes() {
			byte[][] allBytes = {NwsMessage.toBytes(timeStamp),
					NwsMessage.toBytes(measurement)};
			return NwsMessage.concatenateBytes(allBytes);
		}
	}
}