Created
October 13, 2016 20:50
-
-
Save siordache/a0ae8a712f28d5543b5c8e56be69b4a7 to your computer and use it in GitHub Desktop.
Answer to http://stackoverflow.com/questions/40023754 using the Streamplify library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.effect.BoxBlur; | |
import javafx.scene.effect.Effect; | |
import javafx.scene.effect.Shadow; | |
import javafx.scene.paint.Color; | |
import org.beryx.streamplify.product.CartesianProduct; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.BiConsumer; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class FeatureCombiner { | |
public static class Characteristics { | |
private Color color; | |
private int height; | |
private int length; | |
private int width; | |
private String material; | |
private int thickness; | |
private String texture; | |
private Effect effect; | |
public Color getColor() { | |
return color; | |
} | |
public void setColor(Color color) { | |
this.color = color; | |
} | |
public int getHeight() { | |
return height; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
public int getLength() { | |
return length; | |
} | |
public void setLength(int length) { | |
this.length = length; | |
} | |
public int getWidth() { | |
return width; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public String getMaterial() { | |
return material; | |
} | |
public void setMaterial(String material) { | |
this.material = material; | |
} | |
public int getThickness() { | |
return thickness; | |
} | |
public void setThickness(int thickness) { | |
this.thickness = thickness; | |
} | |
public String getTexture() { | |
return texture; | |
} | |
public void setTexture(String texture) { | |
this.texture = texture; | |
} | |
public Effect getEffect() { | |
return effect; | |
} | |
public void setEffect(Effect effect) { | |
this.effect = effect; | |
} | |
@Override | |
public String toString() { | |
return color + ", " | |
+ height + ", " | |
+ length + ", " | |
+ width + ", " | |
+ material + ", " | |
+ thickness + ", " | |
+ texture + ", " | |
+ effect.getClass().getSimpleName(); | |
} | |
} | |
private static class Feature<T> { | |
final BiConsumer<Characteristics, T> setter; | |
final List<T> choices; | |
Feature(BiConsumer<Characteristics, T> setter, T... choices) { | |
this.setter = setter; | |
this.choices = Arrays.asList(choices); | |
} | |
} | |
public static void main(String[] args) { | |
final List<Feature<?>> features = Arrays.asList( | |
new Feature<Color>(Characteristics::setColor, Color.BLUE, Color.YELLOW, Color.RED), | |
new Feature<Integer>(Characteristics::setHeight, 20, 30), | |
new Feature<Integer>(Characteristics::setLength, 100, 120), | |
new Feature<Integer>(Characteristics::setWidth, 60, 80), | |
new Feature<String>(Characteristics::setMaterial, "wood", "metal", "glass"), | |
new Feature<Integer>(Characteristics::setThickness, 4, 6), | |
new Feature<String>(Characteristics::setTexture, "smooth", "grainy", "velvety"), | |
new Feature<Effect>(Characteristics::setEffect, new Shadow(), new BoxBlur()) | |
); | |
int[] dimensions = features.stream().mapToInt(f -> f.choices.size()).toArray(); | |
List<Characteristics> chrVariants = new CartesianProduct(dimensions) | |
.stream() | |
.map(prod -> { | |
Characteristics chr = new Characteristics(); | |
IntStream.range(0, prod.length) | |
.forEach(i -> { | |
Feature f = features.get(i); | |
f.setter.accept(chr, f.choices.get(prod[i])); | |
}); | |
return chr; | |
}) | |
.collect(Collectors.toList()); | |
chrVariants.forEach(chr -> System.out.println(chr)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment