125 lines
3.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/recyclea/RecycleA.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Recycling with RTTI
2016-07-28 12:48:23 -06:00
// {java patterns.recyclea.RecycleA}
2015-06-15 17:47:35 -07:00
package patterns.recyclea;
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
import java.util.stream.*;
2015-06-15 17:47:35 -07:00
abstract class Trash {
2015-11-03 12:00:44 -08:00
double weight;
2015-06-15 17:47:35 -07:00
Trash(double wt) { weight = wt; }
abstract double value();
// Sums the value of Trash in a bin:
2015-11-03 12:00:44 -08:00
private static double val;
2015-06-15 17:47:35 -07:00
static void sumValue(List<? extends Trash> bin) {
2015-11-03 12:00:44 -08:00
val = 0.0f;
bin.forEach( t -> {
2015-06-15 17:47:35 -07:00
// Polymorphism in action:
2015-11-03 12:00:44 -08:00
val += t.weight * t.value();
2015-06-15 17:47:35 -07:00
System.out.println(
"weight of " +
// Using RTTI to get type
// information about the class:
2015-11-03 12:00:44 -08:00
t.getClass().getSimpleName() +
" = " + t.weight);
});
2015-06-15 17:47:35 -07:00
System.out.println("Total value = " + val);
}
}
class Aluminum extends Trash {
static double val = 1.67f;
Aluminum(double wt) { super(wt); }
@Override double value() { return val; }
2015-06-15 17:47:35 -07:00
static void value(double newval) {
val = newval;
}
}
class Paper extends Trash {
static double val = 0.10f;
Paper(double wt) { super(wt); }
@Override double value() { return val; }
2015-06-15 17:47:35 -07:00
static void value(double newval) {
val = newval;
}
}
class Glass extends Trash {
static double val = 0.23f;
Glass(double wt) { super(wt); }
@Override double value() { return val; }
2015-06-15 17:47:35 -07:00
static void value(double newval) {
val = newval;
}
}
2015-11-03 12:00:44 -08:00
class TrashFactory {
static List<Function<Double, Trash>> ttypes =
Arrays.asList(
Aluminum::new, Paper::new, Glass::new);
static final int SZ = ttypes.size();
2017-01-08 22:55:49 -08:00
private static SplittableRandom rand =
new SplittableRandom(47);
2015-11-03 12:00:44 -08:00
public static Trash newTrash() {
return ttypes
.get(rand.nextInt(SZ))
2015-11-03 12:00:44 -08:00
.apply(rand.nextDouble());
}
}
2015-06-15 17:47:35 -07:00
public class RecycleA {
public static void main(String[] args) {
2015-11-03 12:00:44 -08:00
List<Trash> bin =
Stream.generate(TrashFactory::newTrash)
.limit(25)
.collect(Collectors.toList());
2015-06-15 17:47:35 -07:00
List<Glass> glassBin = new ArrayList<>();
List<Paper> paperBin = new ArrayList<>();
List<Aluminum> alBin = new ArrayList<>();
// Sort the Trash:
2015-11-03 12:00:44 -08:00
bin.forEach( t -> {
2015-06-15 17:47:35 -07:00
// RTTI to discover Trash type:
if(t instanceof Aluminum)
alBin.add((Aluminum)t);
if(t instanceof Paper)
paperBin.add((Paper)t);
if(t instanceof Glass)
glassBin.add((Glass)t);
2015-11-03 12:00:44 -08:00
});
2015-06-15 17:47:35 -07:00
Trash.sumValue(alBin);
Trash.sumValue(paperBin);
Trash.sumValue(glassBin);
Trash.sumValue(bin);
}
2015-09-07 11:44:36 -06:00
}
2016-07-20 06:32:39 -06:00
/* Output: (First and Last 11 Lines)
2016-07-22 14:45:35 -06:00
weight of Aluminum = 0.2893030122276371
weight of Aluminum = 0.1970234961398979
weight of Aluminum = 0.36295525806274787
weight of Aluminum = 0.4825532324565849
weight of Aluminum = 0.8036398273294586
weight of Aluminum = 0.510430896154935
weight of Aluminum = 0.6703377164093444
weight of Aluminum = 0.41477933066243455
weight of Aluminum = 0.3603022312124007
weight of Aluminum = 0.43690089841661006
weight of Aluminum = 0.6708820087907101
...________...________...________...________...
weight of Aluminum = 0.41477933066243455
weight of Aluminum = 0.3603022312124007
weight of Aluminum = 0.43690089841661006
weight of Glass = 0.5999637765664924
weight of Glass = 0.7748836191212746
weight of Paper = 0.5735994548427199
weight of Glass = 0.5362827750851034
weight of Aluminum = 0.6708820087907101
weight of Paper = 0.8370669795210507
weight of Glass = 0.3397919679731668
Total value = 9.90671597531968
2015-09-07 11:44:36 -06:00
*/