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);
            }
        }
    }
}

@@ -527,24 +527,63 @@
 nt digit) %7B%0A
+        if (mCount %3C MAX_DIGITS) %7B%0A    
         mInp
@@ -606,24 +606,28 @@
 it;%0A        
+ 
+   
 mCount++;%0A  
@@ -624,16 +624,26 @@
 ount++;%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1795,92 +1795,148 @@
 if (
-mCount == mInput.length)%0A                break;%0A            if (d %3E= 0 && d %3C 10) %7B%0A
+d %3C 0 %7C%7C d %3E 9)%0A                throw new IllegalArgumentException(%22Cannot store digit %22 + d);%0A            if (mCount == MAX_DIGITS)%0A       
     
@@ -1940,23 +1940,35 @@
         
+break;%0A
        
+     
 storeDig
@@ -1978,22 +1978,8 @@
 d);%0A
-            %7D%0A
     

Add checks when storing digits.