/
TariffSpecification.java
340 lines (304 loc) · 9.18 KB
/
TariffSpecification.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2011-2013 by the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.powertac.common;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.Instant;
import org.powertac.common.enumerations.PowerType;
import org.powertac.common.state.Domain;
import org.powertac.common.state.StateChange;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
/**
* Represents a Tariff offered by a Broker to customers. A TariffSpecification
* may specify upfront and periodic payments, and
* aggregates a set of Rate instances that specify
* prices for energy in various circumstances. This class is a value type -- a
* serializable, immutable data structure. You need to initialize a
* Tariff from it to make serious use of it. New tariffs and their Rates
* are communicated to Customers and to Brokers when tariffs are published.
* <p>
* TariffSpecifications and their associated Rates and HourlyCharges are
* specified from the Customer's viewpoint in terms of quantities and charges.
* In other words, a positive charge means the broker pays the customer, while
* a positive energy quantity means the broker sends energy to the customer.</p>
* <p>
* <strong>Note:</strong> Must be serialized "deep" to gather up the Rates and
* associated HourlyCharge instances.</p>
* <p>
* State log entry format for new instance:<br/>
* <code>long brokerId, PowerType powerType</code><br/><br/>
* State log fields for readResolve():<br>
* <code>long brokerId, PowerType powerType, long minDuration,<br>
* double signupPayment, double earlyWithdrawPayment,<br>
* double periodicPayment, List<tariffId> supersedes</code>
*
* @author John Collins
*/
@Domain(fields = {"broker", "powerType", "expiration", "minDuration",
"signupPayment", "earlyWithdrawPayment", "periodicPayment",
"supersedes"})
@XStreamAlias("tariff-spec")
public class TariffSpecification extends TariffMessage
{
static private Logger log = LogManager.getLogger(TariffSpecification.class);
/** Last date, in msec past epoch, that new subscriptions will be accepted.
* Zero means never expire. */
@XStreamAsAttribute
private long expiration = 0l;
/** Minimum contract duration (in milliseconds) */
@XStreamAsAttribute
private long minDuration = 0l;
/** Type of power covered by this tariff */
@XStreamAsAttribute
private PowerType powerType = PowerType.CONSUMPTION;
/** One-time payment for subscribing to tariff, positive for payment
* to customer, negative for payment from customer. */
@XStreamAsAttribute
private double signupPayment = 0.0;
/** Payment from customer to broker for canceling subscription before
* minDuration has elapsed. */
@XStreamAsAttribute
private double earlyWithdrawPayment = 0.0;
/** Flat payment per period for two-part tariffs */
@XStreamAsAttribute
private double periodicPayment = 0.0;
private List<RateCore> rates;
private List<Long> supersedes;
/**
* Creates a new TariffSpecification for a broker and a specific powerType.
* Attributes are provided by the fluent {@code withX()} methods.
*/
public TariffSpecification (Broker broker, PowerType powerType)
{
super(broker);
this.broker = broker;
this.powerType = powerType;
this.rates = new ArrayList<RateCore>();
}
public PowerType getPowerType ()
{
return powerType;
}
public Instant getExpiration ()
{
if (expiration > 0)
return new Instant(expiration);
else
return null;
}
/**
* Sets expiration date as msec since epoch. See Issue #1016.
*/
@StateChange
public TariffSpecification withExpiration (long expiration)
{
this.expiration = expiration;
return this;
}
/**
* Sets the expiration date for this tariff. After this date, customers
* will no longer be allowed to subscribe.
*/
public TariffSpecification withExpiration (Instant expiration)
{
return this.withExpiration(expiration.getMillis());
}
public long getMinDuration ()
{
return minDuration;
}
/**
* Sets the minimum duration of a subscription for this tariff. If a customer
* wishes to withdraw earlier than the minimum duration, it may be required
* to pay a fee as specified by the withdrawPayment.
*/
@StateChange
public TariffSpecification withMinDuration (long minDuration)
{
this.minDuration = minDuration;
return this;
}
public double getSignupPayment ()
{
return signupPayment;
}
/**
* Sets the signup payment for new subscriptions. This is a positive
* number if the broker pays the customer.
*/
@StateChange
public TariffSpecification withSignupPayment (double signupPayment)
{
this.signupPayment = signupPayment;
return this;
}
public double getEarlyWithdrawPayment ()
{
return earlyWithdrawPayment;
}
/**
* Sets the payment for a customer who withdraws from a subscription to
* this tariff before the minimumDuration has expired. A negative number
* indicates that the customer pays the broker.
*/
@StateChange
public TariffSpecification withEarlyWithdrawPayment (double earlyWithdrawPayment)
{
this.earlyWithdrawPayment = earlyWithdrawPayment;
return this;
}
public double getPeriodicPayment ()
{
return periodicPayment;
}
/**
* Sets the daily payment per customer for subscriptions to this tariff.
* A negative number indicates that the customer pays the broker.
*/
@StateChange
public TariffSpecification withPeriodicPayment (double periodicPayment)
{
this.periodicPayment = periodicPayment;
return this;
}
@Override
public long getId ()
{
return id;
}
@Override
public Broker getBroker ()
{
return broker;
}
/**
* Returns the Rate instances from among the rates in this tariff spec.
*/
public List<Rate> getRates ()
{
List<Rate> result = new ArrayList<Rate>();
if (null == rates) {
log.error("Null rates tariff {}", this.getId());
return result;
}
for (RateCore rate : rates) {
if (rate instanceof Rate) {
result.add((Rate)rate);
}
}
return result;
}
/**
* Returns the RegulationRate instances from among the rates
* in this tariff spec.
*/
public List<RegulationRate> getRegulationRates ()
{
List<RegulationRate> result = new ArrayList<RegulationRate>();
for (RateCore rate : rates) {
if (rate instanceof RegulationRate) {
result.add((RegulationRate)rate);
}
}
return result;
}
/**
* Returns true just in case this tariff has one or more RegulationRates.
*/
public boolean hasRegulationRate ()
{
for (RateCore rate : rates) {
if (rate instanceof RegulationRate) {
return true;
}
}
return false;
}
/**
* Adds a new RateCore (Rate, RegulationRate, etc.) to this tariff.
*/
@StateChange
public TariffSpecification addRate (RateCore rate)
{
if (null == rates)
// readResolve does not create the list
rates = new ArrayList<RateCore>();
rates.add(rate);
rate.setTariffId(id);
return this;
}
public List<Long> getSupersedes ()
{
return supersedes;
}
/**
* Indicates that this tariff supersedes the tariff specified by the
* specId, the id of the superseded tariff. Setting this value has no
* effect unless the superseded tariff is revoked by sending a TariffRevoke
* message for that tariff.
*/
@StateChange
public TariffSpecification addSupersedes (long specId)
{
if (supersedes == null)
supersedes = new ArrayList<Long>();
supersedes.add(specId);
return this;
}
/**
* A TariffSpecification is valid if
* <ul>
* <li>it has a least one Rate, and all its Rates are valid;</li>
* <li>minDuration is non-negative;</li>
* </ul>
*/
@Override
public boolean isValid ()
{
if (getRates().size() == 0) {
log.warn("invalid: no rates");
return false;
}
for (Rate rate : getRates()) {
if (!rate.isValid(this)) {
log.warn("invalid rate");
return false;
}
}
if (minDuration < 0l) {
log.warn("invalid: negative minDuration");
return false;
}
return true;
}
/**
* Returns a String giving the id, broker username, and powertype
*/
@Override
public String toString ()
{
return ("TariffSpecification " + getId() + " "
+ getBroker().getUsername() + "." + getPowerType());
}
// protected default constructor to simplify deserialization
protected TariffSpecification ()
{
super();
}
}