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