package com.philliphsu.bottomsheetpickers.view.numberpad;

import java.util.Arrays;

/**
 * Model that encapsulates data pertaining to the inputted time
 * in a number pad time picker.
 */
final class DigitwiseTimeModel {
    private final int[] digits = new int[4];

    private int count;

    DigitwiseTimeModel() {
        Arrays.fill(digits, -1);
    }

    void storeDigit(int digit) {
        digits[count] = digit;
        count++;
    }

    int getDigit(int at) {
        return digits[at];
    }

    void removeDigit() {
        if (count > 0) {
            count--; // move the cursor back
            digits[count] = -1;
        }
    }

    int count() {
        return count;
    }
}

package com.philliphsu.bottomsheetpickers.view.numberpad;

import java.util.Arrays;

/**
 * Model that encapsulates data pertaining to the inputted time
 * in a number pad time picker.
 */
final class DigitwiseTimeModel {
    private static final int MAX_DIGITS = 4;
    private static final int UNMODIFIED = -1;

    private final int[] mInput = new int[MAX_DIGITS];

    private int mCount;

    DigitwiseTimeModel() {
        // Set contents of input array to UNMODIFIED.
        clearDigits();
    }

    void storeDigit(int digit) {
        mInput[mCount] = digit;
        mCount++;
    }

    int getDigit(int at) {
        return mInput[at];
    }

    /**
     * @return a defensive copy of the internal array of inputted digits
     */
    int[] getDigits() {
        int[] digits = new int[mInput.length];
        System.arraycopy(mInput, 0, digits, 0, mInput.length);
        return digits;
    }

    void removeDigit() {
        if (mCount > 0) {
            mCount--; // move the cursor back
            mInput[mCount] = UNMODIFIED;
        }
    }

    void clearDigits() {
        Arrays.fill(mInput, UNMODIFIED);
        mCount = 0;
    }

    int count() {
        return mCount;
    }

    /**
     * @return the integer represented by the inputted digits
     */
    int getInput() {
        if (mCount <= 0) return UNMODIFIED;

        int result = 0;
        for (int i = 0; i < mCount; i++) {
            result = result * 10 + mInput[i];
        }
        return result;
    }

    /**
     * Inserts as many of the digits in the given sequence
     * into the input as possible.
     */
    void storeDigits(int... digits) {
        if (digits == null)
            return;
        for (int d : digits) {
            if (mCount == mInput.length)
                break;
            if (d >= 0 && d < 10) {
                storeDigit(d);
            }
        }
    }
}

Implement DigitwiseTimeModel.