1 /*
2  * Copyright (C) 2011 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.dx.io.instructions;
18 
19 /**
20  * Base implementation of {@link CodeCursor}.
21  */
22 public abstract class BaseCodeCursor implements CodeCursor {
23     /** base address map */
24     private final AddressMap baseAddressMap;
25 
26     /** next index within {@link #array} to read from or write to */
27     private int cursor;
28 
29     /**
30      * Constructs an instance.
31      */
BaseCodeCursor()32     public BaseCodeCursor() {
33         this.baseAddressMap = new AddressMap();
34         this.cursor = 0;
35     }
36 
37     /** @inheritDoc */
cursor()38     public final int cursor() {
39         return cursor;
40     }
41 
42     /** @inheritDoc */
baseAddressForCursor()43     public final int baseAddressForCursor() {
44         int mapped = baseAddressMap.get(cursor);
45         return (mapped >= 0) ? mapped : cursor;
46     }
47 
48     /** @inheritDoc */
setBaseAddress(int targetAddress, int baseAddress)49     public final void setBaseAddress(int targetAddress, int baseAddress) {
50         baseAddressMap.put(targetAddress, baseAddress);
51     }
52 
53     /**
54      * Advance the cursor by the indicated amount.
55      */
advance(int amount)56     protected final void advance(int amount) {
57         cursor += amount;
58     }
59 }
60