• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.car.obd2;
18 
19 import 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  */
25 public class IntegerArrayStream {
26     private final int[] mData;
27     private int mIndex;
28 
IntegerArrayStream(int[] data)29     public IntegerArrayStream(int[] data) {
30         mData = data;
31         mIndex = 0;
32     }
33 
peek()34     public int peek() {
35         return mData[mIndex];
36     }
37 
consume()38     public int consume() {
39         return mData[mIndex++];
40     }
41 
residualLength()42     public int residualLength() {
43         return mData.length - mIndex;
44     }
45 
isEmpty()46     public boolean isEmpty() {
47         return residualLength() == 0;
48     }
49 
hasAtLeast(int n)50     public boolean hasAtLeast(int n) {
51         return residualLength() >= n;
52     }
53 
hasAtLeast(int n, Function<IntegerArrayStream, T> ifTrue)54     public <T> T hasAtLeast(int n, Function<IntegerArrayStream, T> ifTrue) {
55         return hasAtLeast(n, ifTrue, null);
56     }
57 
hasAtLeast( int n, Function<IntegerArrayStream, T> ifTrue, Function<IntegerArrayStream, T> ifFalse)58     public <T> T hasAtLeast(
59             int n,
60             Function<IntegerArrayStream, T> ifTrue,
61             Function<IntegerArrayStream, T> ifFalse) {
62         if (hasAtLeast(n)) {
63             return ifTrue.apply(this);
64         } else {
65             if (ifFalse != null) {
66                 return ifFalse.apply(this);
67             } else {
68                 return null;
69             }
70         }
71     }
72 
73     /**
74      * Validates the content of this stream against an expected data-set.
75      *
76      * <p>If any element of values causes a mismatch, that element will not be consumed and this
77      * method will return false. All elements that do match are consumed from the stream.
78      *
79      * <p>For instance, given a stream with {1,2,3,4}, a call of expect(1,2,5) will consume 1 and 2,
80      * will return false, and stream.peek() will return 3 since it is the first element that did not
81      * match and was not consumed.
82      *
83      * @param values The values to compare this stream's elements against.
84      * @return true if all elements of values match this stream, false otherwise.
85      */
expect(int... values)86     public boolean expect(int... values) {
87         if (!hasAtLeast(values.length)) {
88             return false;
89         }
90         for (int value : values) {
91             if (value != peek()) {
92                 return false;
93             } else {
94                 consume();
95             }
96         }
97         return true;
98     }
99 }
100