You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Billing.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. */
  10. package telecom;
  11. /**
  12. * The Billing aspect deals with... billing.
  13. * How much money did each connection cost?
  14. * How much money did each call cost?
  15. * How much money is being debited to a customer?
  16. * This aspect can be used by other parts of the system. (not in this example)
  17. *
  18. * Billing can depend many things, such as timing, the type of the connection,
  19. * some special discounts the customer has, special features, etc. In here,
  20. * it depends only on timing and on the type of the connection.
  21. */
  22. public aspect Billing {
  23. // precedence required to get advice on endtiming in the right order
  24. declare precedence: Billing, Timing;
  25. public static final long LOCAL_RATE = 3;
  26. public static final long LONG_DISTANCE_RATE = 10;
  27. public Customer Connection.payer;
  28. public Customer getPayer(Connection conn) { return conn.payer; }
  29. /**
  30. * Caller pays for the call
  31. */
  32. after(Customer cust) returning (Connection conn):
  33. args(cust, ..) && call(Connection+.new(..)) {
  34. conn.payer = cust;
  35. }
  36. /**
  37. * Connections give the appropriate call rate
  38. */
  39. public abstract long Connection.callRate();
  40. public long LongDistance.callRate() { return LONG_DISTANCE_RATE; }
  41. public long Local.callRate() { return LOCAL_RATE; }
  42. /**
  43. * When timing stops, calculate and add the charge from the
  44. * connection time
  45. */
  46. after(Connection conn): Timing.endTiming(conn) {
  47. long time = Timing.aspectOf().getTimer(conn).getTime();
  48. long rate = conn.callRate();
  49. long cost = rate * time;
  50. getPayer(conn).addCharge(cost);
  51. }
  52. /**
  53. * Customers have a bill paying aspect with state
  54. */
  55. public long Customer.totalCharge = 0;
  56. public long getTotalCharge(Customer cust) { return cust.totalCharge; }
  57. public void Customer.addCharge(long charge){
  58. totalCharge += charge;
  59. }
  60. }