Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package local.laer.app.knapsack.domain.support; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; import com.google.common.io.CharStreams; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collection; import java.util.Deque; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; import local.laer.app.knapsack.domain.ImmutablePosition; import local.laer.app.knapsack.domain.Position; import local.laer.app.knapsack.support.IntFunctions; import local.laer.app.knapsack.support.OffsetIntMatrix; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import org.hamcrest.collection.IsIterableContainingInOrder; import static org.junit.Assert.assertThat; import org.junit.Test; /** * * @author Lars Eriksson (larsq.eriksson@gmail.com) */ public class KnappsackProblemTest { private final static Logger LOG = Logger.getLogger(KnappsackProblemTest.class.getPackage().getName()); private final static Shape shape22 = new Shape(2, 2, 1); private final static Shape shape21 = new Shape(2, 1, 1); private final static Shape shape11 = new Shape(1, 1, 1); private final static Shape shape12 = new Shape(1, 2, 1); private final ObjectMapper mapper = new ObjectMapper(); private EquivalenceTester<Shape> shapeTester = EquivalenceTester.equalsWhen(Shape::getCols, Shape::getRows, Shape::getValue); private String readResource(String resourceName) throws IOException { InputStream in = getClass().getResourceAsStream(resourceName); Verify.verifyNotNull(in); try { return CharStreams.toString(new InputStreamReader(in)); } finally { in.close(); } } private <T> T readJson(String resourceName, Class<T> clz) throws IOException { return mapper.readValue(readResource(resourceName), clz); } private KnapsackProblem defineProblem() { KnapsackProblem problem = new KnapsackProblem(); Area area = new Area(8, 8, 1); area.addShape(shape22, 2, ImmutablePosition.of(0, 0)); area.addShape(shape22, 3, ImmutablePosition.of(2, 2)); area.addShape(shape22, 4, ImmutablePosition.of(4, 4)); area.addShape(shape22, 5, ImmutablePosition.of(6, 6)); problem.addShapes(shape22, "1", "2", "9", "10"); //16 problem.addShapes(shape21, "3", "4", "5"); //6 problem.addShapes(shape12, "6", "7", "8"); //6 problem.setArea(area); return problem; } public KnappsackProblemTest() { } @Test public void testProblemSerializeDeserialize() throws JsonProcessingException, IOException { KnapsackProblem original = defineProblem(); EquivalenceTester<KnapsackProblem> test = EquivalenceTester.equalsWhen(KnapsackProblem::getArea, KnapsackProblem::getShapes); String json = mapper.writeValueAsString(original); LOG.info(String.format("json: %s", json)); KnapsackProblem deserialized; try { deserialized = mapper.readValue(json, KnapsackProblem.class); } catch (JsonProcessingException e) { LOG.log(Level.SEVERE, e.getMessage(), e); throw e; } assertThat(deserialized.getShapes(), is(original.getShapes())); } private <T, S extends Collection<T>, COLLECTION extends Collection<S>> ImmutableList<ImmutableList<T>> convert( COLLECTION collection) { ImmutableList<ImmutableList<T>> list = ImmutableList .copyOf(collection.stream().map(c -> ImmutableList.copyOf(c)).collect(Collectors.toList())); return list; } @Test public void testLayout() { Area area = new Area(12, 12, 1); area.addShape(shape22, 2, ImmutablePosition.of(0, 0)); area.addShape(shape22, 3, ImmutablePosition.of(2, 2)); area.addShape(shape22, 4, ImmutablePosition.of(4, 4)); area.addShape(shape22, 5, ImmutablePosition.of(6, 6)); OffsetIntMatrix m = area.asMatrix(ImmutablePosition.of(-2, -2), -1); LOG.info("\n" + IntFunctions.toString(m)); } }