local.laer.app.knapsack.domain.support.Shape.java Source code

Java tutorial

Introduction

Here is the source code for local.laer.app.knapsack.domain.support.Shape.java

Source

/*
 * 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.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
import local.laer.app.knapsack.domain.Position;
import local.laer.app.knapsack.support.IntFunctions;
import local.laer.app.knapsack.support.OffsetIntMatrix;

/**
 *
 * @author Lars Eriksson (larsq.eriksson@gmail.com)
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Shape {

    protected Integer rows;
    protected Integer cols;
    protected int value;

    public Shape(Integer rows, Integer cols, int value) {
        this.rows = rows;
        this.cols = cols;
        this.value = value;
    }

    public Shape() {
    }

    @JsonProperty("rows")
    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    @JsonProperty("cols")
    public Integer getCols() {
        return cols;
    }

    public void setCols(Integer cols) {
        this.cols = cols;
    }

    @JsonProperty("value")
    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.format("%s {rows=%s, cols=%s, value=%s}", getClass().getSimpleName(), rows, cols, value);
    }

    public static Shape of(int rows, int cols, int value) {
        return new Shape(rows, cols, value);
    }

    public OffsetIntMatrix asMatrix(Position offset, Integer defaultValue) {
        OffsetIntMatrix m = new OffsetIntMatrix(offset.getRow(), offset.getCol(), rows, cols, defaultValue);
        return m.transform(IntFunctions.fillWith(value));
    }

    @Override
    public int hashCode() {
        return Objects.hash(rows, cols, value);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || obj == this) {
            return obj == this;
        }

        if (!Objects.equals(getClass(), obj.getClass())) {
            return false;
        }

        Shape other = getClass().cast(obj);

        return Objects.equals(rows, other.rows) && Objects.equals(cols, other.cols) && value == other.value;
    }

}