RegisterArray.java revision 12657:6ef01bd40ce2
122347Spst/*
222347Spst * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
329964Sache * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
492906Smarkm *
522347Spst * This code is free software; you can redistribute it and/or modify it
622347Spst * under the terms of the GNU General Public License version 2 only, as
722347Spst * published by the Free Software Foundation.
822347Spst *
922347Spst * This code is distributed in the hope that it will be useful, but WITHOUT
1022347Spst * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1122347Spst * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1222347Spst * version 2 for more details (a copy is included in the LICENSE file that
1322347Spst * accompanied this code).
1422347Spst *
1522347Spst * You should have received a copy of the GNU General Public License version
1622347Spst * 2 along with this work; if not, write to the Free Software Foundation,
1722347Spst * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1822347Spst *
1922347Spst * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2022347Spst * or visit www.oracle.com if you need additional information or have any
2122347Spst * questions.
2222347Spst */
2322347Spstpackage jdk.vm.ci.code;
2422347Spst
2522347Spstimport java.util.Arrays;
2622347Spstimport java.util.Collection;
2722347Spstimport java.util.Collections;
2822347Spstimport java.util.Iterator;
2922347Spstimport java.util.List;
3022347Spst
3122347Spst/**
3222347Spst * An immutable ordered list of registers. Only required because Java lacks immutable arrays.
3322347Spst */
3422347Spstpublic final class RegisterArray implements Iterable<Register> {
3522347Spst
3622347Spst    private final Register[] registers;
3722347Spst    private int hash;
3822347Spst
3922347Spst    public RegisterArray(Register... registers) {
4022347Spst        this.registers = registers;
4122347Spst    }
4222347Spst
4322347Spst    public RegisterArray(Collection<Register> registers) {
4422347Spst        this.registers = registers.toArray(new Register[registers.size()]);
4522347Spst    }
4622347Spst
4722347Spst    /**
4822347Spst     * Gets the number of registers.
4922347Spst     */
5022347Spst    public int size() {
5122347Spst        return registers.length;
5222347Spst    }
5322347Spst
5422347Spst    /**
5522347Spst     * Gets the register at a given index.
5622347Spst     *
5722347Spst     * @param index the index of the register to retrieve
5822347Spst     */
5922347Spst    public Register get(int index) {
6022347Spst        return registers[index];
6122347Spst    }
6222347Spst
6322347Spst    public void addTo(Collection<Register> collection) {
6422347Spst        collection.addAll(Arrays.asList(registers));
6522347Spst    }
6622347Spst
6722347Spst    /**
6822347Spst     * Gets an immutable view of the registers as a list.
6922347Spst     */
7022347Spst    public List<Register> asList() {
7122347Spst        return Collections.unmodifiableList(Arrays.asList(registers));
7222347Spst    }
7322347Spst
7422347Spst    /**
7522347Spst     * Gets a copy of the registers as an array.
7622347Spst     */
7722347Spst    public Register[] toArray() {
7822347Spst        return registers.clone();
7922347Spst    }
8022347Spst
8122347Spst    public Iterator<Register> iterator() {
8222347Spst        return Arrays.asList(registers).iterator();
83    }
84
85    @Override
86    public int hashCode() {
87        if (hash == 0 && registers.length > 0) {
88            hash = Arrays.hashCode(registers);
89        }
90        return hash;
91    }
92
93    @Override
94    public boolean equals(Object obj) {
95        if (obj instanceof RegisterArray) {
96            return Arrays.equals(registers, ((RegisterArray) obj).registers);
97        }
98        return false;
99    }
100
101    @Override
102    public String toString() {
103        return Arrays.toString(registers);
104    }
105}
106