Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. package com.cyrillemartraire.monoids;
  2.  
  3. import static com.cyrillemartraire.monoids.EnvironmentalImpactTest.CertifiedAmount.certified;
  4. import static com.cyrillemartraire.monoids.EnvironmentalImpactTest.CertifiedAmount.uncertified;
  5. import static com.cyrillemartraire.monoids.EnvironmentalImpactTest.EnvironmentalImpact.singleSupplier;
  6. import static java.lang.Double.doubleToLongBits;
  7. import static java.lang.Math.abs;
  8. import static org.junit.Assert.assertEquals;
  9.  
  10. import org.junit.Test;
  11.  
  12. public class EnvironmentalImpactTest {
  13.  
  14. @Test
  15. public void pizzaImpact() {
  16. EnvironmentalImpact cooking = singleSupplier(certified(1, "kWh", 0.3), certified(1, "T", 0.25));
  17. EnvironmentalImpact dough = singleSupplier(uncertified(5, "kWh", 5.), uncertified(0.5, "T", 1.));
  18. EnvironmentalImpact tomatoSauce = singleSupplier(uncertified(3, "kWh", 1.), certified(0.2, "T", 0.1));
  19.  
  20. EnvironmentalImpact expectedPizza = new EnvironmentalImpact(3,
  21. new CertifiedAmount(new Amount(6.9, "kWh", 5.6), 1., 2.3),
  22. new CertifiedAmount(new Amount(1.56, "T", 1.28), 1.3, 2.3));
  23.  
  24. EnvironmentalImpact pizza = cooking.add(dough).add(tomatoSauce.times(0.3));
  25. assertEquals(expectedPizza, pizza);
  26. }
  27.  
  28. /**
  29. * The energy and carbon impacts across a supply chain
  30. */
  31. public static class EnvironmentalImpact {
  32.  
  33. private final int supplierCount;
  34. private final CertifiedAmount energyConsumption;
  35. private final CertifiedAmount carbonEmission;
  36.  
  37. public static final EnvironmentalImpact neutral() {
  38. return new EnvironmentalImpact(0, CertifiedAmount.neutral("kWh"), CertifiedAmount.neutral("T"));
  39. }
  40.  
  41. public static final EnvironmentalImpact singleSupplier(CertifiedAmount energyConsumption,
  42. CertifiedAmount carbonEmission) {
  43. return new EnvironmentalImpact(1, energyConsumption, carbonEmission);
  44. }
  45.  
  46. public EnvironmentalImpact(int supplierCount, CertifiedAmount energyConsumption,
  47. CertifiedAmount carbonEmission) {
  48. this.supplierCount = supplierCount;
  49. this.energyConsumption = energyConsumption;
  50. this.carbonEmission = carbonEmission;
  51. }
  52.  
  53. public EnvironmentalImpact add(EnvironmentalImpact other) {
  54. return new EnvironmentalImpact(supplierCount + other.supplierCount,
  55. energyConsumption.add(other.energyConsumption), carbonEmission.add(other.carbonEmission));
  56. }
  57.  
  58. public EnvironmentalImpact add(double coefficient, EnvironmentalImpact other) {
  59. return add(other.times(coefficient));
  60. }
  61.  
  62. public EnvironmentalImpact times(double coefficient) {
  63. return new EnvironmentalImpact(supplierCount, energyConsumption.times(coefficient),
  64. carbonEmission.times(coefficient));
  65. }
  66.  
  67. @Override
  68. public int hashCode() {
  69. return 31 ^ carbonEmission.hashCode() + energyConsumption.hashCode() ^ supplierCount;
  70. }
  71.  
  72. @Override
  73. public boolean equals(Object obj) {
  74. EnvironmentalImpact other = (EnvironmentalImpact) obj;
  75. return supplierCount == other.supplierCount && energyConsumption.equals(other.energyConsumption)
  76. && carbonEmission.equals(other.carbonEmission);
  77. }
  78.  
  79. @Override
  80. public String toString() {
  81. return "EnvironmentalImpact(" + supplierCount + " supplier(s), energy (kWh): " + energyConsumption
  82. + ", carbon (T): " + carbonEmission + " )";
  83. }
  84.  
  85. }
  86.  
  87. /**
  88. * An amount that keeps track of its percentage of certification
  89. */
  90. public static class CertifiedAmount {
  91. private final Amount amount;
  92. private final double score;// the total certification score
  93. private final double weight; // the total weight of the certified thing
  94.  
  95. public static final CertifiedAmount certified(Amount amount) {
  96. return new CertifiedAmount(amount, 1., 1.);
  97. }
  98.  
  99. public static CertifiedAmount neutral(String unit) {
  100. return new CertifiedAmount(Amount.neutral(unit), 0., 0.);
  101. }
  102.  
  103. public static final CertifiedAmount certified(double value, String unit, double errorMargin) {
  104. return new CertifiedAmount(new Amount(value, unit, errorMargin), 1., 1.);
  105. }
  106.  
  107. public static final CertifiedAmount uncertified(double value, String unit, double errorMargin) {
  108. return new CertifiedAmount(new Amount(value, unit, errorMargin), 0., 1.);
  109. }
  110.  
  111. public CertifiedAmount(Amount amount, double score, double weight) {
  112. this.amount = amount;
  113. this.score = score;
  114. this.weight = weight;
  115. }
  116.  
  117. public CertifiedAmount add(CertifiedAmount other) {
  118. return new CertifiedAmount(amount.add(other.amount), score + other.score, weight + other.weight);
  119. }
  120.  
  121. public CertifiedAmount times(double coefficient) {
  122. return new CertifiedAmount(amount.times(coefficient), coefficient * score, coefficient);
  123. }
  124.  
  125. @Override
  126. public int hashCode() {
  127. return (int) (amount.hashCode() ^ doubleToLongBits(score) ^ doubleToLongBits(weight));
  128. }
  129.  
  130. @Override
  131. public boolean equals(Object obj) {
  132. CertifiedAmount other = (CertifiedAmount) obj;
  133. return amount.equals(other.amount) && abs(score - other.score) <= 0.01
  134. && abs(weight - other.weight) <= 0.01;
  135. }
  136.  
  137. @Override
  138. public String toString() {
  139. return amount + ", " + ((int) (score * 100. / weight)) + "% certified";
  140. }
  141. }
  142.  
  143. /**
  144. * An amount of a physical quantity, with its unit, margin of error and
  145. * percentage of certification
  146. */
  147. public static class Amount {
  148. private final double value;
  149. private final String unit;
  150. private final double errorMargin;
  151.  
  152. public static final Amount neutral(String unit) {
  153. return new Amount(0., unit, 0.);
  154. }
  155.  
  156. public Amount(double value, String unit, double errorMargin) {
  157. this.value = value;
  158. this.unit = unit;
  159. this.errorMargin = errorMargin;
  160. }
  161.  
  162. public Amount add(double coefficient, Amount other) {
  163. return this.add(other.times(coefficient));
  164. }
  165.  
  166. public Amount add(Amount other) {
  167. if (!unit.equals(other.unit))
  168. throw new IllegalArgumentException(
  169. "Cannot add amounts of different units: " + unit + " <> " + other.unit);
  170. return new Amount(value + other.value, unit, errorMargin + other.errorMargin);
  171. }
  172.  
  173. public Amount times(double coefficient) {
  174. return new Amount(coefficient * value, unit, coefficient * errorMargin);
  175. }
  176.  
  177. @Override
  178. public int hashCode() {
  179. return (int) (31 ^ doubleToLongBits(errorMargin) + unit.hashCode() ^ doubleToLongBits(value));
  180. }
  181.  
  182. @Override
  183. public boolean equals(Object obj) {
  184. Amount other = (Amount) obj;
  185. return abs(value - other.value) <= 0.01 && unit.equals(other.unit)
  186. && abs(errorMargin - other.errorMargin) <= 0.1;
  187. }
  188.  
  189. @Override
  190. public String toString() {
  191. return "Amount(" + value + unit + (errorMargin == 0. ? "" : "+/-" + errorMargin);
  192. }
  193. }
  194.  
  195. }
Add Comment
Please, Sign In to add comment