com.zaradai.primes.Guess.java Source code

Java tutorial

Introduction

Here is the source code for com.zaradai.primes.Guess.java

Source

/**
 * Copyright 2014 Zaradai
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zaradai.primes;

import com.google.common.base.Preconditions;

import java.util.Arrays;

public class Guess implements Cloneable {
    private final int[] rowData;
    private int targetColumn;

    private Guess(int rowSize, int column) {
        rowData = new int[rowSize];
        targetColumn = column;
    }

    private Guess(int rowSize, int column, int[] rowData) {
        this.rowData = Arrays.copyOf(rowData, rowSize);
        targetColumn = column;
    }

    public static Guess newInstance(int rowSize, int column) {
        return new Guess(rowSize, column);
    }

    public int rows() {
        return rowData.length;
    }

    public int getTargetColumn() {
        return targetColumn;
    }

    public int get(int index) {
        Preconditions.checkPositionIndex(index, rows(), "Invalid index");

        return rowData[index];
    }

    public void set(int index, int rowValue) {
        Preconditions.checkPositionIndex(index, rows(), "Invalid index");

        rowData[index] = rowValue;
    }

    @Override
    protected Object clone() {
        return new Guess(rows(), targetColumn, rowData);
    }

    /**
     * Get the value of a column up to number of digits.  The columns position
     * is 0 for the LSD of each row.
     * <p>
     * For Example:
     *  12345
     *  23456
     *  34567
     *  10023
     *  00062
     *
     * The value for column 0 is 56732
     * </p>
     * @param column
     * @param numOfDigits
     * @return
     */
    public int getColumnValue(int column, int numOfDigits) {
        int result = 0;
        int multiplier = 1;
        // as we are counting num of digits, the lowest significant digit is row(numOfDigits-1)
        for (int row = numOfDigits - 1; row >= 0; --row) {
            result += ((Util.getDigit(get(row), column)) * multiplier);
            multiplier *= 10;
        }

        return result;
    }

    /**
     * Get the value of the diagonal starting from top left to bottom right.
     * <p>
     * For Example.
     *
     * 12345
     * 23456
     * 34567
     * 10023
     * 00062
     *
     * The value is 13522
     *
     * </p>
     * @param numOfDigits
     */
    public int getLeftToRightDiagonal(int numOfDigits) {
        int result = 0;
        int multiplier = 1;

        for (int row = numOfDigits - 1, digit = rows() - numOfDigits; row >= 0; --row, ++digit) {
            result += ((Util.getDigit(get(row), digit)) * multiplier);
            multiplier *= 10;
        }

        return result;
    }

    /**
     * Get the value of the diagonal starting from top right to bottom left.
     * <p>
     * For Example.
     *
     * 12345
     * 23456
     * 34567
     * 10023
     * 00062
     *
     * The value is 55500
     *
     * </p>
     * @param numOfDigits
     */
    public int getRightToLeftDiagonal(int numOfDigits) {
        int result = 0;
        int multiplier = 1;

        for (int rowcol = numOfDigits - 1; rowcol >= 0; --rowcol) {
            result += ((Util.getDigit(get(rowcol), rowcol)) * multiplier);
            multiplier *= 10;
        }

        return result;
    }
}