1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car.obd2;
18
19import java.util.function.Function;
20
21/**
22 * A wrapper over an int[] that offers a moving offset into the array, allowing for sequential
23 * consumption of the array data
24 */
25public class IntegerArrayStream {
26    private final int[] mData;
27    private int mIndex;
28
29    public IntegerArrayStream(int[] data) {
30        mData = data;
31        mIndex = 0;
32    }
33
34    public int peek() {
35        return mData[mIndex];
36    }
37
38    public int consume() {
39        return mData[mIndex++];
40    }
41
42    public int residualLength() {
43        return mData.length - mIndex;
44    }
45
46    public boolean hasAtLeast(int n) {
47        return residualLength() >= n;
48    }
49
50    public <T> T hasAtLeast(int n, Function<IntegerArrayStream, T> ifTrue) {
51        return hasAtLeast(n, ifTrue, null);
52    }
53
54    public <T> T hasAtLeast(
55            int n,
56            Function<IntegerArrayStream, T> ifTrue,
57            Function<IntegerArrayStream, T> ifFalse) {
58        if (hasAtLeast(n)) {
59            return ifTrue.apply(this);
60        } else {
61            if (ifFalse != null) {
62                return ifFalse.apply(this);
63            } else {
64                return null;
65            }
66        }
67    }
68
69    /**
70     * Validates the content of this stream against an expected data-set.
71     *
72     * <p>If any element of values causes a mismatch, that element will not be consumed and this
73     * method will return false. All elements that do match are consumed from the stream.
74     *
75     * <p>For instance, given a stream with {1,2,3,4}, a call of expect(1,2,5) will consume 1 and 2,
76     * will return false, and stream.peek() will return 3 since it is the first element that did not
77     * match and was not consumed.
78     *
79     * @param values The values to compare this stream's elements against.
80     * @return true if all elements of values match this stream, false otherwise.
81     */
82    public boolean expect(int... values) {
83        if (!hasAtLeast(values.length)) {
84            return false;
85        }
86        for (int value : values) {
87            if (value != peek()) {
88                return false;
89            } else {
90                consume();
91            }
92        }
93        return true;
94    }
95}
96