1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.code;
  24 
  25 import java.util.Arrays;
  26 import java.util.Collection;
  27 import java.util.Collections;
  28 import java.util.Iterator;
  29 import java.util.List;
  30 
  31 /**
  32  * An immutable ordered list of registers. Only required because Java lacks immutable arrays.
  33  */
  34 public final class RegisterArray implements Iterable<Register> {
  35 
  36     private final Register[] registers;
  37     private int hash;
  38 
  39     public RegisterArray(Register... registers) {
  40         this.registers = registers;
  41     }
  42 
  43     public RegisterArray(Collection<Register> registers) {
  44         this.registers = registers.toArray(new Register[registers.size()]);
  45     }
  46 
  47     /**
  48      * Gets the number of registers.
  49      */
  50     public int size() {
  51         return registers.length;
  52     }
  53 
  54     /**
  55      * Gets the register at a given index.
  56      *
  57      * @param index the index of the register to retrieve
  58      */
  59     public Register get(int index) {
  60         return registers[index];
  61     }
  62 
  63     public void addTo(Collection<Register> collection) {
  64         collection.addAll(Arrays.asList(registers));
  65     }
  66 
  67     /**
  68      * Gets an immutable view of the registers as a list.
  69      */
  70     public List<Register> asList() {
  71         return Collections.unmodifiableList(Arrays.asList(registers));
  72     }
  73 
  74     /**
  75      * Gets a copy of the registers as an array.
  76      */
  77     public Register[] toArray() {
  78         return registers.clone();
  79     }
  80 
  81     public Iterator<Register> iterator() {
  82         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 }