Skip to content

Instantly share code, notes, and snippets.

@GeePawHill
Created February 19, 2018 21:45
Show Gist options
  • Select an option

  • Save GeePawHill/e6d7fc004dc42920534f30df0332be39 to your computer and use it in GitHub Desktop.

Select an option

Save GeePawHill/e6d7fc004dc42920534f30df0332be39 to your computer and use it in GitHub Desktop.
package org.geepawhill.contentment.flow;
import static org.geepawhill.contentment.utility.JfxUtility.color;
import java.util.HashMap;
import java.util.Map;
import org.geepawhill.contentment.format.Format;
import org.geepawhill.contentment.style.Frames;
import org.geepawhill.contentment.style.TypeFace;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
public class FormatTable
{
Map<Size,Map<Color,Format>> sizeToColors;
static class EntryNotFoundException extends RuntimeException
{
public EntryNotFoundException(Size size, Color color, String string)
{
super("FormatTable.get( "+size.toString()+","+color.toString()+" "+string);
}
private static final long serialVersionUID = -4794179798871696945L;
}
public FormatTable()
{
final double jumbo = 80d;
final double normal = 55d;
final double small = 45d;
Paint primary = color(119, 187, 65);
Paint secondary = color(177, 140, 254);
Paint emphatic = color(255, 255, 0);
sizeToColors = new HashMap<>();
Map<Color,Format> jumbos = new HashMap<>();
jumbos.put(Color.Primary, format(primary,jumbo));
jumbos.put(Color.Secondary, format(secondary,jumbo));
jumbos.put(Color.Emphatic, format(emphatic,jumbo));
sizeToColors.put(Size.Jumbo, jumbos);
Map<Color,Format> normals = new HashMap<>();
normals.put(Color.Primary, format(primary,normal));
normals.put(Color.Secondary, format(secondary,normal));
normals.put(Color.Emphatic, format(emphatic,normal));
sizeToColors.put(Size.Normal, normals);
Map<Color,Format> smalls = new HashMap<>();
smalls.put(Color.Primary, format(primary,small));
smalls.put(Color.Secondary, format(secondary,small));
smalls.put(Color.Emphatic, format(emphatic,small));
sizeToColors.put(Size.Small, smalls);
}
public Format get(Size size, Color color)
{
Map<Color,Format> colorToFormat = sizeToColors.get(size);
if(colorToFormat==null) throw new EntryNotFoundException(size,color,"Size not found.");
Format result = colorToFormat.get(color);
if(result==null) throw new EntryNotFoundException(size,color,"Color not found.");
return result;
}
private Format format(Paint majorColor, double fontsize) {
Font font = Font.font("Chewed Pen BB", FontPosture.ITALIC, fontsize);
return new Format(TypeFace.font(font, 2d, 1d), TypeFace.color(majorColor, 1d),
Frames.frame(majorColor, 5d, 1d));
}
}
package org.geepawhill.contentment.flow;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
public class FormatTableTest
{
private FormatTable table;
@Before
public void before()
{
table = new FormatTable();
}
@Test
public void findsAllNineValues()
{
for (Size size : Size.values())
{
if(size.equals(Size.None)) continue;
for (Color color : Color.values())
{
if(color.equals(Color.None)) continue;
assertThat(table.get(size, color)).isNotNull();
}
}
}
@Test
public void missingSize()
{
try
{
table.get(Size.None, Color.Emphatic);
fail("No throw on missing size.");
}
catch (FormatTable.EntryNotFoundException e)
{
assertThat(e.getMessage()).endsWith("Size not found.");
}
try
{
table.get(Size.Jumbo, Color.None);
fail("No throw on missing color.");
}
catch (FormatTable.EntryNotFoundException e)
{
assertThat(e.getMessage()).endsWith("Color not found.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment